ConfigDir for Go
This library provides a cross platform means of detecting the system's
configuration directories so that your Go app can store config files in a
standard location. For Linux and other Unixes (BSD, etc.) this means using the
Freedesktop.org XDG Base Directory Specification (0.8), and for Windows
and macOS it uses their standard directories.
This is a simple no-nonsense module that just gives you the path names to do
with as you please. You can either get the bare root config path, or get a
path with any number of names suffixed onto it for vendor- or
application-specific namespacing.
For the impatient, the directories this library can return tend to be like
the following:
| System-wide Configuration |
---|
Windows | %PROGRAMDATA% or C:\ProgramData |
Linux | $XDG_CONFIG_DIRS or /etc/xdg |
macOS | /Library/Application Support |
| User-level Configuration |
Windows | %APPDATA% or C:\Users\%USER%\AppData\Roaming |
Linux | $XDG_CONFIG_HOME or $HOME/.config |
macOS | $HOME/Library/Application Support |
| User-level Cache Folder |
Windows | %LOCALAPPDATA% or C:\Users\%USER%\AppData\Local |
Linux | $XDG_CACHE_HOME or $HOME/.cache |
macOS | $HOME/Library/Caches |
Quick Start
configPath := configdir.LocalConfig("my-app")
err := configdir.MakePath(configPath)
if err != nil {
panic(err)
}
configFile := filepath.Join(configPath, "settings.json")
type AppSettings struct {
Username string `json:"username"`
Password string `json:"password"`
}
var settings AppSettings
if _, err = os.Stat(configFile); os.IsNotExist(err) {
settings = AppSettings{"MyUser", "MyPassword"}
fh, err := os.Create(configFile)
if err != nil {
panic(err)
}
defer fh.Close()
encoder := json.NewEncoder(fh)
encoder.Encode(&settings)
} else {
fh, err := os.Open(configFile)
if err != nil {
panic(err)
}
defer fh.Close()
decoder := json.NewDecoder(fh)
decoder.Decode(&settings)
}
Documentation
Package documentation is available at
https://godoc.org/github.com/kirsle/configdir
Author
Noah Petherbridge, @kirsle
License
MIT