GithubHelp home page GithubHelp logo

favadi / xdg Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adrg/xdg

0.0 1.0 0.0 101 KB

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

Home Page: https://pkg.go.dev/github.com/adrg/xdg

License: MIT License

Go 100.00%

xdg's Introduction

xdg

Build Status Code coverage pkg.go.dev documentation MIT license Go report card GitHub 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 and an implementation of the state directory proposal. Windows, macOS and most flavors of Unix are supported.

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.

XDG Base Directory

Unix macOS Windows
XDG_DATA_HOME ~/.local/share ~/Library/Application Support %LOCALAPPDATA%
XDG_DATA_DIRS /usr/local/share
/usr/share
/Library/Application Support %APPDATA%\Roaming
%PROGRAMDATA%
XDG_CONFIG_HOME ~/.config ~/Library/Preferences %LOCALAPPDATA%
XDG_CONFIG_DIRS /etc/xdg /Library/Preferences %PROGRAMDATA%
XDG_CACHE_HOME ~/.cache ~/Library/Caches %LOCALAPPDATA%\cache
XDG_RUNTIME_DIR /run/user/UID ~/Library/Application Support %LOCALAPPDATA%

XDG user directories

Unix macOS Windows
XDG_DESKTOP_DIR ~/Desktop ~/Desktop %USERPROFILE%/Desktop
XDG_DOWNLOAD_DIR ~/Downloads ~/Downloads %USERPROFILE%/Downloads
XDG_DOCUMENTS_DIR ~/Documents ~/Documents %USERPROFILE%/Documents
XDG_MUSIC_DIR ~/Music ~/Music %USERPROFILE%/Music
XDG_PICTURES_DIR ~/Pictures ~/Pictures %USERPROFILE%/Pictures
XDG_VIDEOS_DIR ~/Videos ~/Movies %USERPROFILE%/Videos
XDG_TEMPLATES_DIR ~/Templates ~/Templates %USERPROFILE%/Templates
XDG_PUBLICSHARE_DIR ~/Public ~/Public %PUBLIC%

Non-standard directories

State directory

Unix:    ~/.local/state
macOS:   ~/Library/Application Support
Windows: %LOCALAPPDATA%

Application directories

Unix:
- $XDG_DATA_HOME/applications
- ~/.local/share/applications
- /usr/local/share/applications
- /usr/share/applications
- $XDG_DATA_DIRS/applications

macOS:
- /Applications

Windows:
- %APPDATA%\Roaming\Microsoft\Windows\Start Menu\Programs

Font directories

Unix:
- $XDG_DATA_HOME/fonts
- ~/.fonts
- ~/.local/share/fonts
- /usr/local/share/fonts
- /usr/share/fonts
- $XDG_DATA_DIRS/fonts

macOS:
- ~/Library/Fonts
- /Library/Fonts
- /System/Library/Fonts
- /Network/Library/Fonts

Windows:
- %windir%\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("Cache directory:", xdg.CacheHome)
	log.Println("Runtime directory:", xdg.RuntimeDir)

	// Non-standard directories.
	log.Println("Home state directory:", xdg.StateHome)
	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.CacheFile()
	// xdg.RuntimeFile()
	// xdg.StateFile()

	// 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.SearchCacheFile()
	// xdg.SearchRuntimeFile()
	// xdg.SearchStateFile()
}

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.

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.

xdg's People

Contributors

adrg avatar gabriel-vasile avatar kalledk avatar bouncepaw avatar wichert avatar

Watchers

 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.