sing-box/constant/path.go

42 lines
884 B
Go
Raw Permalink Normal View History

2022-07-02 22:55:10 +08:00
package constant
import (
"os"
"path/filepath"
"github.com/sagernet/sing/common/rw"
)
const dirName = "sing-box"
2023-04-21 17:29:00 +08:00
var resourcePaths []string
2022-07-06 14:44:51 +08:00
func FindPath(name string) (string, bool) {
2022-07-02 22:55:10 +08:00
name = os.ExpandEnv(name)
2024-06-24 09:49:15 +08:00
if rw.IsFile(name) {
2022-07-02 22:55:10 +08:00
return name, true
}
for _, dir := range resourcePaths {
2024-06-24 09:49:15 +08:00
if path := filepath.Join(dir, dirName, name); rw.IsFile(path) {
2022-07-02 22:55:10 +08:00
return path, true
}
2024-06-24 09:49:15 +08:00
if path := filepath.Join(dir, name); rw.IsFile(path) {
return path, true
}
2022-07-02 22:55:10 +08:00
}
return name, false
}
func init() {
resourcePaths = append(resourcePaths, ".")
if home := os.Getenv("HOME"); home != "" {
resourcePaths = append(resourcePaths, home)
}
2022-07-02 22:55:10 +08:00
if userConfigDir, err := os.UserConfigDir(); err == nil {
resourcePaths = append(resourcePaths, userConfigDir)
}
if userCacheDir, err := os.UserCacheDir(); err == nil {
resourcePaths = append(resourcePaths, userCacheDir)
}
}