Skip to content

Instantly share code, notes, and snippets.

@SlyDen
Created February 28, 2017 16:24
Show Gist options
  • Save SlyDen/ba9a6960a7cae6e0495c4600f98bc765 to your computer and use it in GitHub Desktop.
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/
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