-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
59 lines (54 loc) · 1.42 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"github.com/thiefmaster/controller/apis"
"gopkg.in/yaml.v2"
)
type appConfig struct {
Port string
Foobar apis.HTTPCredentials
NotHub apis.HTTPCredentials
Mattermost apis.MattermostSettings
TubeRemotePort int `yaml:"tubeRemotePort"`
Numlock bool
}
func (c *appConfig) load(path string) error {
log.Printf("loading config file: %s\n", path)
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("could not open config file: %v", err)
}
if err = yaml.UnmarshalStrict(yamlFile, c); err != nil {
return fmt.Errorf("could not parse config file: %v", err)
}
if err := c.validate(); err != nil {
return fmt.Errorf("config invalid: %v", err)
}
return nil
}
func (c *appConfig) validate() error {
if c.Port == "" {
return errors.New("no port specified")
}
if c.Foobar.BaseURL == "" {
return errors.New("no foobar url specified")
}
if c.Mattermost.ServerURL != "" {
if c.Mattermost.AccessToken == "" {
return errors.New("no mattermost token specified")
}
if c.Mattermost.TeamName == "" {
return errors.New("no mattermost team specified")
}
if c.Mattermost.ChannelName == "" {
return errors.New("no mattermost chammel specified")
}
}
if c.TubeRemotePort != 0 && (c.TubeRemotePort < 1024 || c.TubeRemotePort > 65535) {
return errors.New("invalid tuberemote port specified")
}
return nil
}