-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
54 lines (44 loc) · 1.05 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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
// Config - Base Workable Bot Configuration
type Config struct {
ConfigPath string `yaml:"-"`
GroupChannels map[string]GroupChannel `yaml:"group_channels"`
UserStatuses map[string]int `yaml:"user_statuses"`
}
type GroupChannel struct {
SlackChannel string `yaml:"slack_channel"`
SlackChannelID string `yaml:"slack_channel_id"`
}
func (c *Config) LoadConfig(fs fileSystem) error {
err := c.ValidateConfigPath(fs)
if err != nil {
return err
}
file, err := fs.Open(c.ConfigPath)
if err != nil {
return err
}
defer file.Close()
decodedFile := yaml.NewDecoder(file)
if err := decodedFile.Decode(c); err != nil {
return err
}
return nil
}
func (c *Config) ValidateConfigPath(fs fileSystem) error {
s, err := fs.Stat(c.ConfigPath)
if err != nil {
return err
}
if s.Size() == 0 {
return fmt.Errorf("'%s' configuration file is empty.", c.ConfigPath)
}
if s.IsDir() {
return fmt.Errorf("'%s' is a directory, not a normal file.", c.ConfigPath)
}
return nil
}