Skip to content

Commit

Permalink
Do home directory expansion for config paths
Browse files Browse the repository at this point in the history
Expand leading ~ in config paths to user's home directory.
  • Loading branch information
gammazero committed Oct 12, 2024
1 parent ee8dd34 commit 491db83
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 33 deletions.
42 changes: 27 additions & 15 deletions assigner/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,48 @@ 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
}

// 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.
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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion assigner/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 27 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,48 @@ 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
}

// 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.
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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions gc/reaper/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 491db83

Please sign in to comment.