path.go 884 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package constant
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/sagernet/sing/common/rw"
  6. )
  7. const dirName = "sing-box"
  8. var resourcePaths []string
  9. func FindPath(name string) (string, bool) {
  10. name = os.ExpandEnv(name)
  11. if rw.IsFile(name) {
  12. return name, true
  13. }
  14. for _, dir := range resourcePaths {
  15. if path := filepath.Join(dir, dirName, name); rw.IsFile(path) {
  16. return path, true
  17. }
  18. if path := filepath.Join(dir, name); rw.IsFile(path) {
  19. return path, true
  20. }
  21. }
  22. return name, false
  23. }
  24. func init() {
  25. resourcePaths = append(resourcePaths, ".")
  26. if home := os.Getenv("HOME"); home != "" {
  27. resourcePaths = append(resourcePaths, home)
  28. }
  29. if userConfigDir, err := os.UserConfigDir(); err == nil {
  30. resourcePaths = append(resourcePaths, userConfigDir)
  31. }
  32. if userCacheDir, err := os.UserCacheDir(); err == nil {
  33. resourcePaths = append(resourcePaths, userCacheDir)
  34. }
  35. }