-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
49 lines (42 loc) · 921 Bytes
/
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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path"
"strings"
)
var (
homeUser = os.Getenv("HOME")
configPath = path.Join(homeUser, ".dnscli")
configFile = path.Join(configPath, "config.json")
config = readConfig()
)
// A Config struct keeps the credentials information
// stored as a JSON document on `configFile`.
type Config struct {
Token string
Domain string
Mail string
ApiURL string
}
func createConfigPath() {
// Create the config directory.
err := os.Mkdir(configPath, 0755)
if err != nil {
e := err.(*os.PathError)
if strings.Contains(e.Err.Error(), "file exists") {
// Ignore error if dir already exists.
} else {
log.Fatal("pathConfig: ", err)
}
}
// Create an empty config file.
c := Config{}
emptyConfig, err := json.Marshal(c)
if err != nil {
log.Fatal("pathConfig-Marshal: ", err)
}
ioutil.WriteFile(configFile, emptyConfig, 0644)
}