diff --git a/cmd/ipfswatch/main.go b/cmd/ipfswatch/main.go index 3178cf564..916a420d1 100644 --- a/cmd/ipfswatch/main.go +++ b/cmd/ipfswatch/main.go @@ -13,6 +13,7 @@ import ( "syscall" commands "github.com/ipfs/kubo/commands" + "github.com/ipfs/kubo/config" core "github.com/ipfs/kubo/core" coreapi "github.com/ipfs/kubo/core/coreapi" corehttp "github.com/ipfs/kubo/core/corehttp" @@ -25,10 +26,18 @@ import ( var ( http = flag.Bool("http", false, "expose IPFS HTTP API") - repoPath = flag.String("repo", os.Getenv("IPFS_PATH"), "IPFS_PATH to use") + repoPath *string watchPath = flag.String("path", ".", "the path to watch") ) +func init() { + ipfsPath, err := config.PathRoot() + if err != nil { + ipfsPath = os.Getenv(config.EnvDir) + } + repoPath = flag.String("repo", ipfsPath, "repo path to use") +} + func main() { flag.Parse() diff --git a/core/commands/sysdiag.go b/core/commands/sysdiag.go index 123dcb973..5a7c41ce9 100644 --- a/core/commands/sysdiag.go +++ b/core/commands/sysdiag.go @@ -2,14 +2,13 @@ package commands import ( "os" - "path" "runtime" + "github.com/ipfs/go-ipfs-cmds" version "github.com/ipfs/kubo" + "github.com/ipfs/kubo/config" "github.com/ipfs/kubo/core" cmdenv "github.com/ipfs/kubo/core/commands/cmdenv" - - cmds "github.com/ipfs/go-ipfs-cmds" manet "github.com/multiformats/go-multiaddr/net" sysi "github.com/whyrusleeping/go-sysinfo" ) @@ -84,32 +83,28 @@ func runtimeInfo(out map[string]interface{}) error { func envVarInfo(out map[string]interface{}) error { ev := make(map[string]interface{}) ev["GOPATH"] = os.Getenv("GOPATH") - ev["IPFS_PATH"] = os.Getenv("IPFS_PATH") + ev[config.EnvDir] = os.Getenv(config.EnvDir) out["environment"] = ev return nil } -func ipfsPath() string { - p := os.Getenv("IPFS_PATH") - if p == "" { - p = path.Join(os.Getenv("HOME"), ".ipfs") - } - return p -} - func diskSpaceInfo(out map[string]interface{}) error { - di := make(map[string]interface{}) - dinfo, err := sysi.DiskUsage(ipfsPath()) + pathRoot, err := config.PathRoot() + if err != nil { + return err + } + dinfo, err := sysi.DiskUsage(pathRoot) if err != nil { return err } - di["fstype"] = dinfo.FsType - di["total_space"] = dinfo.Total - di["free_space"] = dinfo.Free + out["diskinfo"] = map[string]interface{}{ + "fstype": dinfo.FsType, + "total_space": dinfo.Total, + "free_space": dinfo.Free, + } - out["diskinfo"] = di return nil } diff --git a/repo/fsrepo/migrations/fetch_test.go b/repo/fsrepo/migrations/fetch_test.go index 6e87c966b..c09b3444a 100644 --- a/repo/fsrepo/migrations/fetch_test.go +++ b/repo/fsrepo/migrations/fetch_test.go @@ -20,10 +20,7 @@ func TestGetDistPath(t *testing.T) { } testDist := "/unit/test/dist" - err := os.Setenv(envIpfsDistPath, testDist) - if err != nil { - panic(err) - } + t.Setenv(envIpfsDistPath, testDist) defer func() { os.Unsetenv(envIpfsDistPath) }() @@ -139,18 +136,12 @@ func TestFetchBinary(t *testing.T) { if err != nil { panic(err) } - err = os.Setenv("TMPDIR", tmpDir) - if err != nil { - panic(err) - } + t.Setenv("TMPDIR", tmpDir) _, err = FetchBinary(ctx, fetcher, "go-ipfs", "v1.0.0", "ipfs", tmpDir) if !os.IsPermission(err) { t.Error("expected 'permission' error, got:", err) } - err = os.Setenv("TMPDIR", "/tmp") - if err != nil { - panic(err) - } + t.Setenv("TMPDIR", "/tmp") err = os.Chmod(tmpDir, 0o755) if err != nil { panic(err) diff --git a/repo/fsrepo/migrations/ipfsdir.go b/repo/fsrepo/migrations/ipfsdir.go index 8cb087d53..88b39459b 100644 --- a/repo/fsrepo/migrations/ipfsdir.go +++ b/repo/fsrepo/migrations/ipfsdir.go @@ -8,12 +8,11 @@ import ( "strconv" "strings" + "github.com/ipfs/kubo/config" "github.com/ipfs/kubo/misc/fsutil" ) const ( - envIpfsPath = "IPFS_PATH" - defIpfsDir = ".ipfs" versionFile = "version" ) @@ -24,25 +23,16 @@ const ( func IpfsDir(dir string) (string, error) { var err error if dir == "" { - dir = os.Getenv(envIpfsPath) - } - if dir != "" { - dir, err = fsutil.ExpandHome(dir) + dir, err = config.PathRoot() if err != nil { return "", err } - return dir, nil } - - home, err := os.UserHomeDir() + dir, err = fsutil.ExpandHome(dir) if err != nil { return "", err } - if home == "" { - return "", errors.New("could not determine IPFS_PATH, home dir not set") - } - - return filepath.Join(home, defIpfsDir), nil + return dir, nil } // CheckIpfsDir gets the ipfs directory and checks that the directory exists. diff --git a/repo/fsrepo/migrations/ipfsdir_test.go b/repo/fsrepo/migrations/ipfsdir_test.go index e4e626794..c94ebc586 100644 --- a/repo/fsrepo/migrations/ipfsdir_test.go +++ b/repo/fsrepo/migrations/ipfsdir_test.go @@ -4,24 +4,28 @@ import ( "os" "path/filepath" "testing" -) -var ( - fakeHome string - fakeIpfs string + "github.com/ipfs/kubo/config" ) func TestRepoDir(t *testing.T) { - fakeHome = t.TempDir() - os.Setenv("HOME", fakeHome) - fakeIpfs = filepath.Join(fakeHome, ".ipfs") + fakeHome := t.TempDir() + t.Setenv("HOME", fakeHome) + fakeIpfs := filepath.Join(fakeHome, ".ipfs") + t.Setenv(config.EnvDir, fakeIpfs) - t.Run("testIpfsDir", testIpfsDir) - t.Run("testCheckIpfsDir", testCheckIpfsDir) - t.Run("testRepoVersion", testRepoVersion) + t.Run("testIpfsDir", func(t *testing.T) { + testIpfsDir(t, fakeIpfs) + }) + t.Run("testCheckIpfsDir", func(t *testing.T) { + testCheckIpfsDir(t, fakeIpfs) + }) + t.Run("testRepoVersion", func(t *testing.T) { + testRepoVersion(t, fakeIpfs) + }) } -func testIpfsDir(t *testing.T) { +func testIpfsDir(t *testing.T, fakeIpfs string) { _, err := CheckIpfsDir("") if err == nil { t.Fatal("expected error when no .ipfs directory to find") @@ -37,16 +41,16 @@ func testIpfsDir(t *testing.T) { t.Fatal(err) } if dir != fakeIpfs { - t.Fatal("wrong ipfs directory:", dir) + t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs) } - os.Setenv(envIpfsPath, "~/.ipfs") + t.Setenv(config.EnvDir, "~/.ipfs") dir, err = IpfsDir("") if err != nil { t.Fatal(err) } if dir != fakeIpfs { - t.Fatal("wrong ipfs directory:", dir) + t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs) } _, err = IpfsDir("~somesuer/foo") @@ -54,15 +58,12 @@ func testIpfsDir(t *testing.T) { t.Fatal("expected error with user-specific home dir") } - err = os.Setenv(envIpfsPath, "~somesuer/foo") - if err != nil { - panic(err) - } + t.Setenv(config.EnvDir, "~somesuer/foo") _, err = IpfsDir("~somesuer/foo") if err == nil { t.Fatal("expected error with user-specific home dir") } - err = os.Unsetenv(envIpfsPath) + err = os.Unsetenv(config.EnvDir) if err != nil { panic(err) } @@ -72,7 +73,7 @@ func testIpfsDir(t *testing.T) { t.Fatal(err) } if dir != fakeIpfs { - t.Fatal("wrong ipfs directory:", dir) + t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs) } _, err = IpfsDir("") @@ -81,7 +82,7 @@ func testIpfsDir(t *testing.T) { } } -func testCheckIpfsDir(t *testing.T) { +func testCheckIpfsDir(t *testing.T, fakeIpfs string) { _, err := CheckIpfsDir("~somesuer/foo") if err == nil { t.Fatal("expected error with user-specific home dir") @@ -101,7 +102,7 @@ func testCheckIpfsDir(t *testing.T) { } } -func testRepoVersion(t *testing.T) { +func testRepoVersion(t *testing.T, fakeIpfs string) { badDir := "~somesuer/foo" _, err := RepoVersion(badDir) if err == nil { diff --git a/repo/fsrepo/migrations/migrations_test.go b/repo/fsrepo/migrations/migrations_test.go index f690290f8..c84e2d228 100644 --- a/repo/fsrepo/migrations/migrations_test.go +++ b/repo/fsrepo/migrations/migrations_test.go @@ -33,9 +33,7 @@ func TestFindMigrations(t *testing.T) { createFakeBin(i-1, i, tmpDir) } - origPath := os.Getenv("PATH") - os.Setenv("PATH", tmpDir) - defer os.Setenv("PATH", origPath) + t.Setenv("PATH", tmpDir) migs, bins, err = findMigrations(ctx, 0, 5) if err != nil { @@ -80,9 +78,7 @@ func TestFindMigrationsReverse(t *testing.T) { createFakeBin(i-1, i, tmpDir) } - origPath := os.Getenv("PATH") - os.Setenv("PATH", tmpDir) - defer os.Setenv("PATH", origPath) + t.Setenv("PATH", tmpDir) migs, bins, err = findMigrations(ctx, 5, 0) if err != nil { @@ -144,10 +140,8 @@ func TestFetchMigrations(t *testing.T) { } func TestRunMigrations(t *testing.T) { - fakeHome := t.TempDir() - - os.Setenv("HOME", fakeHome) - fakeIpfs := filepath.Join(fakeHome, ".ipfs") + fakeIpfs := filepath.Join(t.TempDir(), ".ipfs") + t.Setenv(config.EnvDir, fakeIpfs) err := os.Mkdir(fakeIpfs, os.ModePerm) if err != nil {