pathutil.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package pathutil
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. )
  7. // Unique eliminates the duplicate paths from the provided slice and returns
  8. // the result. The paths are expanded using the `ExpandHome` function and only
  9. // absolute paths are kept. The items in the output slice are in the order in
  10. // which they occur in the input slice.
  11. func Unique(paths []string) []string {
  12. var (
  13. uniq []string
  14. registry = map[string]struct{}{}
  15. )
  16. for _, p := range paths {
  17. if p = ExpandHome(p); p != "" && filepath.IsAbs(p) {
  18. if _, ok := registry[p]; ok {
  19. continue
  20. }
  21. registry[p] = struct{}{}
  22. uniq = append(uniq, p)
  23. }
  24. }
  25. return uniq
  26. }
  27. // First returns the first absolute path from the provided slice.
  28. // The paths in the input slice are expanded using the `ExpandHome` function.
  29. func First(paths []string) string {
  30. for _, p := range paths {
  31. if p = ExpandHome(p); p != "" && filepath.IsAbs(p) {
  32. return p
  33. }
  34. }
  35. return ""
  36. }
  37. // Create returns a suitable location relative to which the file with the
  38. // specified `name` can be written. The first path from the provided `paths`
  39. // slice which is successfully created (or already exists) is used as a base
  40. // path for the file. The `name` parameter should contain the name of the file
  41. // which is going to be written in the location returned by this function, but
  42. // it can also contain a set of parent directories, which will be created
  43. // relative to the selected parent path.
  44. func Create(name string, paths []string) (string, error) {
  45. searchedPaths := make([]string, 0, len(paths))
  46. for _, p := range paths {
  47. p = filepath.Join(p, name)
  48. dir := filepath.Dir(p)
  49. if Exists(dir) {
  50. return p, nil
  51. }
  52. if err := os.MkdirAll(dir, os.ModeDir|0o700); err == nil {
  53. return p, nil
  54. }
  55. searchedPaths = append(searchedPaths, dir)
  56. }
  57. return "", fmt.Errorf("could not create any of the following paths: %v",
  58. searchedPaths)
  59. }
  60. // Search searches for the file with the specified `name` in the provided
  61. // slice of `paths`. The `name` parameter must contain the name of the file,
  62. // but it can also contain a set of parent directories.
  63. func Search(name string, paths []string) (string, error) {
  64. searchedPaths := make([]string, 0, len(paths))
  65. for _, p := range paths {
  66. p = filepath.Join(p, name)
  67. if Exists(p) {
  68. return p, nil
  69. }
  70. searchedPaths = append(searchedPaths, filepath.Dir(p))
  71. }
  72. return "", fmt.Errorf("could not locate `%s` in any of the following paths: %v",
  73. filepath.Base(name), searchedPaths)
  74. }
  75. // EnvPath returns the value of the environment variable with the specified
  76. // `name` if it is an absolute path, or the first absolute fallback path.
  77. // All paths are expanded using the `ExpandHome` function.
  78. func EnvPath(name string, fallbackPaths ...string) string {
  79. dir := ExpandHome(os.Getenv(name))
  80. if dir != "" && filepath.IsAbs(dir) {
  81. return dir
  82. }
  83. return First(fallbackPaths)
  84. }
  85. // EnvPathList reads the value of the environment variable with the specified
  86. // `name` and attempts to extract a list of absolute paths from it. If there
  87. // are none, a list of absolute fallback paths is returned instead. Duplicate
  88. // paths are removed from the returned slice. All paths are expanded using the
  89. // `ExpandHome` function.
  90. func EnvPathList(name string, fallbackPaths ...string) []string {
  91. dirs := Unique(filepath.SplitList(os.Getenv(name)))
  92. if len(dirs) != 0 {
  93. return dirs
  94. }
  95. return Unique(fallbackPaths)
  96. }