|
|
9 luni în urmă | |
|---|---|---|
| .. | ||
| internal | 9 luni în urmă | |
| CODE_OF_CONDUCT.md | 9 luni în urmă | |
| CONTRIBUTING.md | 9 luni în urmă | |
| LICENSE | 9 luni în urmă | |
| README.md | 9 luni în urmă | |
| base_dirs.go | 9 luni în urmă | |
| codecov.yml | 9 luni în urmă | |
| doc.go | 9 luni în urmă | |
| paths_darwin.go | 9 luni în urmă | |
| paths_plan9.go | 9 luni în urmă | |
| paths_unix.go | 9 luni în urmă | |
| paths_windows.go | 9 luni în urmă | |
| xdg.go | 9 luni în urmă | |
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,
support for the non-standard XDG_BIN_HOME directory, 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.
go get github.com/adrg/xdg
The package defines sensible defaults for XDG variables which are empty or not present in the environment.
Unix
|macOS
|Plan 9
| | :------------------------------------------------------------: | :-----------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------: | | XDG_DATA_HOME | ~/.local/share | ~/Library/Application Support | $home/lib | | XDG_DATA_DIRS | /usr/local/shareKnown Folder(s)
|Fallback(s)
| | :------------------------------------------------------------: | :---------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------: | | XDG_DATA_HOME | LocalAppData | %LOCALAPPDATA% | | XDG_DATA_DIRS | RoamingAppDataXDG user directories environment variables are usually not set on most operating systems. However, if they are present in the environment, they take precedence. Appropriate fallback locations are used for the environment variables which are not set.
Lastly, default locations are used for any user directories which are not set, as shown in the following tables.
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 |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% |Unix
|macOS
|Plan 9
| | :-----------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------: | | Home | $HOME | $HOME | $home | | Applications | $XDG_DATA_HOME/applicationsKnown Folder(s)
|Fallback(s)
| | :-----------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | | Home | Profile | %USERPROFILE% | | Applications | Programspackage 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)
log.Println("Home binaries directory:", xdg.BinHome)
// 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.
// It is recommended for files to be saved inside an application directory
// relative to the base directory rather than directly inside the base
// directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`).
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()
}
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)
}
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, rrjjvv, GreyXor, Rican7, nothub.
For more information see:
Copyright (c) 2014 Adrian-George Bostan.
This project is licensed under the MIT license. See LICENSE for more details.