Giter Club home page Giter Club logo

configdir's Introduction

ConfigDir for Go and portable feature for Supersonic

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

For a portable Supersonic (if the env are not found, it will fallback to the original one):

System-wide Configuration
Windows %SUPERSONIC_PROGRAMDATA_DIR% or Custom relative path
User-level Configuration
Windows %SUPERSONIC_APPDATA_DIR% or Custom relative path
User-level Cache Folder
Windows %SUPERSONIC_LOCALAPPDATA%_DIR or Custom relative path

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

configdir's People

Contributors

makazzz avatar 20after4 avatar kirsle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.