datadir.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. )
  7. // Profiles and definitions are the tool's own files, and a binary installed with
  8. // `go install` sits in $GOPATH/bin where neither of them exists. They are looked
  9. // for next to the binary, then in the working directory, then under the platform's
  10. // per-user configuration directory. The first that exists wins, and the user
  11. // directory is where one is created.
  12. //
  13. // The order puts a repository checkout before the user directory on purpose: a
  14. // project that ships its own definitions, as this one does, must keep using them.
  15. // The other way round, a stale file in someone's home directory would silently
  16. // name a board they are working on.
  17. const dataDirName = "qmk-rgb-tool"
  18. // userConfigDir is a seam: os.UserConfigDir is the platform's own answer, and a
  19. // test cannot rely on which platform it runs on.
  20. var userConfigDir = os.UserConfigDir
  21. // executableDir and workingDir are seams for the same reason.
  22. var executableDir = func() string {
  23. if exe, err := os.Executable(); err == nil {
  24. return filepath.Dir(exe)
  25. }
  26. return ""
  27. }
  28. var workingDir = os.Getwd
  29. // The two overrides short-circuit the lookup, which is how the tests point the
  30. // lookup at a temporary directory. Empty means resolve it.
  31. var (
  32. profilesDirOverride string
  33. definitionsDirOverride string
  34. )
  35. // resolveDataDir returns the directory a named set of files lives in: the first
  36. // that exists, in the order next to the binary, then the working directory, then
  37. // the user's. When none exists the user's is returned, because that is where
  38. // something new belongs.
  39. func resolveDataDir(exeDir, cwd, name string) string {
  40. for _, dir := range []string{
  41. filepath.Join(exeDir, name),
  42. filepath.Join(cwd, name),
  43. filepath.Join(userDataDir(), name),
  44. } {
  45. if dir == name {
  46. continue // an empty parent would resolve to a relative path
  47. }
  48. if info, err := os.Stat(dir); err == nil && info.IsDir() {
  49. return dir
  50. }
  51. }
  52. return filepath.Join(userDataDir(), name)
  53. }
  54. // userDataDir is this tool's directory under the platform's configuration
  55. // directory: ~/.config/qmk-rgb-tool on Linux, ~/Library/Application
  56. // Support/qmk-rgb-tool on macOS, %AppData%\qmk-rgb-tool on Windows. A hardcoded
  57. // ~/.config would be wrong on two of the three.
  58. func userDataDir() string {
  59. base, err := userConfigDir()
  60. if err != nil || base == "" {
  61. return dataDirName
  62. }
  63. return filepath.Join(base, dataDirName)
  64. }
  65. // definitionsPath is where the VIA definition files are read from and fetched to.
  66. func definitionsPath() string {
  67. if definitionsDirOverride != "" {
  68. return definitionsDirOverride
  69. }
  70. return resolveDataDir(executableDir(), workingDirResult(), "definitions")
  71. }
  72. // profilesPath is where the profiles are read from and written to.
  73. func profilesPath() string {
  74. if profilesDirOverride != "" {
  75. return profilesDirOverride
  76. }
  77. return resolveDataDir(executableDir(), workingDirResult(), "profiles")
  78. }
  79. // workingDirResult keeps a failing os.Getwd from turning into a relative path.
  80. func workingDirResult() string {
  81. cwd, err := workingDir()
  82. if err != nil {
  83. return ""
  84. }
  85. return cwd
  86. }
  87. // ensureDataDir returns a directory and creates it, for the commands that write.
  88. func ensureDataDir(path string) (string, error) {
  89. if err := os.MkdirAll(path, 0o755); err != nil {
  90. return "", err
  91. }
  92. return path, nil
  93. }
  94. // describeDataDir annotates a path only where the answer is not the obvious one:
  95. // the user directory is neither beside the binary nor in the directory the
  96. // command was run from, so a path pointing there is worth marking. The other two
  97. // are the two places a user would look, and the full path is printed either way.
  98. func describeDataDir(path string) string {
  99. if user := userDataDir(); path == user || strings.HasPrefix(path, user+string(filepath.Separator)) {
  100. return path + " (your user directory)"
  101. }
  102. return path
  103. }