Chore: refactoring code of config path

This commit is contained in:
Dreamacro 2018-10-14 21:22:58 +08:00
parent 64e3791654
commit 381f764507
5 changed files with 68 additions and 48 deletions

View file

@ -87,13 +87,18 @@ func (c *Config) Report() chan<- interface{} {
}
func (c *Config) readConfig() (*RawConfig, error) {
if _, err := os.Stat(C.ConfigPath); os.IsNotExist(err) {
if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
return nil, err
}
data, err := ioutil.ReadFile(C.ConfigPath)
data, err := ioutil.ReadFile(C.Path.Config())
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf("Configuration file %s is empty", C.Path.Config())
}
// config with some default value
rawConfig := &RawConfig{
AllowLan: false,

View file

@ -55,16 +55,23 @@ func downloadMMDB(path string) (err error) {
// Init prepare necessary files
func Init() {
// initial homedir
if _, err := os.Stat(C.Path.HomeDir()); os.IsNotExist(err) {
if err := os.MkdirAll(C.Path.HomeDir(), 0777); err != nil {
log.Fatalf("Can't create config directory %s: %s", C.Path.HomeDir(), err.Error())
}
}
// initial config.ini
if _, err := os.Stat(C.ConfigPath); os.IsNotExist(err) {
if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
log.Info("Can't find config, create a empty file")
os.OpenFile(C.ConfigPath, os.O_CREATE|os.O_WRONLY, 0644)
os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0644)
}
// initial mmdb
if _, err := os.Stat(C.MMDBPath); os.IsNotExist(err) {
if _, err := os.Stat(C.Path.MMDB()); os.IsNotExist(err) {
log.Info("Can't find MMDB, start download")
err := downloadMMDB(C.MMDBPath)
err := downloadMMDB(C.Path.MMDB())
if err != nil {
log.Fatalf("Can't download MMDB: %s", err.Error())
}

View file

@ -1,23 +1,5 @@
package constant
import (
"os"
"os/user"
"path"
log "github.com/sirupsen/logrus"
)
const (
Name = "clash"
)
var (
HomeDir string
ConfigPath string
MMDBPath string
)
type General struct {
Mode *string `json:"mode,omitempty"`
AllowLan *bool `json:"allow-lan,omitempty"`
@ -26,26 +8,3 @@ type General struct {
RedirPort *int `json:"redir-port,omitempty"`
LogLevel *string `json:"log-level,omitempty"`
}
func init() {
currentUser, err := user.Current()
if err != nil {
dir := os.Getenv("HOME")
if dir == "" {
log.Fatalf("Can't get current user: %s", err.Error())
}
HomeDir = dir
} else {
HomeDir = currentUser.HomeDir
}
dirPath := path.Join(HomeDir, ".config", Name)
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
if err := os.MkdirAll(dirPath, 0777); err != nil {
log.Fatalf("Can't create config directory %s: %s", dirPath, err.Error())
}
}
ConfigPath = path.Join(dirPath, "config.yml")
MMDBPath = path.Join(dirPath, "Country.mmdb")
}

49
constant/path.go Normal file
View file

@ -0,0 +1,49 @@
package constant
import (
"os"
"os/user"
P "path"
)
const Name = "clash"
// Path is used to get the configuration path
var Path *path
type path struct {
homedir string
}
func init() {
currentUser, err := user.Current()
var homedir string
if err != nil {
dir := os.Getenv("HOME")
if dir == "" {
dir, _ = os.Getwd()
}
homedir = dir
} else {
homedir = currentUser.HomeDir
}
homedir = P.Join(homedir, ".config", Name)
Path = &path{homedir: homedir}
}
// SetHomeDir is used to set the configuration path
func SetHomeDir(root string) {
Path = &path{homedir: root}
}
func (p *path) HomeDir() string {
return p.homedir
}
func (p *path) Config() string {
return P.Join(p.homedir, "config.yml")
}
func (p *path) MMDB() string {
return P.Join(p.homedir, "Country.mmdb")
}

View file

@ -42,7 +42,7 @@ func (g *GEOIP) Payload() string {
func NewGEOIP(country string, adapter string) *GEOIP {
once.Do(func() {
var err error
mmdb, err = geoip2.Open(C.MMDBPath)
mmdb, err = geoip2.Open(C.Path.MMDB())
if err != nil {
log.Fatalf("Can't load mmdb: %s", err.Error())
}