2018-10-14 13:22:58 +00:00
|
|
|
package constant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
P "path"
|
2019-12-01 05:22:47 +00:00
|
|
|
"path/filepath"
|
2018-10-14 13:22:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const Name = "clash"
|
|
|
|
|
|
|
|
// Path is used to get the configuration path
|
2021-07-11 11:42:54 +00:00
|
|
|
var Path = func() *path {
|
2019-10-14 10:11:22 +00:00
|
|
|
homeDir, err := os.UserHomeDir()
|
2018-10-14 13:22:58 +00:00
|
|
|
if err != nil {
|
2019-10-14 10:11:22 +00:00
|
|
|
homeDir, _ = os.Getwd()
|
2018-10-14 13:22:58 +00:00
|
|
|
}
|
2019-02-26 17:02:43 +00:00
|
|
|
|
2019-10-14 10:11:22 +00:00
|
|
|
homeDir = P.Join(homeDir, ".config", Name)
|
2021-07-11 11:42:54 +00:00
|
|
|
return &path{homeDir: homeDir, configFile: "config.yaml"}
|
|
|
|
}()
|
|
|
|
|
|
|
|
type path struct {
|
|
|
|
homeDir string
|
|
|
|
configFile string
|
2018-10-14 13:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHomeDir is used to set the configuration path
|
|
|
|
func SetHomeDir(root string) {
|
2019-10-14 10:11:22 +00:00
|
|
|
Path.homeDir = root
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig is used to set the configuration file
|
|
|
|
func SetConfig(file string) {
|
|
|
|
Path.configFile = file
|
2018-10-14 13:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) HomeDir() string {
|
2019-10-14 10:11:22 +00:00
|
|
|
return p.homeDir
|
2018-10-14 13:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) Config() string {
|
2019-10-14 10:11:22 +00:00
|
|
|
return p.configFile
|
2018-10-14 13:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 09:03:11 +00:00
|
|
|
// Resolve return a absolute path or a relative path with homedir
|
|
|
|
func (p *path) Resolve(path string) string {
|
2019-12-01 05:22:47 +00:00
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
return filepath.Join(p.HomeDir(), path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2018-10-14 13:22:58 +00:00
|
|
|
func (p *path) MMDB() string {
|
2019-10-14 10:11:22 +00:00
|
|
|
return P.Join(p.homeDir, "Country.mmdb")
|
2018-10-14 13:22:58 +00:00
|
|
|
}
|
2021-02-18 15:41:50 +00:00
|
|
|
|
|
|
|
func (p *path) Cache() string {
|
|
|
|
return P.Join(p.homeDir, ".cache")
|
|
|
|
}
|
2021-07-01 14:49:29 +00: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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) GetAssetLocation(file string) string {
|
|
|
|
return P.Join(p.homeDir, file)
|
|
|
|
}
|