dirs.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package settings
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/leonelquinteros/gotext"
  6. "github.com/pkg/errors"
  7. )
  8. // configFileName holds the name of the config file.
  9. const configFileName string = "config.json"
  10. // vcsFileName holds the name of the vcs file.
  11. const vcsFileName string = "vcs.json"
  12. const completionFileName string = "completion.cache"
  13. func getConfigPath() string {
  14. if configHome := os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
  15. if err := initDir(configHome); err == nil {
  16. return filepath.Join(configHome, "yay", configFileName)
  17. }
  18. }
  19. if configHome := os.Getenv("HOME"); configHome != "" {
  20. if err := initDir(configHome); err == nil {
  21. return filepath.Join(configHome, ".config", "yay", configFileName)
  22. }
  23. }
  24. return ""
  25. }
  26. func getCacheHome() string {
  27. if cacheHome := os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
  28. if err := initDir(cacheHome); err == nil {
  29. return filepath.Join(cacheHome, "yay")
  30. }
  31. }
  32. if cacheHome := os.Getenv("HOME"); cacheHome != "" {
  33. if err := initDir(cacheHome); err == nil {
  34. return filepath.Join(cacheHome, ".cache", "yay")
  35. }
  36. }
  37. return "/tmp"
  38. }
  39. func initDir(dir string) error {
  40. if _, err := os.Stat(dir); os.IsNotExist(err) {
  41. if err = os.MkdirAll(dir, 0o755); err != nil {
  42. return errors.New(gotext.Get("failed to create config directory '%s': %s", dir, err))
  43. }
  44. } else if err != nil {
  45. return err
  46. }
  47. return nil
  48. }