Categorygithub.com/kirsle/configdir
modulepackage
0.0.0-20170128060238-e45d2f54772f
Repository: https://github.com/kirsle/configdir.git
Documentation: pkg.go.dev

# README

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

// A common use case is to get a private config folder for your app to
// place its settings files into, that are specific to the local user.
configPath := configdir.LocalConfig("my-app")
err := configdir.MakePath(configPath) // Ensure it exists.
if err != nil {
    panic(err)
}

// Deal with a JSON configuration file in that folder.
configFile := filepath.Join(configPath, "settings.json")
type AppSettings struct {
    Username string `json:"username"`
    Password string `json:"password"`
}
var settings AppSettings

// Does the file not exist?
if _, err = os.Stat(configFile); os.IsNotExist(err) {
    // Create the new config file.
    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 {
    // Load the existing file.
    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

# Functions

LocalCache returns the local user cache folder, with optional path components added to the end for vendor/application-specific settings.
LocalConfig returns the local user configuration path, with optional path components added to the end for vendor/application-specific settings.
MakePath ensures that the full path you wanted, including vendor or application-specific components, exists.
Refresh will rediscover the config paths, checking current environment variables again.
SystemConfig returns the system-wide configuration paths, with optional path components added to the end for vendor/application-specific settings.

# Constants

VERSION is the semantic version number of the configdir library.

# Variables

DefaultFileMode controls the default permissions on any paths created by using MakePath.