/
config.go
164 lines (146 loc) · 3.27 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package config
import (
"bytes"
"os"
"time"
plugins "github.com/tylerconlee/slab/_plugins"
l "github.com/tylerconlee/slab/log"
"github.com/BurntSushi/toml"
)
var log = l.Log
// Config maps the values of the configuration file to a struct usable by the
// rest of the app
type Config struct {
Zendesk Zendesk
Slack Slack
LogLevel string
SLA SLA
UpdateFreq Duration
TriageEnabled bool
Metadata Metadata
Port int
Plugins plugins.Plugins
Postgres Postgres
}
// Metadata holds configuration related to the server metadata used in status calls
type Metadata struct {
Server string
}
// Slack API key and Channel ID tell SLAB where to post notifications
type Slack struct {
APIKey string
ChannelID string
}
type Postgres struct {
Host string
Port int
User string
Password string
DBName string
}
// Zendesk contains configuration values specific to the Zendesk interactions
type Zendesk struct {
User string
APIKey string
URL string
PremiumUpdates bool
}
// SLA supports up to 4 levels of SLA in the configuration
type SLA struct {
LevelOne Level
LevelTwo Level
LevelThree Level
LevelFour Level
}
// Level reflects the 4 priority levels Zendesk uses for SLA.
type Level struct {
Tag string
Low Duration
Normal Duration
High Duration
Urgent Duration
Notify bool
}
// Duration allows for configurations to contain "3h", "8m", etc.
type Duration struct {
time.Duration
}
// LoadConfig grabs the command line argument for where the configuration file
// is located and loads that into memory.
func LoadConfig() (config Config) {
if _, err := os.Stat("config.toml"); err == nil {
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
log.Error("Configuration file not found.", map[string]interface{}{
"module": "config",
"error": err,
})
config = defaultConfig()
return
}
log.Info("Configuration loaded successfully", map[string]interface{}{
"module": "config",
"file": "config.toml",
})
return
}
config = defaultConfig()
return
}
// SaveConfig takes a config and saves it to the local file, config.toml.
func SaveConfig(config Config) bool {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(config); err != nil {
log.Error("Error creating new buffer for config", map[string]interface{}{
"module": "config",
"config": config,
"error": err,
})
return false
}
f, err := os.Create("config.toml")
if nil != err {
log.Error("error saving file", map[string]interface{}{
"module": "config",
"error": err,
})
}
defer f.Close()
n, err := f.WriteString(buf.String())
if nil != err {
log.Error("error saving file", map[string]interface{}{
"module": "config",
})
}
f.Sync()
log.Debug("Saved configuration file", map[string]interface{}{
"module": "config",
"output": n,
})
return true
}
func defaultConfig() (config Config) {
config = Config{
Zendesk: Zendesk{
APIKey: "",
User: "",
URL: "",
},
SLA: SLA{
LevelOne: Level{
Tag: "platinum",
},
LevelTwo: Level{
Tag: "gold",
},
LevelThree: Level{
Tag: "silver",
},
LevelFour: Level{
Tag: "bronze",
},
},
Metadata: Metadata{},
TriageEnabled: true,
}
return
}