-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
57 lines (48 loc) · 1.15 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
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
const configFile = "config.yaml" // Default config file name
type Configuration struct {
User string `yaml:"user"`
Password string `yaml:"password"`
MongoHost string `yaml:"mongoHost"`
MongoPort uint16 `yaml:"mongoPort"` // Only allow 0-65535
SlackToken string `yaml:"slackToken"`
SlackChanID string `yaml:"slackChanID"`
}
func defaultConfig() *Configuration {
return &Configuration{
User: "simple-wd",
Password: "",
MongoHost: "localhost",
MongoPort: 27017,
SlackToken: "",
SlackChanID: "",
}
}
func NewConfig() *Configuration {
newConfig := defaultConfig()
data, err := os.ReadFile(configFile)
if err != nil {
fmt.Fprintf(
os.Stderr,
"Failed to read config file '%s': %s\nUsing default configuration values\n",
configFile,
err,
)
return newConfig
}
// yaml.Unmarshal applies the YAML config to the config object
if err = yaml.Unmarshal(data, &newConfig); err != nil {
fmt.Fprintf(
os.Stderr,
"Failed to parse YAML in config file '%s': %s\nUsing default configuration values\n",
configFile,
err,
)
}
return newConfig
}