-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d964b5
commit 6c5d6df
Showing
2 changed files
with
64 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package folderutil | ||
|
||
import ( | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
) | ||
|
||
// Below Contains utils for standard directories | ||
// which should be used by tools to store data | ||
// and configuration files respectively | ||
|
||
// HomeDirOrDefault tries to obtain the user's home directory and | ||
// returns the default if it cannot be obtained. | ||
func HomeDirOrDefault(defaultDirectory string) string { | ||
if homeDir, err := os.UserHomeDir(); err == nil && IsWritable(homeDir) { | ||
return homeDir | ||
} | ||
if user, err := user.Current(); err == nil && IsWritable(user.HomeDir) { | ||
return user.HomeDir | ||
} | ||
return defaultDirectory | ||
} | ||
|
||
// UserConfigDirOrDefault returns the user config directory or defaultConfigDir in case of error | ||
func UserConfigDirOrDefault(defaultConfigDir string) string { | ||
userConfigDir, err := os.UserConfigDir() | ||
if err != nil { | ||
return defaultConfigDir | ||
} | ||
return userConfigDir | ||
} | ||
|
||
// AppConfigDirOrDefault returns the app config directory | ||
func AppConfigDirOrDefault(defaultAppConfigDir string, toolName string) string { | ||
userConfigDir := UserConfigDirOrDefault("") | ||
if userConfigDir == "" { | ||
return filepath.Join(defaultAppConfigDir, toolName) | ||
} | ||
return filepath.Join(userConfigDir, toolName) | ||
} | ||
|
||
// AppCacheDirOrDefault returns the user cache directory or defaultCacheDir in case of error | ||
func AppCacheDirOrDefault(defaultCacheDir string, toolName string) string { | ||
userCacheDir, err := os.UserCacheDir() | ||
if err != nil || userCacheDir == "" { | ||
return filepath.Join(defaultCacheDir, toolName) | ||
} | ||
return filepath.Join(userCacheDir, toolName) | ||
} |