-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
179 additions
and
12 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,46 @@ | ||
package basedir | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
// GetXDGDirectory returns either a string or a slice of strings depending on the directory type. | ||
func GetXDGDirectory(dirType string) interface{} { | ||
switch dirType { | ||
case "data": | ||
return getEnvOrDefault("XDG_DATA_HOME", os.Getenv("HOME")+"/.local/share") | ||
case "config": | ||
return getEnvOrDefault("XDG_CONFIG_HOME", os.Getenv("HOME")+"/.config") | ||
case "state": | ||
return getEnvOrDefault("XDG_STATE_HOME", os.Getenv("HOME")+"/.local/state") | ||
case "cache": | ||
return getEnvOrDefault("XDG_CACHE_HOME", os.Getenv("HOME")+"/.cache") | ||
case "runtime": | ||
return getEnvOrDefault("XDG_RUNTIME_DIR", "") | ||
case "dataDirs": | ||
return getEnvOrDefaultList("XDG_DATA_DIRS", "/usr/local/share:/usr/share") | ||
case "configDirs": | ||
return getEnvOrDefaultList("XDG_CONFIG_DIRS", "/etc/xdg") | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
// getEnvOrDefault returns the value of an environment variable or a default if not set or empty. | ||
func getEnvOrDefault(envVar, defaultValue string) string { | ||
value := os.Getenv(envVar) | ||
if value == "" { | ||
return defaultValue | ||
} | ||
return value | ||
} | ||
|
||
// getEnvOrDefaultList returns a slice of strings by splitting an environment variable or using a default. | ||
func getEnvOrDefaultList(envVar, defaultValue string) []string { | ||
value := os.Getenv(envVar) | ||
if value == "" { | ||
value = defaultValue | ||
} | ||
return strings.Split(value, ":") | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= | ||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= | ||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= | ||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= |
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