doc.go raw

   1  /*
   2  Package xdg provides an implementation of the XDG Base Directory Specification.
   3  The specification defines a set of standard paths for storing application files
   4  including data and configuration files. For portability and flexibility reasons,
   5  applications should use the XDG defined locations instead of hardcoding paths.
   6  The package also includes the locations of well known user directories.
   7  
   8  The current implementation supports most flavors of Unix, Windows, Mac OS and Plan 9.
   9  
  10  	For more information regarding the XDG Base Directory Specification see:
  11  	https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
  12  
  13  	For more information regarding the XDG user directories see:
  14  	https://wiki.archlinux.org/index.php/XDG_user_directories
  15  
  16  	For more information regarding the Windows Known Folders see:
  17  	https://docs.microsoft.com/en-us/windows/win32/shell/known-folders
  18  
  19  # Usage
  20  
  21  XDG Base Directory
  22  
  23  	package main
  24  
  25  	import (
  26  		"log"
  27  
  28  		"github.com/adrg/xdg"
  29  	)
  30  
  31  	func main() {
  32  		// XDG Base Directory paths.
  33  		log.Println("Home data directory:", xdg.DataHome)
  34  		log.Println("Data directories:", xdg.DataDirs)
  35  		log.Println("Home config directory:", xdg.ConfigHome)
  36  		log.Println("Config directories:", xdg.ConfigDirs)
  37  		log.Println("Home state directory:", xdg.StateHome)
  38  		log.Println("Cache directory:", xdg.CacheHome)
  39  		log.Println("Runtime directory:", xdg.RuntimeDir)
  40  		log.Println("Home binaries directory:", xdg.BinHome)
  41  
  42  		// Other common directories.
  43  		log.Println("Home directory:", xdg.Home)
  44  		log.Println("Application directories:", xdg.ApplicationDirs)
  45  		log.Println("Font directories:", xdg.FontDirs)
  46  
  47  		// Obtain a suitable location for application config files.
  48  		// ConfigFile takes one parameter which must contain the name of the file,
  49  		// but it can also contain a set of parent directories. If the directories
  50  		// don't exist, they will be created relative to the base config directory.
  51  		// It is recommended for files to be saved inside an application directory
  52  		// relative to the base directory rather than directly inside the base
  53  		// directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`).
  54  		configFilePath, err := xdg.ConfigFile("appname/config.yaml")
  55  		if err != nil {
  56  			log.Fatal(err)
  57  		}
  58  		log.Println("Save the config file at:", configFilePath)
  59  
  60  		// For other types of application files use:
  61  		// xdg.DataFile()
  62  		// xdg.StateFile()
  63  		// xdg.CacheFile()
  64  		// xdg.RuntimeFile()
  65  
  66  		// Finding application config files.
  67  		// SearchConfigFile takes one parameter which must contain the name of
  68  		// the file, but it can also contain a set of parent directories relative
  69  		// to the config search paths (xdg.ConfigHome and xdg.ConfigDirs).
  70  		configFilePath, err = xdg.SearchConfigFile("appname/config.yaml")
  71  		if err != nil {
  72  			log.Fatal(err)
  73  		}
  74  		log.Println("Config file was found at:", configFilePath)
  75  
  76  		// For other types of application files use:
  77  		// xdg.SearchDataFile()
  78  		// xdg.SearchStateFile()
  79  		// xdg.SearchCacheFile()
  80  		// xdg.SearchRuntimeFile()
  81  	}
  82  
  83  XDG user directories
  84  
  85  	package main
  86  
  87  	import (
  88  		"log"
  89  
  90  		"github.com/adrg/xdg"
  91  	)
  92  
  93  	func main() {
  94  		// XDG user directories.
  95  		log.Println("Desktop directory:", xdg.UserDirs.Desktop)
  96  		log.Println("Download directory:", xdg.UserDirs.Download)
  97  		log.Println("Documents directory:", xdg.UserDirs.Documents)
  98  		log.Println("Music directory:", xdg.UserDirs.Music)
  99  		log.Println("Pictures directory:", xdg.UserDirs.Pictures)
 100  		log.Println("Videos directory:", xdg.UserDirs.Videos)
 101  		log.Println("Templates directory:", xdg.UserDirs.Templates)
 102  		log.Println("Public directory:", xdg.UserDirs.PublicShare)
 103  	}
 104  */
 105  package xdg
 106