Skip to content

Commit

Permalink
Stop using github.com/mitchellh/go-homedir
Browse files Browse the repository at this point in the history
This repo is archived, no longer needed, and no longer maintained.
  • Loading branch information
gammazero committed Oct 2, 2024
1 parent 9b2309d commit 7fc5a00
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 23 deletions.
4 changes: 2 additions & 2 deletions assigner/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"

sticfg "github.com/ipni/storetheindex/config"
"github.com/mitchellh/go-homedir"
"github.com/ipni/storetheindex/fsutil"
)

// Config is used to load config files.
Expand Down Expand Up @@ -65,7 +65,7 @@ func PathRoot() (string, error) {
if dir != "" {
return dir, nil
}
return homedir.Expand(DefaultPathRoot)
return fsutil.ExpandHome(DefaultPathRoot)
}

// Load reads the json-serialized config at the specified path.
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"path/filepath"

"github.com/mitchellh/go-homedir"
"github.com/ipni/storetheindex/fsutil"
)

// Config is used to load config files.
Expand Down Expand Up @@ -74,7 +74,7 @@ func PathRoot() (string, error) {
if dir != "" {
return dir, nil
}
return homedir.Expand(DefaultPathRoot)
return fsutil.ExpandHome(DefaultPathRoot)
}

// Load reads the json-serialized config at the specified path.
Expand Down
59 changes: 43 additions & 16 deletions fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package fsutil
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"time"

"github.com/mitchellh/go-homedir"
)

// DirWritable checks if a directory is writable. If the directory does
Expand All @@ -17,37 +17,64 @@ func DirWritable(dir string) error {
}

var err error
dir, err = homedir.Expand(dir)
dir, err = ExpandHome(dir)
if err != nil {
return err
}

if _, err = os.Stat(dir); err != nil {
if errors.Is(err, os.ErrNotExist) {
// dir doesn't exist, check that we can create it
err = os.Mkdir(dir, 0o775)
fi, err := os.Stat(dir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
// Directory does not exist, so create it.
err = os.Mkdir(dir, 0775)
if err == nil {
return nil
}
}
if errors.Is(err, os.ErrPermission) {
err = os.ErrPermission
if errors.Is(err, fs.ErrPermission) {
err = fs.ErrPermission
}
return fmt.Errorf("cannot write to %s: %w", dir, err)
return fmt.Errorf("directory not writable: %s: %w", dir, err)
}
if !fi.IsDir() {
return fmt.Errorf("not a directory: %s", dir)
}

// dir exists, make sure we can write to it
file, err := os.CreateTemp(dir, "test")
// Directory exists, check that a file can be written.
file, err := os.CreateTemp(dir, "writetest")
if err != nil {
if errors.Is(err, os.ErrPermission) {
err = os.ErrPermission
if errors.Is(err, fs.ErrPermission) {
err = fs.ErrPermission
}
return fmt.Errorf("cannot write to %s: %w", dir, err)
return fmt.Errorf("directory not writable: %s: %w", dir, err)
}
file.Close()
return os.Remove(file.Name())
}

// ExpandHome expands the path to include the home directory if the path is
// prefixed with `~`. If it isn't prefixed with `~`, the path is returned
// as-is.
func ExpandHome(path string) (string, error) {
if path == "" {
return path, nil
}

if path[0] != '~' {
return path, nil
}

if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
return "", errors.New("cannot expand user-specific home dir")
}

dir, err := os.UserHomeDir()
if err != nil {
return "", err
}

return filepath.Join(dir, path[1:]), nil
}

// FileChanged returns the modification time of a file and true if different
// from the given time.
func FileChanged(filePath string, modTime time.Time) (time.Time, bool, error) {
Expand Down
33 changes: 33 additions & 0 deletions fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,36 @@ func TestFileExists(t *testing.T) {

require.True(t, fsutil.FileExists(fileName))
}

func TestExpandHome(t *testing.T) {
dir, err := fsutil.ExpandHome("")
require.NoError(t, err)
require.Equal(t, "", dir)

origDir := filepath.Join("somedir", "somesub")
dir, err = fsutil.ExpandHome(origDir)
require.NoError(t, err)
require.Equal(t, origDir, dir)

_, err = fsutil.ExpandHome(filepath.FromSlash("~nosuchuser/somedir"))
require.Error(t, err)

homeEnv := "HOME"
if runtime.GOOS == "windows" {
homeEnv = "USERPROFILE"
}
origHome := os.Getenv(homeEnv)
defer os.Setenv(homeEnv, origHome)
homeDir := filepath.Join(t.TempDir(), "testhome")
os.Setenv(homeEnv, homeDir)

const subDir = "mytmp"
origDir = filepath.Join("~", subDir)
dir, err = fsutil.ExpandHome(origDir)
require.NoError(t, err)
require.Equal(t, filepath.Join(homeDir, subDir), dir)

os.Unsetenv(homeEnv)
_, err = fsutil.ExpandHome(origDir)
require.Error(t, err)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/ipni/go-libipni v0.5.25
github.com/libp2p/go-libp2p v0.36.3
github.com/libp2p/go-msgio v0.3.0
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multiaddr v0.13.0
github.com/multiformats/go-multicodec v0.9.0
github.com/multiformats/go-multihash v0.2.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,6 @@ github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1
github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 h1:dcztxKSvZ4Id8iPpHERQBbIJfabdt4wUm5qy3wOL2Zc=
Expand Down

0 comments on commit 7fc5a00

Please sign in to comment.