mihomo/constant/path.go

91 lines
1.7 KiB
Go
Raw Normal View History

2018-10-14 21:22:58 +08:00
package constant
import (
"os"
P "path"
"path/filepath"
2018-10-14 21:22:58 +08:00
)
const Name = "clash"
// Path is used to get the configuration path
var Path = func() *path {
homeDir, err := os.UserHomeDir()
2018-10-14 21:22:58 +08:00
if err != nil {
homeDir, _ = os.Getwd()
2018-10-14 21:22:58 +08:00
}
2019-02-27 01:02:43 +08:00
homeDir = P.Join(homeDir, ".config", Name)
return &path{homeDir: homeDir, configFile: "config.yaml"}
}()
type path struct {
homeDir string
configFile string
2021-10-15 14:11:14 +08:00
scriptDir string
2018-10-14 21:22:58 +08:00
}
// SetHomeDir is used to set the configuration path
func SetHomeDir(root string) {
Path.homeDir = root
}
// SetConfig is used to set the configuration file
func SetConfig(file string) {
Path.configFile = file
2018-10-14 21:22:58 +08:00
}
func (p *path) HomeDir() string {
return p.homeDir
2018-10-14 21:22:58 +08:00
}
func (p *path) Config() string {
return p.configFile
2018-10-14 21:22:58 +08:00
}
2020-01-30 17:03:11 +08:00
// Resolve return a absolute path or a relative path with homedir
func (p *path) Resolve(path string) string {
if !filepath.IsAbs(path) {
return filepath.Join(p.HomeDir(), path)
}
return path
}
2018-10-14 21:22:58 +08:00
func (p *path) MMDB() string {
return P.Join(p.homeDir, "Country.mmdb")
2018-10-14 21:22:58 +08:00
}
func (p *path) Cache() string {
return P.Join(p.homeDir, ".cache")
}
2021-07-01 22:49:29 +08:00
func (p *path) GeoIP() string {
return P.Join(p.homeDir, "geoip.dat")
}
func (p *path) GeoSite() string {
return P.Join(p.homeDir, "geosite.dat")
}
2021-10-15 14:11:14 +08:00
func (p *path) ScriptDir() string {
if len(p.scriptDir) != 0 {
return p.scriptDir
}
if dir, err := os.MkdirTemp("", Name+"-"); err == nil {
p.scriptDir = dir
} else {
p.scriptDir = P.Join(os.TempDir(), Name)
os.MkdirAll(p.scriptDir, 0644)
}
return p.scriptDir
}
func (p *path) Script() string {
return P.Join(p.ScriptDir(), "clash_script.py")
}
2021-07-01 22:49:29 +08:00
func (p *path) GetAssetLocation(file string) string {
return P.Join(p.homeDir, file)
}