-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
54 lines (45 loc) · 1.06 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 config
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type (
Config struct {
DiscordToken string `yaml:"discord_token"`
YoutubeKey string `yaml:"youtube_key"`
}
)
// Creates a new config instance, reading from yaml file
func New(path string, c *Config) error {
cfgFile, err := os.Open(path)
if err != nil {
logrus.Errorf("[config.Open] Error opening config: %v", err)
return err
}
defer cfgFile.Close()
// Init new yaml decoder
decoder := yaml.NewDecoder(cfgFile)
err = decoder.Decode(c)
if err != nil {
logrus.Errorf("[config.Decode] Error decoding config: %v", err)
return err
}
return nil
}
// ValidateConfigPath just makes sure, that the path provided is a file,
// that can be read
func ValidateConfigPath(path string) error {
s, err := os.Stat(path)
if err != nil {
logrus.Errorf("[validateConfigPath.Stat] Error: %v", err)
return err
}
if s.IsDir() {
err = fmt.Errorf("'%s' is a directory, not a normal file", path)
logrus.Errorf("[validateConfigPath] Error: %v", err)
return err
}
return nil
}