Skip to content

Commit 17b0085

Browse files
authored
repo: use config api to get node root path (#10934)
Replaces #8964 Closes #8848
1 parent 1905aef commit 17b0085

File tree

6 files changed

+58
-78
lines changed

6 files changed

+58
-78
lines changed

cmd/ipfswatch/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"syscall"
1414

1515
commands "github.com/ipfs/kubo/commands"
16+
"github.com/ipfs/kubo/config"
1617
core "github.com/ipfs/kubo/core"
1718
coreapi "github.com/ipfs/kubo/core/coreapi"
1819
corehttp "github.com/ipfs/kubo/core/corehttp"
@@ -25,10 +26,18 @@ import (
2526

2627
var (
2728
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
2930
watchPath = flag.String("path", ".", "the path to watch")
3031
)
3132

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+
3241
func main() {
3342
flag.Parse()
3443

core/commands/sysdiag.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package commands
22

33
import (
44
"os"
5-
"path"
65
"runtime"
76

7+
"github.com/ipfs/go-ipfs-cmds"
88
version "github.com/ipfs/kubo"
9+
"github.com/ipfs/kubo/config"
910
"github.com/ipfs/kubo/core"
1011
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
11-
12-
cmds "github.com/ipfs/go-ipfs-cmds"
1312
manet "github.com/multiformats/go-multiaddr/net"
1413
sysi "github.com/whyrusleeping/go-sysinfo"
1514
)
@@ -84,32 +83,28 @@ func runtimeInfo(out map[string]interface{}) error {
8483
func envVarInfo(out map[string]interface{}) error {
8584
ev := make(map[string]interface{})
8685
ev["GOPATH"] = os.Getenv("GOPATH")
87-
ev["IPFS_PATH"] = os.Getenv("IPFS_PATH")
86+
ev[config.EnvDir] = os.Getenv(config.EnvDir)
8887

8988
out["environment"] = ev
9089
return nil
9190
}
9291

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-
10192
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)
10498
if err != nil {
10599
return err
106100
}
107101

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+
}
111107

112-
out["diskinfo"] = di
113108
return nil
114109
}
115110

repo/fsrepo/migrations/fetch_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ func TestGetDistPath(t *testing.T) {
2020
}
2121

2222
testDist := "/unit/test/dist"
23-
err := os.Setenv(envIpfsDistPath, testDist)
24-
if err != nil {
25-
panic(err)
26-
}
23+
t.Setenv(envIpfsDistPath, testDist)
2724
defer func() {
2825
os.Unsetenv(envIpfsDistPath)
2926
}()
@@ -139,18 +136,12 @@ func TestFetchBinary(t *testing.T) {
139136
if err != nil {
140137
panic(err)
141138
}
142-
err = os.Setenv("TMPDIR", tmpDir)
143-
if err != nil {
144-
panic(err)
145-
}
139+
t.Setenv("TMPDIR", tmpDir)
146140
_, err = FetchBinary(ctx, fetcher, "go-ipfs", "v1.0.0", "ipfs", tmpDir)
147141
if !os.IsPermission(err) {
148142
t.Error("expected 'permission' error, got:", err)
149143
}
150-
err = os.Setenv("TMPDIR", "/tmp")
151-
if err != nil {
152-
panic(err)
153-
}
144+
t.Setenv("TMPDIR", "/tmp")
154145
err = os.Chmod(tmpDir, 0o755)
155146
if err != nil {
156147
panic(err)

repo/fsrepo/migrations/ipfsdir.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88
"strconv"
99
"strings"
1010

11+
"github.com/ipfs/kubo/config"
1112
"github.com/ipfs/kubo/misc/fsutil"
1213
)
1314

1415
const (
15-
envIpfsPath = "IPFS_PATH"
16-
defIpfsDir = ".ipfs"
1716
versionFile = "version"
1817
)
1918

@@ -24,25 +23,16 @@ const (
2423
func IpfsDir(dir string) (string, error) {
2524
var err error
2625
if dir == "" {
27-
dir = os.Getenv(envIpfsPath)
28-
}
29-
if dir != "" {
30-
dir, err = fsutil.ExpandHome(dir)
26+
dir, err = config.PathRoot()
3127
if err != nil {
3228
return "", err
3329
}
34-
return dir, nil
3530
}
36-
37-
home, err := os.UserHomeDir()
31+
dir, err = fsutil.ExpandHome(dir)
3832
if err != nil {
3933
return "", err
4034
}
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
4636
}
4737

4838
// CheckIpfsDir gets the ipfs directory and checks that the directory exists.

repo/fsrepo/migrations/ipfsdir_test.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ import (
44
"os"
55
"path/filepath"
66
"testing"
7-
)
87

9-
var (
10-
fakeHome string
11-
fakeIpfs string
8+
"github.com/ipfs/kubo/config"
129
)
1310

1411
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+
})
2226
}
2327

