Created
February 28, 2017 16:24
-
-
Save SlyDen/ba9a6960a7cae6e0495c4600f98bc765 to your computer and use it in GitHub Desktop.
Most commonly-used modern Linux distributions use the XDG Base Directory Specification http://benaiah.me/posts/configuring-go-apps-with-toml/
This file contains hidden or 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
import ( | |
"path/filepath" | |
"os" | |
"runtime" | |
"github.com/mitchellh/go-homedir" | |
) | |
var configDirName = "example" | |
func GetDefaultConfigDir() (string, error) { | |
var configDirLocation string | |
homeDir, err := homedir.Dir() | |
if err != nil { | |
return "", err | |
} | |
switch runtime.GOOS { | |
case "linux": | |
// Use the XDG_CONFIG_HOME variable if it is set, otherwise | |
// $HOME/.config/example | |
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME") | |
if xdgConfigHome != "" { | |
configDirLocation = xdgConfigHome | |
} else { | |
configDirLocation = filepath.Join(homeDir, ".config", configDirName) | |
} | |
default: | |
// On other platforms we just use $HOME/.example | |
hiddenConfigDirName := "." + configDirName | |
configDirLocation = filepath.Join(homeDir, hiddenConfigDirName) | |
} | |
return configDirLocation, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment