xdg

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: MIT Imports: 4 Imported by: 615

README

xdg logo

Go implementation of the XDG Base Directory Specification and XDG user directories.

Build status Code coverage pkg.go.dev documentation MIT license
Go report card Awesome Go GitHub contributors GitHub open issues Buy me a coffee

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. The package also includes the locations of well known user directories, as well as other common directories such as fonts and applications.

The current implementation supports most flavors of Unix, Windows, macOS and Plan 9.
On Windows, where XDG environment variables are not usually set, the package uses Known Folders as defaults. Therefore, appropriate locations are used for common folders which may have been redirected.

See usage examples below. Full documentation can be found at https://pkg.go.dev/github.com/adrg/xdg.

Installation

go get github.com/adrg/xdg

Default locations

The package defines sensible defaults for XDG variables which are empty or not present in the environment.

  • On Unix-like operating systems, XDG environment variables are tipically defined. Appropriate default locations are used for the environment variables which are not set.
  • On Windows, XDG environment variables are usually not set. If that is the case, the package relies on the appropriate Known Folders. Sensible fallback locations are used for the folders which are not set.

XDG Base Directory

Unix-like operating systems

Unix

macOS

Plan 9

XDG_DATA_HOME ~/.local/share ~/Library/Application Support $home/lib
XDG_DATA_DIRS /usr/local/share
/usr/share
/Library/Application Support /lib
XDG_CONFIG_HOME ~/.config ~/Library/Application Support $home/lib
XDG_CONFIG_DIRS /etc/xdg ~/Library/Preferences
/Library/Application Support
/Library/Preferences
/lib
XDG_STATE_HOME ~/.local/state ~/Library/Application Support $home/lib/state
XDG_CACHE_HOME ~/.cache ~/Library/Caches $home/lib/cache
XDG_RUNTIME_DIR /run/user/UID ~/Library/Application Support /tmp
Microsoft Windows

Known Folder(s)

Fallback(s)

XDG_DATA_HOME LocalAppData %LOCALAPPDATA%
XDG_DATA_DIRS RoamingAppData
ProgramData
%APPADATA%
%ProgramData%
XDG_CONFIG_HOME LocalAppData %LOCALAPPDATA%
XDG_CONFIG_DIRS ProgramData
RoamingAppData
%ProgramData%
%APPDATA%
XDG_STATE_HOME LocalAppData %LOCALAPPDATA%
XDG_CACHE_HOME LocalAppData\cache %LOCALAPPDATA%\cache
XDG_RUNTIME_DIR LocalAppData %LOCALAPPDATA%

XDG user directories

Unix-like operating systems

Unix

macOS

Plan 9

XDG_DESKTOP_DIR ~/Desktop ~/Desktop $home/desktop
XDG_DOWNLOAD_DIR ~/Downloads ~/Downloads $home/downloads
XDG_DOCUMENTS_DIR ~/Documents ~/Documents $home/documents
XDG_MUSIC_DIR ~/Music ~/Music $home/music
XDG_PICTURES_DIR ~/Pictures ~/Pictures $home/pictures
XDG_VIDEOS_DIR ~/Videos ~/Movies $home/videos
XDG_TEMPLATES_DIR ~/Templates ~/Templates $home/templates
XDG_PUBLICSHARE_DIR ~/Public ~/Public $home/public
Microsoft Windows

Known Folder(s)

Fallback(s)

XDG_DESKTOP_DIR Desktop %USERPROFILE%\Desktop
XDG_DOWNLOAD_DIR Downloads %USERPROFILE%\Downloads
XDG_DOCUMENTS_DIR Documents %USERPROFILE%\Documents
XDG_MUSIC_DIR Music %USERPROFILE%\Music
XDG_PICTURES_DIR Pictures %USERPROFILE%\Pictures
XDG_VIDEOS_DIR Videos %USERPROFILE%\Videos
XDG_TEMPLATES_DIR Templates %APPDATA%\Microsoft\Windows\Templates
XDG_PUBLICSHARE_DIR Public %PUBLIC%

Other directories

Unix-like operating systems

Unix

macOS

Plan 9

Home $HOME $HOME $home
Applications $XDG_DATA_HOME/applications
~/.local/share/applications
/usr/local/share/applications
/usr/share/applications
$XDG_DATA_DIRS/applications
/Applications $home/bin
/bin
Fonts $XDG_DATA_HOME/fonts
~/.fonts
~/.local/share/fonts
/usr/local/share/fonts
/usr/share/fonts
$XDG_DATA_DIRS/fonts
~/Library/Fonts
/Library/Fonts
/System/Library/Fonts
/Network/Library/Fonts
$home/lib/font
/lib/font
Microsoft Windows

Known Folder(s)

Fallback(s)

Home Profile %USERPROFILE%
Applications Programs
CommonPrograms
%APPDATA%\Microsoft\Windows\Start Menu\Programs
%ProgramData%\Microsoft\Windows\Start Menu\Programs
Fonts Fonts
-
%SystemRoot%\Fonts
%LOCALAPPDATA%\Microsoft\Windows\Fonts

Usage

XDG Base Directory
package main

import (
	"log"

	"github.com/adrg/xdg"
)

func main() {
	// XDG Base Directory paths.
	log.Println("Home data directory:", xdg.DataHome)
	log.Println("Data directories:", xdg.DataDirs)
	log.Println("Home config directory:", xdg.ConfigHome)
	log.Println("Config directories:", xdg.ConfigDirs)
	log.Println("Home state directory:", xdg.StateHome)
	log.Println("Cache directory:", xdg.CacheHome)
	log.Println("Runtime directory:", xdg.RuntimeDir)

	// Other common directories.
	log.Println("Home directory:", xdg.Home)
	log.Println("Application directories:", xdg.ApplicationDirs)
	log.Println("Font directories:", xdg.FontDirs)

	// Obtain a suitable location for application config files.
	// ConfigFile takes one parameter which must contain the name of the file,
	// but it can also contain a set of parent directories. If the directories
	// don't exist, they will be created relative to the base config directory.
	configFilePath, err := xdg.ConfigFile("appname/config.yaml")
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Save the config file at:", configFilePath)

	// For other types of application files use:
	// xdg.DataFile()
	// xdg.StateFile()
	// xdg.CacheFile()
	// xdg.RuntimeFile()

	// Finding application config files.
	// SearchConfigFile takes one parameter which must contain the name of
	// the file, but it can also contain a set of parent directories relative
	// to the config search paths (xdg.ConfigHome and xdg.ConfigDirs).
	configFilePath, err = xdg.SearchConfigFile("appname/config.yaml")
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Config file was found at:", configFilePath)

	// For other types of application files use:
	// xdg.SearchDataFile()
	// xdg.SearchStateFile()
	// xdg.SearchCacheFile()
	// xdg.SearchRuntimeFile()
}
XDG user directories
package main

import (
	"log"

	"github.com/adrg/xdg"
)

func main() {
	// XDG user directories.
	log.Println("Desktop directory:", xdg.UserDirs.Desktop)
	log.Println("Download directory:", xdg.UserDirs.Download)
	log.Println("Documents directory:", xdg.UserDirs.Documents)
	log.Println("Music directory:", xdg.UserDirs.Music)
	log.Println("Pictures directory:", xdg.UserDirs.Pictures)
	log.Println("Videos directory:", xdg.UserDirs.Videos)
	log.Println("Templates directory:", xdg.UserDirs.Templates)
	log.Println("Public directory:", xdg.UserDirs.PublicShare)
}

Stargazers over time

Stargazers over time

Contributing

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

Contributors: adrg, wichert, bouncepaw, gabriel-vasile, KalleDK, nvkv, djdv.

References

For more information see:

License

Copyright (c) 2014 Adrian-George Bostan.

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

Documentation

Overview

Package xdg 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. The package also includes the locations of well known user directories.

The current implementation supports most flavors of Unix, Windows, Mac OS and Plan 9.

For more information regarding the XDG Base Directory Specification see:
https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

For more information regarding the XDG user directories see:
https://wiki.archlinux.org/index.php/XDG_user_directories

For more information regarding the Windows Known Folders see:
https://docs.microsoft.com/en-us/windows/win32/shell/known-folders

Usage

XDG Base Directory

package main

import (
	"log"

	"github.com/adrg/xdg"
)

func main() {
	// XDG Base Directory paths.
	log.Println("Home data directory:", xdg.DataHome)
	log.Println("Data directories:", xdg.DataDirs)
	log.Println("Home config directory:", xdg.ConfigHome)
	log.Println("Config directories:", xdg.ConfigDirs)
	log.Println("Home state directory:", xdg.StateHome)
	log.Println("Cache directory:", xdg.CacheHome)
	log.Println("Runtime directory:", xdg.RuntimeDir)

	// Other common directories.
	log.Println("Home directory:", xdg.Home)
	log.Println("Application directories:", xdg.ApplicationDirs)
	log.Println("Font directories:", xdg.FontDirs)

	// Obtain a suitable location for application config files.
	// ConfigFile takes one parameter which must contain the name of the file,
	// but it can also contain a set of parent directories. If the directories
	// don't exist, they will be created relative to the base config directory.
	configFilePath, err := xdg.ConfigFile("appname/config.yaml")
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Save the config file at:", configFilePath)

	// For other types of application files use:
	// xdg.DataFile()
	// xdg.StateFile()
	// xdg.CacheFile()
	// xdg.RuntimeFile()

	// Finding application config files.
	// SearchConfigFile takes one parameter which must contain the name of
	// the file, but it can also contain a set of parent directories relative
	// to the config search paths (xdg.ConfigHome and xdg.ConfigDirs).
	configFilePath, err = xdg.SearchConfigFile("appname/config.yaml")
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Config file was found at:", configFilePath)

	// For other types of application files use:
	// xdg.SearchDataFile()
	// xdg.SearchStateFile()
	// xdg.SearchCacheFile()
	// xdg.SearchRuntimeFile()
}

XDG user directories

package main

import (
	"log"

	"github.com/adrg/xdg"
)

