-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
56 lines (48 loc) · 1.16 KB
/
config.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package handlers
import (
"os"
"path"
"github.com/pelletier/go-toml"
"github.com/bianjieai/tibc-relayer-go/internal/pkg/configs"
"github.com/bianjieai/tibc-relayer-go/tools"
)
func ConfigInit(home string) error {
cfgDir := path.Join(home, tools.DefaultConfigDirName)
cfgPath := path.Join(cfgDir, tools.DefaultConfigName)
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
// And the home folder doesn't exist
if _, err := os.Stat(home); os.IsNotExist(err) {
// Create the home folder
if err = os.Mkdir(home, os.ModePerm); err != nil {
return err
}
}
// Create the home config folder
if err = os.Mkdir(cfgDir, os.ModePerm); err != nil {
return err
}
}
// Then create the file...
f, err := os.Create(cfgPath)
if err != nil {
return err
}
defer f.Close()
// And write the default config to that location...
if _, err = f.Write(defaultConfig()); err != nil {
return err
}
// And return no error...
return nil
}
func defaultConfig() []byte {
cfg := configs.NewConfig()
cfg.App.MetricAddr = "0.0.0.0:8083"
cfg.App.Env = "dev"
cfg.App.LogLevel = "debug"
data, err := toml.Marshal(cfg)
if err != nil {
panic(err)
}
return data
}