-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
66 lines (56 loc) · 1.17 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
package main
import (
"errors"
"io/ioutil"
"os"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
)
// Config contains the Cassandra configuration required for the
// cqllock tools to run.
type Config struct {
Seeds []string
CertPath string
KeyPath string
Username string
Password string
Keyspace string
Table string
Timeout int
Retries int
}
var configFiles = []string{"~/.cqllockrc", "/etc/cqllock.yaml"}
// ParseConfig parses the cqllock config file into a Config object.
func parseConfig() *Config {
config := Config{}
path, err := configPath()
if err != nil {
log.Fatal(err)
}
contents, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
if err := yaml.Unmarshal(contents, &config); err != nil {
log.Fatal(err)
}
return &config
}
func expandHome(path string) (ret string) {
ret, err := homedir.Expand(path)
if err != nil {
log.Fatal(err)
}
return
}
func configPath() (path string, err error) {
for _, path = range configFiles {
path = expandHome(path)
if _, err = os.Stat(path); os.IsNotExist(err) {
continue
}
return path, nil
}
return "", errors.New("no config file found")
}