func main() {
	// XDG user directories.
	log.Println("Desktop directory:", xdg.UserDirs.Desktop)
	log.Println("Download directory:", xdg.UserDirs.Download)
	log.Println("Documents directory:", xdg.UserDirs.Documents)
	log.Println("Music directory:", xdg.UserDirs.Music)
	log.Println("Pictures directory:", xdg.UserDirs.Pictures)
	log.Println("Videos directory:", xdg.UserDirs.Videos)
	log.Println("Templates directory:", xdg.UserDirs.Templates)
	log.Println("Public directory:", xdg.UserDirs.PublicShare)
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Home contains the path of the user's home directory.
	Home string

	// DataHome defines the base directory relative to which user-specific
	// data files should be stored. This directory is defined by the
	// $XDG_DATA_HOME environment variable. If the variable is not set,
	// a default equal to $HOME/.local/share should be used.
	DataHome string

	// DataDirs defines the preference-ordered set of base directories to
	// search for data files in addition to the DataHome base directory.
	// This set of directories is defined by the $XDG_DATA_DIRS environment
	// variable. If the variable is not set, the default directories
	// to be used are /usr/local/share and /usr/share, in that order. The
	// DataHome directory is considered more important than any of the
	// directories defined by DataDirs. Therefore, user data files should be
	// written relative to the DataHome directory, if possible.
	DataDirs []string

	// ConfigHome defines the base directory relative to which user-specific
	// configuration files should be written. This directory is defined by
	// the $XDG_CONFIG_HOME environment variable. If the variable is not
	// not set, a default equal to $HOME/.config should be used.
	ConfigHome string

	// ConfigDirs defines the preference-ordered set of base directories to
	// search for configuration files in addition to the ConfigHome base
	// directory. This set of directories is defined by the $XDG_CONFIG_DIRS
	// environment variable. If the variable is not set, a default equal
	// to /etc/xdg should be used. The ConfigHome directory is considered
	// more important than any of the directories defined by ConfigDirs.
	// Therefore, user config files should be written relative to the
	// ConfigHome directory, if possible.
	ConfigDirs []string

	// StateHome defines the base directory relative to which user-specific
	// state files should be stored. This directory is defined by the
	// $XDG_STATE_HOME environment variable. If the variable is not set,
	// a default equal to ~/.local/state should be used.
	StateHome string

	// CacheHome defines the base directory relative to which user-specific
	// non-essential (cached) data should be written. This directory is
	// defined by the $XDG_CACHE_HOME environment variable. If the variable
	// is not set, a default equal to $HOME/.cache should be used.
	CacheHome string

	// RuntimeDir defines the base directory relative to which user-specific
	// non-essential runtime files and other file objects (such as sockets,
	// named pipes, etc.) should be stored. This directory is defined by the
	// $XDG_RUNTIME_DIR environment variable. If the variable is not set,
	// applications should fall back to a replacement directory with similar
	// capabilities. Applications should use this directory for communication
	// and synchronization purposes and should not place larger files in it,
	// since it might reside in runtime memory and cannot necessarily be
	// swapped out to disk.
	RuntimeDir string

	// UserDirs defines the locations of well known user directories.
	UserDirs UserDirectories

	// FontDirs defines the common locations where font files are stored.
	FontDirs []string

	// ApplicationDirs defines the common locations of applications.
	ApplicationDirs []string
)

Functions

func CacheFile

func CacheFile(relPath string) (string, error)

CacheFile returns a suitable location for the specified cache file. The relPath parameter must contain the name of the cache file, and optionally, a set of parent directories (e.g. appname/app.cache). If the specified directories do not exist, they will be created relative to the base cache directory. On failure, an error containing the attempted paths is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	cacheFilePath, err := xdg.CacheFile("appname/app.cache")
	if err != nil {
		// Treat error.
	}

	fmt.Println("Save cache file at:", cacheFilePath)
}
Output:

func ConfigFile

func ConfigFile(relPath string) (string, error)

ConfigFile returns a suitable location for the specified config file. The relPath parameter must contain the name of the config file, and optionally, a set of parent directories (e.g. appname/app.yaml). If the specified directories do not exist, they will be created relative to the base config directory. On failure, an error containing the attempted paths is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	configFilePath, err := xdg.ConfigFile("appname/app.yaml")
	if err != nil {
		// Treat error.
	}

	fmt.Println("Save config file at:", configFilePath)
}
Output:

func DataFile

func DataFile(relPath string) (string, error)

DataFile returns a suitable location for the specified data file. The relPath parameter must contain the name of the data file, and optionally, a set of parent directories (e.g. appname/app.data). If the specified directories do not exist, they will be created relative to the base data directory. On failure, an error containing the attempted paths is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	dataFilePath, err := xdg.DataFile("appname/app.data")
	if err != nil {
		// Treat error.
	}

	fmt.Println("Save data file at:", dataFilePath)
}
Output:

func Reload added in v0.2.0

func Reload()

Reload refreshes base and user directories by reading the environment. Defaults are applied for XDG variables which are empty or not present in the environment.

func RuntimeFile

func RuntimeFile(relPath string) (string, error)

RuntimeFile returns a suitable location for the specified runtime file. The relPath parameter must contain the name of the runtime file, and optionally, a set of parent directories (e.g. appname/app.pid). If the specified directories do not exist, they will be created relative to the base runtime directory. On failure, an error containing the attempted paths is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	runtimeFilePath, err := xdg.RuntimeFile("appname/app.pid")
	if err != nil {
		// Treat error.
	}

	fmt.Println("Save runtime file at:", runtimeFilePath)
}
Output:

