diff --git a/assigner/config/config.go b/assigner/config/config.go index 3a9bc12be..1ff91896d 100644 --- a/assigner/config/config.go +++ b/assigner/config/config.go @@ -44,17 +44,25 @@ func Marshal(value interface{}) ([]byte, error) { // empty string is provided for `configRoot`, the default root is used. If // configFile is an absolute path, then configRoot is ignored. func Path(configRoot, configFile string) (string, error) { + var err error if configFile == "" { configFile = DefaultConfigFile - } else if filepath.IsAbs(configFile) { - return filepath.Clean(configFile), nil - } - if configRoot == "" { - var err error - configRoot, err = PathRoot() + } else { + configFile, err = fsutil.ExpandHome(configFile) if err != nil { return "", err } + if filepath.IsAbs(configFile) { + return filepath.Clean(configFile), nil + } + } + if configRoot == "" { + configRoot, err = PathRoot() + } else { + configRoot, err = fsutil.ExpandHome(configRoot) + } + if err != nil { + return "", err } return filepath.Join(configRoot, configFile), nil } @@ -62,10 +70,10 @@ func Path(configRoot, configFile string) (string, error) { // PathRoot returns the default configuration root directory. func PathRoot() (string, error) { dir := os.Getenv(EnvDir) - if dir != "" { - return dir, nil + if dir == "" { + dir = DefaultPathRoot } - return fsutil.ExpandHome(DefaultPathRoot) + return fsutil.ExpandHome(dir) } // Load reads the json-serialized config at the specified path. @@ -73,9 +81,11 @@ func Load(filePath string) (*Config, error) { var err error if filePath == "" { filePath, err = Path("", "") - if err != nil { - return nil, err - } + } else { + filePath, err = fsutil.ExpandHome(filePath) + } + if err != nil { + return nil, err } f, err := os.Open(filePath) @@ -128,9 +138,11 @@ func (c *Config) Save(filePath string) error { var err error if filePath == "" { filePath, err = Path("", "") - if err != nil { - return err - } + } else { + filePath, err = fsutil.ExpandHome(filePath) + } + if err != nil { + return err } err = os.MkdirAll(filepath.Dir(filePath), 0755) diff --git a/assigner/server/server_test.go b/assigner/server/server_test.go index 34b14421b..81caeb0c9 100644 --- a/assigner/server/server_test.go +++ b/assigner/server/server_test.go @@ -85,7 +85,8 @@ func TestAssignOnAnnounce(t *testing.T) { require.NoError(t, err) indexerID := stiCfg.Identity.PeerID stiCfg.Indexer.FreezeAtPercent = 99.0 - stiCfg.Save(stiCfgPath) + err = stiCfg.Save(stiCfgPath) + require.NoError(t, err) t.Log("Initialized indexer", indexerID) indexerReady := testcmd.NewStdoutWatcher(indexerReadyMatch) diff --git a/config/config.go b/config/config.go index 00a49c588..a710797d4 100644 --- a/config/config.go +++ b/config/config.go @@ -53,17 +53,25 @@ func Marshal(value interface{}) ([]byte, error) { // empty string is provided for `configRoot`, the default root is used. If // configFile is an absolute path, then configRoot is ignored. func Path(configRoot, configFile string) (string, error) { + var err error if configFile == "" { configFile = DefaultConfigFile - } else if filepath.IsAbs(configFile) { - return filepath.Clean(configFile), nil - } - if configRoot == "" { - var err error - configRoot, err = PathRoot() + } else { + configFile, err = fsutil.ExpandHome(configFile) if err != nil { return "", err } + if filepath.IsAbs(configFile) { + return filepath.Clean(configFile), nil + } + } + if configRoot == "" { + configRoot, err = PathRoot() + } else { + configRoot, err = fsutil.ExpandHome(configRoot) + } + if err != nil { + return "", err } return filepath.Join(configRoot, configFile), nil } @@ -71,10 +79,10 @@ func Path(configRoot, configFile string) (string, error) { // PathRoot returns the default configuration root directory. func PathRoot() (string, error) { dir := os.Getenv(EnvDir) - if dir != "" { - return dir, nil + if dir == "" { + dir = DefaultPathRoot } - return fsutil.ExpandHome(DefaultPathRoot) + return fsutil.ExpandHome(dir) } // Load reads the json-serialized config at the specified path. @@ -82,9 +90,11 @@ func Load(filePath string) (*Config, error) { var err error if filePath == "" { filePath, err = Path("", "") - if err != nil { - return nil, err - } + } else { + filePath, err = fsutil.ExpandHome(filePath) + } + if err != nil { + return nil, err } f, err := os.Open(filePath) @@ -141,9 +151,11 @@ func (c *Config) Save(filePath string) error { var err error if filePath == "" { filePath, err = Path("", "") - if err != nil { - return err - } + } else { + filePath, err = fsutil.ExpandHome(filePath) + } + if err != nil { + return err } err = os.MkdirAll(filepath.Dir(filePath), 0755) diff --git a/e2e_test.go b/e2e_test.go index 71cec1691..e396ee788 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -150,7 +150,8 @@ func testEndToEndWithReferenceProvider(t *testing.T, publisherProto string) { }, } rdMirrorDir := rnr.Dir - cfg.Save(stiCfgPath) + err = cfg.Save(stiCfgPath) + require.NoError(t, err) // start provider providerReady := testcmd.NewStderrWatcher(providerReadyMatch) @@ -264,7 +265,8 @@ func testEndToEndWithReferenceProvider(t *testing.T, publisherProto string) { }, }, } - cfg.Save(sti2CfgPath) + err = cfg.Save(sti2CfgPath) + require.NoError(t, err) wrMirrorDir := rnr.Dir indexerReady2 := testcmd.NewStdoutWatcher(indexerReadyMatch) diff --git a/gc/reaper/option.go b/gc/reaper/option.go index c9be62587..e126ee5e0 100644 --- a/gc/reaper/option.go +++ b/gc/reaper/option.go @@ -7,6 +7,7 @@ import ( "github.com/ipni/go-libipni/pcache" "github.com/ipni/storetheindex/carstore" + "github.com/ipni/storetheindex/fsutil" "github.com/libp2p/go-libp2p/core/host" ) @@ -89,6 +90,11 @@ func WithCarDelete(del bool) Option { // provider-specific datastores. func WithDatastoreDir(dir string) Option { return func(c *config) error { + var err error + dir, err = fsutil.ExpandHome(dir) + if err != nil { + return err + } c.dstoreDir = dir return nil } @@ -98,6 +104,11 @@ func WithDatastoreDir(dir string) Option { // provider-specific temproary datastores. func WithDatastoreTempDir(dir string) Option { return func(c *config) error { + var err error + dir, err = fsutil.ExpandHome(dir) + if err != nil { + return err + } c.dstoreTmpDir = dir return nil }