24-
func testIpfsDir(t *testing.T) {
28+
func testIpfsDir(t *testing.T, fakeIpfs string) {
2529
_, err := CheckIpfsDir("")
2630
if err == nil {
2731
t.Fatal("expected error when no .ipfs directory to find")
@@ -37,32 +41,29 @@ func testIpfsDir(t *testing.T) {
3741
t.Fatal(err)
3842
}
3943
if dir != fakeIpfs {
40-
t.Fatal("wrong ipfs directory:", dir)
44+
t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs)
4145
}
4246

43-
os.Setenv(envIpfsPath, "~/.ipfs")
47+
t.Setenv(config.EnvDir, "~/.ipfs")
4448
dir, err = IpfsDir("")
4549
if err != nil {
4650
t.Fatal(err)
4751
}
4852
if dir != fakeIpfs {
49-
t.Fatal("wrong ipfs directory:", dir)
53+
t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs)
5054
}
5155

5256
_, err = IpfsDir("~somesuer/foo")
5357
if err == nil {
5458
t.Fatal("expected error with user-specific home dir")
5559
}
5660

57-
err = os.Setenv(envIpfsPath, "~somesuer/foo")
58-
if err != nil {
59-
panic(err)
60-
}
61+
t.Setenv(config.EnvDir, "~somesuer/foo")
6162
_, err = IpfsDir("~somesuer/foo")
6263
if err == nil {
6364
t.Fatal("expected error with user-specific home dir")
6465
}
65-
err = os.Unsetenv(envIpfsPath)
66+
err = os.Unsetenv(config.EnvDir)
6667
if err != nil {
6768
panic(err)
6869
}
@@ -72,7 +73,7 @@ func testIpfsDir(t *testing.T) {
7273
t.Fatal(err)
7374
}
7475
if dir != fakeIpfs {
75-
t.Fatal("wrong ipfs directory:", dir)
76+
t.Fatalf("wrong ipfs directory: got %s, expected %s", dir, fakeIpfs)
7677
}
7778

7879
_, err = IpfsDir("")
@@ -81,7 +82,7 @@ func testIpfsDir(t *testing.T) {
8182
}
8283
}
8384

84-
func testCheckIpfsDir(t *testing.T) {
85+
func testCheckIpfsDir(t *testing.T, fakeIpfs string) {
8586
_, err := CheckIpfsDir("~somesuer/foo")
8687
if err == nil {
8788
t.Fatal("expected error with user-specific home dir")
@@ -101,7 +102,7 @@ func testCheckIpfsDir(t *testing.T) {
101102
}
102103
}
103104

104-
func testRepoVersion(t *testing.T) {
105+
func testRepoVersion(t *testing.T, fakeIpfs string) {
105106
badDir := "~somesuer/foo"
106107
_, err := RepoVersion(badDir)
107108
if err == nil {

repo/fsrepo/migrations/migrations_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ func TestFindMigrations(t *testing.T) {
3333
createFakeBin(i-1, i, tmpDir)
3434
}
3535

36-
origPath := os.Getenv("PATH")
37-
os.Setenv("PATH", tmpDir)
38-
defer os.Setenv("PATH", origPath)
36+
t.Setenv("PATH", tmpDir)
3937

4038
migs, bins, err = findMigrations(ctx, 0, 5)
4139
if err != nil {
@@ -80,9 +78,7 @@ func TestFindMigrationsReverse(t *testing.T) {
8078
createFakeBin(i-1, i, tmpDir)
8179
}
8280

83-
origPath := os.Getenv("PATH")
84-
os.Setenv("PATH", tmpDir)
85-
defer os.Setenv("PATH", origPath)
81+
t.Setenv("PATH", tmpDir)
8682

8783
migs, bins, err = findMigrations(ctx, 5, 0)
8884
if err != nil {
@@ -144,10 +140,8 @@ func TestFetchMigrations(t *testing.T) {
144140
}
145141

146142
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)
151145

152146
err := os.Mkdir(fakeIpfs, os.ModePerm)
153147
if err != nil {

0 commit comments

Comments
 (0)