func SearchCacheFile

func SearchCacheFile(relPath string) (string, error)

SearchCacheFile searches for the specified file in the cache search path. The relPath parameter must contain the name of the cache file, and optionally, a set of parent directories (e.g. appname/app.cache). If the file cannot be found, an error specifying the searched path is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	cacheFilePath, err := xdg.SearchCacheFile("appname/app.cache")
	if err != nil {
		// The cache file could not be found.
	}

	fmt.Println("The cache file was found at:", cacheFilePath)
}
Output:

func SearchConfigFile

func SearchConfigFile(relPath string) (string, error)

SearchConfigFile searches for the specified file in config search paths. The relPath parameter must contain the name of the config file, and optionally, a set of parent directories (e.g. appname/app.yaml). If the file cannot be found, an error specifying the searched paths is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	configFilePath, err := xdg.SearchConfigFile("appname/app.yaml")
	if err != nil {
		// The config file could not be found.
	}

	fmt.Println("The config file was found at:", configFilePath)
}
Output:

func SearchDataFile

func SearchDataFile(relPath string) (string, error)

SearchDataFile searches for specified file in the data search paths. The relPath parameter must contain the name of the data file, and optionally, a set of parent directories (e.g. appname/app.data). If the file cannot be found, an error specifying the searched paths is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	dataFilePath, err := xdg.SearchDataFile("appname/app.data")
	if err != nil {
		// The data file could not be found.
	}

	fmt.Println("The data file was found at:", dataFilePath)
}
Output:

func SearchRuntimeFile

func SearchRuntimeFile(relPath string) (string, error)

SearchRuntimeFile searches for the specified file in the runtime search path. The relPath parameter must contain the name of the runtime file, and optionally, a set of parent directories (e.g. appname/app.pid). If the file cannot be found, an error specifying the searched path is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	runtimeFilePath, err := xdg.SearchRuntimeFile("appname/app.pid")
	if err != nil {
		// The runtime file could not be found.
	}

	fmt.Println("The runtime file was found at:", runtimeFilePath)
}
Output:

func SearchStateFile added in v0.3.0

func SearchStateFile(relPath string) (string, error)

SearchStateFile searches for the specified file in the state search path. The relPath parameter must contain the name of the state file, and optionally, a set of parent directories (e.g. appname/app.state). If the file cannot be found, an error specifying the searched path is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	stateFilePath, err := xdg.SearchStateFile("appname/app.state")
	if err != nil {
		// The state file could not be found.
	}

	fmt.Println("The state file was found at:", stateFilePath)
}
Output:

func StateFile added in v0.3.0

func StateFile(relPath string) (string, error)

StateFile returns a suitable location for the specified state file. State files are usually volatile data files, not suitable to be stored relative to the $XDG_DATA_HOME directory. The relPath parameter must contain the name of the state file, and optionally, a set of parent directories (e.g. appname/app.state). If the specified directories do not exist, they will be created relative to the base state directory. On failure, an error containing the attempted paths is returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/xdg"
)

func main() {
	stateFilePath, err := xdg.DataFile("appname/app.state")
	if err != nil {
		// Treat error.
	}

	fmt.Println("Save state file at:", stateFilePath)
}
Output:

Types

type UserDirectories added in v0.2.0

type UserDirectories struct {
	// Desktop defines the location of the user's desktop directory.
	Desktop string

	// Download defines a suitable location for user downloaded files.
	Download string

	// Documents defines a suitable location for user document files.
	Documents string

	// Music defines a suitable location for user audio files.
	Music string

	// Pictures defines a suitable location for user image files.
	Pictures string

	// VideosDir defines a suitable location for user video files.
	Videos string

	// Templates defines a suitable location for user template files.
	Templates string

	// PublicShare defines a suitable location for user shared files.
	PublicShare string
}

UserDirectories defines the locations of well known user directories.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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