-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
107 lines (89 loc) · 2.33 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"io"
"os"
"path/filepath"
"regexp"
"time"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
xdgbasedir "github.com/zchee/go-xdgbasedir"
"github.com/zchee/go-xdgbasedir/home"
)
var studentIDRegexp = regexp.MustCompile(`(s|m)\d{7}`)
var configPath = flag.String("config", "", `Location to local config file(default "~/.whroom.toml" or "~/.config/whroom/config.toml")`)
type Config struct {
FirebaseURL string
StudentID string
WifiInterface string
Duration time.Duration
}
type ConfigUnmarshaler struct {
FirebaseURL string `toml:"firebase_url"`
StudentID string `toml:"student_id"`
WifiInterface string `toml:"wifi_interface"`
Duration duration `toml:"duration"`
}
type duration time.Duration
func (d *duration) UnmarshalText(data []byte) error {
dur, err := time.ParseDuration(string(data))
if err != nil {
return err
}
*d = duration(dur)
return nil
}
func decodeConfig(r io.Reader) (*Config, error) {
// Set the default values.
cu := &ConfigUnmarshaler{
Duration: duration(1 * time.Minute),
}
if _, err := toml.DecodeReader(r, &cu); err != nil {
return nil, err
}
if cu.FirebaseURL == "" {
return nil, errors.New("the firebase_url field is not set")
}
if !studentIDRegexp.MatchString(cu.StudentID) {
return nil, errors.New("the student_id field has invalid value or empty")
}
return &Config{
FirebaseURL: cu.FirebaseURL,
StudentID: cu.StudentID,
WifiInterface: cu.WifiInterface,
Duration: time.Duration(cu.Duration),
}, nil
}
func getConfig() (*Config, []string, error) {
flag.Parse()
path, ok := getConfigFilePath()
if !ok {
return nil, nil, errors.New("could not find config file.") // TODO: add reference
}
file, err := os.Open(path)
if err != nil {
return nil, nil, errors.Wrapf(err, "could not open %s", path)
}
config, err := decodeConfig(file)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to parse the config file")
}
return config, flag.Args(), nil
}
func getConfigFilePath() (string, bool) {
paths := []string{
filepath.Join(xdgbasedir.ConfigHome(), "whroom", "config.toml"),
filepath.Join(home.Dir(), ".whroom.toml"),
*configPath,
}
for _, path := range paths {
if path == "" {
continue
}
if _, err := os.Stat(path); !os.IsNotExist(err) {
return path, true
}
}
return "", false
}