Giter Club home page Giter Club logo

configdir's Introduction

configdir for Golang

Multi platform library of configuration directory for Golang.

This library helps to get regular directories for configuration files or cache files that matches target operationg system's convention.

It assumes the following folders are standard paths of each environment:

ย  Windows: Linux/BSDs: MacOSX:
System level configuration folder %PROGRAMDATA% (C:\\ProgramData) ${XDG_CONFIG_DIRS} (/etc/xdg) /Library/Application Support
User level configuration folder %APPDATA% (C:\\Users\\<User>\\AppData\\Roaming) ${XDG_CONFIG_HOME} (${HOME}/.config) ${HOME}/Library/Application Support
User wide cache folder %LOCALAPPDATA% (C:\\Users\\<User>\\AppData\\Local) ${XDG_CACHE_HOME} (${HOME}/.cache) ${HOME}/Library/Caches

Examples

Getting Configuration

configdir.ConfigDir.QueryFolderContainsFile() searches files in the following order:

  • Local path (if you add the path via LocalPath parameter)
  • User level configuration folder(e.g. $HOME/.config/<vendor-name>/<application-name>/setting.json in Linux)
  • System level configuration folder(e.g. /etc/xdg/<vendor-name>/<application-name>/setting.json in Linux)

configdir.Config provides some convenient methods(ReadFile, WriteFile and so on).

var config Config

configDirs := configdir.New("vendor-name", "application-name")
// optional: local path has the highest priority
configDirs.LocalPath, _ = filepath.Abs(".")
folder := configDirs.QueryFolderContainsFile("setting.json")
if folder != nil {
    data, _ := folder.ReadFile("setting.json")
    json.Unmarshal(data, &config)
} else {
    config = DefaultConfig
}

Write Configuration

When storing configuration, get configuration folder by using configdir.ConfigDir.QueryFolders() method.

configDirs := configdir.New("vendor-name", "application-name")

var config Config
data, _ := json.Marshal(&config)

// Stores to local folder
folders := configDirs.QueryFolders(configdir.Local)
folders[0].WriteFile("setting.json", data)

// Stores to user folder
folders = configDirs.QueryFolders(configdir.Global)
folders[0].WriteFile("setting.json", data)

// Stores to system folder
folders = configDirs.QueryFolders(configdir.System)
folders[0].WriteFile("setting.json", data)

Getting Cache Folder

It is similar to the above example, but returns cache folder.

configDirs := configdir.New("vendor-name", "application-name")
cache := configDirs.QueryCacheFolder()

resp, err := http.Get("http://examples.com/sdk.zip")
if err != nil {
    log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

cache.WriteFile("sdk.zip", body)

Document

https://godoc.org/github.com/shibukawa/configdir

License

MIT

configdir's People

Contributors

shibukawa avatar petemoore avatar emersion avatar

Watchers

James Cloos 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.