clash/config/initial.go

33 lines
778 B
Go
Raw Normal View History

package config
import (
2018-11-21 05:47:46 +00:00
"fmt"
"os"
2023-11-03 13:01:45 +00:00
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
)
// Init prepare necessary files
2018-11-21 05:47:46 +00:00
func Init(dir string) error {
2018-10-14 13:22:58 +00:00
// initial homedir
2018-11-21 05:47:46 +00:00
if _, err := os.Stat(dir); os.IsNotExist(err) {
2021-10-10 15:44:09 +00:00
if err := os.MkdirAll(dir, 0o777); err != nil {
2020-08-25 14:19:59 +00:00
return fmt.Errorf("can't create config directory %s: %s", dir, err.Error())
2018-10-14 13:22:58 +00:00
}
}
// initial config.yaml
2018-10-14 13:22:58 +00:00
if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
2020-02-18 05:48:15 +00:00
log.Infoln("Can't find config, create a initial config file")
2021-10-10 15:44:09 +00:00
f, err := os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0o644)
2020-02-18 05:48:15 +00:00
if err != nil {
2020-08-25 14:19:59 +00:00
return fmt.Errorf("can't create file %s: %s", C.Path.Config(), err.Error())
2020-02-18 05:48:15 +00:00
}
f.Write([]byte(`mixed-port: 7890`))
2020-02-18 05:48:15 +00:00
f.Close()
}
2021-11-17 08:03:47 +00:00
2018-11-21 05:47:46 +00:00
return nil
}