forked from MetaCubeX/mihomo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitial.go
32 lines (27 loc) · 778 Bytes
/
initial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package config
import (
"fmt"
"os"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
)
// Init prepare necessary files
func Init(dir string) error {
// initial homedir
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0o777); err != nil {
return fmt.Errorf("can't create config directory %s: %s", dir, err.Error())
}
}
// initial config.yaml
if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
log.Infoln("Can't find config, create a initial config file")
f, err := os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("can't create file %s: %s", C.Path.Config(), err.Error())
}
f.Write([]byte(`mixed-port: 7890`))
f.Close()
}
return nil
}