mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
repo: use config api to get node root path (#10934)
Some checks failed
CodeQL / codeql (push) Has been cancelled
Docker Check / lint (push) Has been cancelled
Docker Check / build (push) Has been cancelled
Gateway Conformance / gateway-conformance (push) Has been cancelled
Gateway Conformance / gateway-conformance-libp2p-experiment (push) Has been cancelled
Go Build / go-build (push) Has been cancelled
Go Check / go-check (push) Has been cancelled
Go Lint / go-lint (push) Has been cancelled
Go Test / go-test (push) Has been cancelled
Interop / interop-prep (push) Has been cancelled
Sharness / sharness-test (push) Has been cancelled
Spell Check / spellcheck (push) Has been cancelled
Interop / helia-interop (push) Has been cancelled
Interop / ipfs-webui (push) Has been cancelled
Some checks failed
CodeQL / codeql (push) Has been cancelled
Docker Check / lint (push) Has been cancelled
Docker Check / build (push) Has been cancelled
Gateway Conformance / gateway-conformance (push) Has been cancelled
Gateway Conformance / gateway-conformance-libp2p-experiment (push) Has been cancelled
Go Build / go-build (push) Has been cancelled
Go Check / go-check (push) Has been cancelled
Go Lint / go-lint (push) Has been cancelled
Go Test / go-test (push) Has been cancelled
Interop / interop-prep (push) Has been cancelled
Sharness / sharness-test (push) Has been cancelled
Spell Check / spellcheck (push) Has been cancelled
Interop / helia-interop (push) Has been cancelled
Interop / ipfs-webui (push) Has been cancelled
Replaces #8964 Closes #8848
This commit is contained in:
parent
1905aef24b
commit
17b0085fdd
@ -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()
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user