github.com/chwjbn/xclash@v0.2.0/constant/path.go (about)

     1  package constant
     2  
     3  import (
     4  	"os"
     5  	P "path"
     6  	"path/filepath"
     7  )
     8  
     9  const Name = "clash"
    10  
    11  // Path is used to get the configuration path
    12  var Path = func() *path {
    13  	homeDir, err := os.UserHomeDir()
    14  	if err != nil {
    15  		homeDir, _ = os.Getwd()
    16  	}
    17  
    18  	homeDir = P.Join(homeDir, ".config", Name)
    19  	return &path{homeDir: homeDir, configFile: "config.yaml"}
    20  }()
    21  
    22  type path struct {
    23  	homeDir    string
    24  	configFile string
    25  }
    26  
    27  // SetHomeDir is used to set the configuration path
    28  func SetHomeDir(root string) {
    29  	Path.homeDir = root
    30  }
    31  
    32  // SetConfig is used to set the configuration file
    33  func SetConfig(file string) {
    34  	Path.configFile = file
    35  }
    36  
    37  func (p *path) HomeDir() string {
    38  	return p.homeDir
    39  }
    40  
    41  func (p *path) Config() string {
    42  	return p.configFile
    43  }
    44  
    45  // Resolve return a absolute path or a relative path with homedir
    46  func (p *path) Resolve(path string) string {
    47  	if !filepath.IsAbs(path) {
    48  		return filepath.Join(p.HomeDir(), path)
    49  	}
    50  
    51  	return path
    52  }
    53  
    54  func (p *path) MMDB() string {
    55  	return P.Join(p.homeDir, "Country.mmdb")
    56  }
    57  
    58  func (p *path) OldCache() string {
    59  	return P.Join(p.homeDir, ".cache")
    60  }
    61  
    62  func (p *path) Cache() string {
    63  	return P.Join(p.homeDir, "cache.db")
    64  }