-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
68 lines (58 loc) · 1.54 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
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
// The Config options
type Config struct {
GitHubToken string
UpstreamOwner string
UpstreamRepo string
DownstreamOwner string
DownstreamRepo string
RepoPath string
ToolPath string
UseWebhook bool
WebhookPort int
WebhookSecret string
}
// Init will initalize the config file
func (c Config) Init() Config {
if _, err := os.Stat("./config.json"); err != nil {
fmt.Println("A configuration could not be found at config.json. Writing a config now. Please customize it to your needs and start the application again.")
c.Save("./config.json")
os.Exit(10)
}
return c.Load("./config.json")
}
// Save writes a config file at the specified path
func (c Config) Save(path string) {
file, err := os.OpenFile(path, os.O_CREATE, 600)
if err != nil {
var errMsg = fmt.Sprintf("Error while opening config: %s\n", err)
panic(errMsg)
}
defer file.Close()
jsonString, err := json.MarshalIndent(c, "", " ")
if err != nil {
var errMsg = fmt.Sprintf("Error while serializing config: %s\n", err)
panic(errMsg)
}
file.Write(jsonString)
}
//Load loads a config file from the specified path
func (c Config) Load(path string) (config Config) {
jsonString, err := ioutil.ReadFile(path)
if err != nil {
var errMsg = fmt.Sprintf("Error while opening config: %s\n", err)
panic(errMsg)
}
err = json.Unmarshal(jsonString, &config)
if err != nil {
var errMsg = fmt.Sprintf("Error while deserializing config: %s\n", err)
panic(errMsg)
}
return config
}