doc.go 3.6 KB

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