@cryptotaxi247 / kubo / commits / 17b0085fd

repo: use config api to get node root path (#10934)

Replaces #8964 Closes #8848

Andrew Gillis committed Sep 2, 2025 at 13:13 UTC 17b0085fdd25de076303b083b6385f884c712cf6
6 files changed +58 -78
cmd/ipfswatch/main.go
+10 -1
@@ -13,6 +13,7 @@ import (
13 "syscall"
14
15 commands "github.com/ipfs/kubo/commands"
16 + "github.com/ipfs/kubo/config"
17 core "github.com/ipfs/kubo/core"
18 coreapi "github.com/ipfs/kubo/core/coreapi"
19 corehttp "github.com/ipfs/kubo/core/corehttp"
@@ -25,10 +26,18 @@ import (
26
27 var (
28 http = flag.Bool("http", false, "expose IPFS HTTP API")
28 - repoPath = flag.String("repo", os.Getenv("IPFS_PATH"), "IPFS_PATH to use")
29 + repoPath *string
30 watchPath = flag.String("path", ".", "the path to watch")
31 )
32
33 +func init() {
34 + ipfsPath, err := config.PathRoot()
35 + if err != nil {
36 + ipfsPath = os.Getenv(config.EnvDir)
37 + }
38 + repoPath = flag.String("repo", ipfsPath, "repo path to use")
39 +}
40 +
41 func main() {
42 flag.Parse()
43
core/commands/sysdiag.go
+13 -18
@@ -2,14 +2,13 @@ package commands
2
3 import (
4 "os"
5 - "path"
5 "runtime"
6
7 + "github.com/ipfs/go-ipfs-cmds"
8 version "github.com/ipfs/kubo"
9 + "github.com/ipfs/kubo/config"
10 "github.com/ipfs/kubo/core"
11 cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
11 -
12 - cmds "github.com/ipfs/go-ipfs-cmds"
12 manet "github.com/multiformats/go-multiaddr/net"
13 sysi "github.com/whyrusleeping/go-sysinfo"
14 )
@@ -84,32 +83,28 @@ func runtimeInfo(out map[string]interface{}) error {
83 func envVarInfo(out map[string]interface{}) error {
84 ev := make(map[string]interface{})
85 ev["GOPATH"] = os.Getenv("GOPATH")
87 - ev["IPFS_PATH"] = os.Getenv("IPFS_PATH")
86 + ev[config.EnvDir] = os.Getenv(config.EnvDir)
87
88 out["environment"] = ev
89 return nil
90 }
91
93 -func ipfsPath() string {
94 - p := os.Getenv("IPFS_PATH")
95 - if p == "" {
96 - p = path.Join(os.Getenv("HOME"), ".ipfs")
97 - }
98 - return p
99 -}
100 -
92 func diskSpaceInfo(out map[string]interface{}) error {
102 - di := make(map[string]interface{})
103 - dinfo, err := sysi.DiskUsage(ipfsPath())
93 + pathRoot, err := config.PathRoot()
94 + if err != nil {
95 + return err
96 + }
97 + dinfo, err := sysi.DiskUsage(pathRoot)
98 if err != nil {
99 return err
100 }
101
108 - di["fstype"] = dinfo.FsType
109 - di["total_space"] = dinfo.Total
110 - di["free_space"] = dinfo.Free
102 + out["diskinfo"] = map[string]interface{}{
103 + "fstype": dinfo.FsType,
104 + "total_space": dinfo.Total,
105 + "free_space": dinfo.Free,
106 + }
107
112 - out["diskinfo"] = di
108 return nil
109 }
110
repo/fsrepo/migrations/fetch_test.go
+3 -12
@@ -20,10 +20,7 @@ func TestGetDistPath(t *testing.T) {
20 }
21
22 testDist := "/unit/test/dist"
23 - err := os.Setenv(envIpfsDistPath, testDist)
24 - if err != nil {
25 - panic(err)
26 - }
23 + t.Setenv(envIpfsDistPath, testDist)
24 defer func() {
25 os.Unsetenv(envIpfsDistPath)
26 }()
@@ -139,18 +136,12 @@ func TestFetchBinary(t *testing.T) {
136 if err != nil {
137 panic(err)
138 }
142 - err = os.Setenv("TMPDIR", tmpDir)
143 - if err != nil {
144 - panic(err)
145 - }
139 + t.Setenv("TMPDIR", tmpDir)
140 _, err = FetchBinary(ctx, fetcher, "go-ipfs", "v1.0.0", "ipfs", tmpDir)
141 if !os.IsPermission(err) {
142 t.Error("expected 'permission' error, got:", err)
143 }
150 - err = os.Setenv("TMPDIR", "/tmp")
151 - if err != nil {
152 - panic(err)
153 - }
144 + t.Setenv("TMPDIR", "/tmp")
145 err = os.Chmod(tmpDir, 0o755)
146 if err != nil {
147 panic(err)
repo/fsrepo/migrations/ipfsdir.go
+4 -14
@@ -8,12 +8,11 @@ import (
8 "strconv"
9 "strings"
10
11 + "github.com/ipfs/kubo/config"
12 "github.com/ipfs/kubo/misc/fsutil"
13 )
14
15 const (
15 - envIpfsPath = "IPFS_PATH"
16 - defIpfsDir = ".ipfs"
16 versionFile = "version"
17 )
18
@@ -24,25 +23,16 @@ const (
23 func IpfsDir(dir string) (string, error) {
24 var err error
25 if dir == "" {
27 - dir = os.Getenv(envIpfsPath)
28 - }
29 - if dir != "" {
30 - dir, err = fsutil.ExpandHome(dir)
26 + dir, err = config.PathRoot()
27 if err != nil {
28 return "", err
29 }
34 - return dir, nil
30 }
36 -
37 - home, err := os.UserHomeDir()
31 + dir, err = fsutil.ExpandHome(dir)
32 if err != nil {
33 return "", err
34 }
41 - if home == "" {
42 - return "", errors.New("could not determine IPFS_PATH, home dir not set")
43 - }
44 -
45 - return filepath.Join(home, defIpfsDir), nil
35 + return dir, nil
36 }
37
38 // CheckIpfsDir gets the ipfs directory and checks that the directory exists.
repo/fsrepo/migrations/ipfsdir_test.go
+24 -23
@@ -4,24 +4,28 @@ import (
4 "os"
5 "path/filepath"
6 "testing"
7 -)
7
9 -var (
10 - fakeHome string
11 - fakeIpfs string
8 + "github.com/ipfs/kubo/config"
9 )
10
11 func TestRepoDir(t *testing.T) {
15 - fakeHome = t.TempDir()
16 - os.Setenv("HOME", fakeHome)
17 - fakeIpfs = filepath.Join(fakeHome, ".ipfs")
18 -
19 - t.Run("testIpfsDir", testIpfsDir)
20 - t.Run("testCheckIpfsDir", testCheckIpfsDir)
21 - t.Run("testRepoVersion", testRepoVersion)
12 + fakeHome := t.TempDir()
13 + t.Setenv("HOME", fakeHome)
14 + fakeIpfs := filepath.Join(fakeHome, ".ipfs")
15 + t.Setenv(config.EnvDir, fakeIpfs)
16 +
17 + t.Run("testIpfsDir", func(t *testing.T) {
18 + testIpfsDir(t, fakeIpfs)
19 + })
20 + t.Run("testCheckIpfsDir", func(t *testing.T) {
21 + testCheckIpfsDir(t, fakeIpfs)
22 + })
23 + t.Run("testRepoVersion", func(t *testing.T) {
24 + testRepoVersion(t, fakeIpfs)
25 + })
26 }
27
24 -func testIpfsDir(t *testing.T) {
28 +func testIpfsDir(t *testing.T, fakeIpfs string) {
29 _, err := CheckIpfsDir("")
30 if err == nil {
31 t.Fatal("expected error when no .ipfs directory to find")
@@ -37,16 +41,16 @@ func testIpfsDir(t *testing.T) {
41 t.Fatal(err)
42 }
43 if dir != fakeIpfs {
40 - t.Fatal("wrong ipfs directory:", dir)
44 + t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs)
45 }
46
43 - os.Setenv(envIpfsPath, "~/.ipfs")
47 + t.Setenv(config.EnvDir, "~/.ipfs")
48 dir, err = IpfsDir("")
49 if err != nil {
50 t.Fatal(err)
51 }
52 if dir != fakeIpfs {
49 - t.Fatal("wrong ipfs directory:", dir)
53 + t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs)
54 }
55
56 _, err = IpfsDir("~somesuer/foo")
@@ -54,15 +58,12 @@ func testIpfsDir(t *testing.T) {
58 t.Fatal("expected error with user-specific home dir")
59 }
60
57 - err = os.Setenv(envIpfsPath, "~somesuer/foo")
58 - if err != nil {
59 - panic(err)
60 - }
61 + t.Setenv(config.EnvDir, "~somesuer/foo")
62 _, err = IpfsDir("~somesuer/foo")
63 if err == nil {
64 t.Fatal("expected error with user-specific home dir")
65 }
65 - err = os.Unsetenv(envIpfsPath)
66 + err = os.Unsetenv(config.EnvDir)
67 if err != nil {
68 panic(err)
69 }
@@ -72,7 +73,7 @@ func testIpfsDir(t *testing.T) {
73 t.Fatal(err)
74 }
75 if dir != fakeIpfs {
75 - t.Fatal("wrong ipfs directory:", dir)
76 + t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs)
77 }
78
79 _, err = IpfsDir("")
@@ -81,7 +82,7 @@ func testIpfsDir(t *testing.T) {
82 }
83 }
84
84 -func testCheckIpfsDir(t *testing.T) {
85 +func testCheckIpfsDir(t *testing.T, fakeIpfs string) {
86 _, err := CheckIpfsDir("~somesuer/foo")
87 if err == nil {
88 t.Fatal("expected error with user-specific home dir")
@@ -101,7 +102,7 @@ func testCheckIpfsDir(t *testing.T) {
102 }
103 }
104
104 -func testRepoVersion(t *testing.T) {
105 +func testRepoVersion(t *testing.T, fakeIpfs string) {
106 badDir := "~somesuer/foo"
107 _, err := RepoVersion(badDir)
108 if err == nil {
repo/fsrepo/migrations/migrations_test.go
+4 -10
@@ -33,9 +33,7 @@ func TestFindMigrations(t *testing.T) {
33 createFakeBin(i-1, i, tmpDir)
34 }
35
36 - origPath := os.Getenv("PATH")
37 - os.Setenv("PATH", tmpDir)
38 - defer os.Setenv("PATH", origPath)
36 + t.Setenv("PATH", tmpDir)
37
38 migs, bins, err = findMigrations(ctx, 0, 5)
39 if err != nil {
@@ -80,9 +78,7 @@ func TestFindMigrationsReverse(t *testing.T) {
78 createFakeBin(i-1, i, tmpDir)
79 }
80
83 - origPath := os.Getenv("PATH")
84 - os.Setenv("PATH", tmpDir)
85 - defer os.Setenv("PATH", origPath)
81 + t.Setenv("PATH", tmpDir)
82
83 migs, bins, err = findMigrations(ctx, 5, 0)
84 if err != nil {
@@ -144,10 +140,8 @@ func TestFetchMigrations(t *testing.T) {
140 }
141
142 func TestRunMigrations(t *testing.T) {
147 - fakeHome := t.TempDir()
148 -
149 - os.Setenv("HOME", fakeHome)
150 - fakeIpfs := filepath.Join(fakeHome, ".ipfs")
143 + fakeIpfs := filepath.Join(t.TempDir(), ".ipfs")
144 + t.Setenv(config.EnvDir, fakeIpfs)
145
146 err := os.Mkdir(fakeIpfs, os.ModePerm)
147 if err != nil {