xdgdir

package
v0.0.0-...-2148625 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

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

Example
package main

import (
	"fmt"
	"os"

	"go4.org/xdgdir"
)

func main() {
	// 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)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dir

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

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

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

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

func (d Dir) SearchPaths() []string

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

func (Dir) String

func (d Dir) String() string

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL