go4.org: go4.org/xdgdir Index | Examples | Files

package xdgdir

import "go4.org/xdgdir"

Package xdgdir implements the Free Desktop Base Directory specification for locating directories.

The specification is at http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html

Code:

// Print the absolute path of the current user's XDG_CONFIG_DIR.
fmt.Println(xdgdir.Config.Path())

// Read a file from $XDG_CONFIG_DIR/myconfig.json.
// This will search for a file named "myconfig.json" inside
// $XDG_CONFIG_DIR and then each entry inside $XDG_CONFIG_DIRS.
// It opens and returns the first file it finds, or returns an error.
if f, err := xdgdir.Data.Create("myconfig.json"); err == nil {
    fmt.Fprintln(f, "Hello, World!")
    if err := f.Close(); err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
} else {
    fmt.Fprintln(os.Stderr, err)
}

// Write a file to $XDG_DATA_DIR/myapp/foo.txt
if f, err := xdgdir.Data.Create("myapp/foo.txt"); err == nil {
    fmt.Fprintln(f, "Hello, World!")
    f.Close()
} else {
    fmt.Fprintln(os.Stderr, err)
}

Index

Examples

Package Files

xdgdir.go

type Dir Uses

type Dir struct {
    // contains filtered or unexported fields
}

A Dir is a logical base directory along with additional search directories.

var (
    Data    Dir
    Config  Dir
    Cache   Dir
    Runtime Dir
)

Directories defined by the specification.

func (Dir) Create Uses

func (d Dir) Create(name string) (*os.File, error)

Create creates the named file inside the directory mode 0666 (before umask), truncating it if it already exists. Parent directories of the file will be created with mode 0700.

func (Dir) Open Uses

func (d Dir) Open(name string) (*os.File, error)

Open opens the named file inside the directory for reading. If the directory has multiple search paths, each path is checked in order for the file and the first one found is opened.

func (Dir) Path Uses

func (d Dir) Path() string

Path returns the absolute path of the primary directory, or an empty string if there's no suitable directory present. This is the path that should be used for writing files.

func (Dir) SearchPaths Uses

func (d Dir) SearchPaths() []string

SearchPaths returns the list of paths (in descending order of preference) to search for files.

func (Dir) String Uses

func (d Dir) String() string

String returns the name of the primary environment variable for the directory.

Package xdgdir imports 6 packages (graph) and is imported by 1 packages. Updated 2018-12-03. Refresh now. Tools for package owners.