-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
71 lines (61 loc) · 1.59 KB
/
init.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"log"
"os"
"github.com/manigandand/angago/config"
"github.com/manigandand/angago/util"
)
func getConfigOrDefault() *AngaGoConf {
isFileExist := util.FileExists(config.AngagoConfigPath)
if !isFileExist {
return loadFromDefault(config.Home, config.AngagoConfigPath)
}
// load from the file
body, err := getBytesForFileOrURL(config.AngagoConfigPath)
if err != nil {
log.Fatal(err)
}
angagoconfig, err := decodeConfig(body)
if err != nil {
log.Fatal(err)
}
log.Printf("configuration loaded from %s...\n", config.AngagoConfigPath)
return angagoconfig
}
func loadFromDefault(home, configFilePath string) *AngaGoConf {
defaultConfigpath := "stub/default.config.yaml"
body, err := getBytesForFileOrURL(defaultConfigpath)
if err != nil {
log.Fatal(err)
}
config, err := decodeConfig(body)
if err != nil {
log.Fatal(err)
}
cloneDefaultConfig(home, configFilePath, body)
log.Println("Default configuration loaded. Please update your configuration at ", configFilePath)
return config
}
func cloneDefaultConfig(home, configFilePath string, content []byte) {
fileDir := home + "/.angago"
if _, err := os.Stat(fileDir); os.IsNotExist(err) {
if err := os.Mkdir(fileDir, os.ModePerm); err != nil {
log.Println("Can't create config dir ", fileDir, err)
return
}
}
f, err := os.Create(configFilePath)
if err != nil {
log.Println("Can't clone default config", err)
return
}
if _, err := f.Write(content); err != nil {
log.Println("Can't clone default config", err)
f.Close()
return
}
if err = f.Close(); err != nil {
log.Println(err)
return
}
}