-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
69 lines (55 loc) · 1.2 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
package main
import (
"flag"
"fmt"
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
Credentail string `yaml:"credential"`
LogFile string `yaml:"logfile"`
SpreadSheet struct {
Id string `yaml:"id"`
ReadRange string `yaml:"readrange"`
} `yaml:"spreadsheet"`
Calendar struct {
Id string `yaml:"id"`
Event string `yaml:"event"`
} `yaml:"calendar"`
Target struct {
URL string `yaml:"url"`
ApiKey string `yaml:"apikey"`
} `yaml:"target"`
}
func NewConfig(configPath string) (*Config, error) {
config := &Config{}
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
func ValidateConfigPath(path string) error {
s, err := os.Stat(path)
if err != nil {
return err
}
if s.IsDir() {
return fmt.Errorf("'%s' is a directory, not a normal file", path)
}
return nil
}
func ParseFlags() (string, error) {
var configPath string
flag.StringVar(&configPath, "config", "./config.yaml", "path to config file")
flag.Parse()
if err := ValidateConfigPath(configPath); err != nil {
return "", err
}
return configPath, nil
}