Popularity
1.2
-
Activity
3.9
-
12
1
2

Description

Provides an implementation of the XDG Base Directory Specification. The specification defines a set of standard paths for storing application files including data and configuration files. For portability and flexibility reasons, applications should use the XDG defined locations instead of hardcoding paths.

Full documentation can be found at: https://godoc.org/github.com/adrg/xdg

Programming language: Go
Tags: Go Tools     Go     Package     Golang     Library     XDG     XDG Base Directory     Specification    
Latest version: v1.0.0

xdg-go alternatives and similar packages

Based on the "Go Tools" category

Do you think we are missing an alternative of xdg-go or a related project?

Add another 'Go Tools' Package

README

xdg

GoDoc License: MIT Go Report Card

Provides an implementation of the XDG Base Directory Specification. The specification defines a set of standard paths for storing application files including data and configuration files. For portability and flexibility reasons, applications should use the XDG defined locations instead of hardcoding paths.

Full documentation can be found at: https://godoc.org/github.com/adrg/xdg

Installation

go get github.com/adrg/xdg

Usage

package main

import (
    "fmt"
    "os"

    "github.com/adrg/xdg"
)

func main() {
    // XDG directories
    fmt.Println(xdg.DataHome)
    fmt.Println(xdg.DataDirs)
    fmt.Println(xdg.ConfigHome)
    fmt.Println(xdg.ConfigDirs)
    fmt.Println(xdg.CacheHome)
    fmt.Println(xdg.RuntimeDir)

    // Finding a config file
    // The function takes one parameter which should contain the config
    // filename but it can also contain a set of directories relative to the
    // config search paths (xdg.ConfigHome and xdg.ConfigDirs).
    // Finding data, cache or runtime files is done in a similar manner using
    // xdg.SearchDataFile(), xdg.SearchCacheFile() and xdg.SearchRuntimeFile().
    configFilePath, err := xdg.SearchConfigFile("appname/config.yaml")
    if err != nil {
        // the config file was not found in the search paths
        // treat error
    } else {
        fmt.Println(configFilePath)
    }

    // Opening a config file
    // The function takes one parameter which should contain the config
    // filename but it can also contain a set of directories relative to the
    // config search paths. If the directories don't exists, they will be
    // created.
    // Opening data, cache or runtime files is done in a similar manner using
    // xdg.DataFile(), xdg.CacheFile() and xdg.RuntimeFile().
    configFilePath, err = xdg.ConfigFile("appname/config.yaml")
    if err != nil {
        // could not create path for the config file in any of the search paths
        // treat error
    } else {
        fp, err := os.Create(configFilePath)
        if err != nil {
            // could not open the file for some reason
            // treat error
        } else {
            // read/write the config file
        }
    }
}

References

For more information see the XDG Base Directory Specification

Contributing

Contributions in the form of pull requests, issues or just general feedback, are always welcome. See CONTRIBUTING.MD.

License

Copyright (c) 2014 Adrian-George Bostan.

This project is licensed under the MIT license. See LICENSE for more details.


*Note that all licence references and agreements mentioned in the xdg-go README section above are relevant to that project's source code only.