-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from skycoin/feature/config-directory
Added multiple paths for config.
- Loading branch information
Showing
3 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pathutil | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// pathutil errors | ||
var ( | ||
ErrDirectoryNotFound = fmt.Errorf( | ||
"directory not found in any of the following paths: %s", strings.Join(paths(), ", ")) | ||
) | ||
|
||
// paths return the paths in which skywire looks for data directories | ||
func paths() []string { | ||
// if we are unable to find the local directory try next option | ||
localDir, _ := os.Getwd() // nolint: errcheck | ||
|
||
return []string{ | ||
localDir, | ||
filepath.Join(HomeDir(), ".skycoin/skywire"), | ||
"/usr/local/skycoin/skywire", | ||
} | ||
} | ||
|
||
// Find tries to find given directory or file in: | ||
// 1) local app directory | ||
// 2) ${HOME}/.skycoin/skywire/{directory} | ||
// 3) /usr/local/skycoin/skywire/{directory} | ||
// It will return the first path, including given directory if found, by preference. | ||
func Find(name string) (string, error) { | ||
for _, path := range paths() { | ||
_, err := os.Stat(filepath.Join(path, name)) | ||
if err == nil { | ||
return filepath.Join(path, name), nil | ||
} | ||
} | ||
|
||
return "", ErrDirectoryNotFound | ||
} |