-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
50 lines (44 loc) · 874 Bytes
/
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
package main
import (
"io/ioutil"
"log"
"regexp"
"gopkg.in/gcfg.v1"
)
type Credentials struct {
Username string
Password string
Enable string
}
type config struct {
Device map[string]*struct {
Username string
Password string
Enable string
}
}
func readConfig(cfgfile string) config {
var cfg config
content, err := ioutil.ReadFile(cfgfile)
if err != nil {
log.Fatal(err)
}
err = gcfg.ReadStringInto(&cfg, string(content))
if err != nil {
log.Fatal("Failed to parse "+cfgfile+":", err)
}
return cfg
}
func getCredentials(host string) Credentials {
var cred Credentials
for device, _ := range cfg.Device {
re := regexp.MustCompile(device)
if re.MatchString(host) {
cred.Username = cfg.Device[device].Username
cred.Password = cfg.Device[device].Password
cred.Enable = cfg.Device[device].Enable
break
}
}
return cred
}