-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
executable file
·71 lines (60 loc) · 1.49 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
package main
import (
"fmt"
"io/ioutil"
"sync"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
Hosts map[string]HostConfig `yaml:"hosts"`
Groups map[string]HostConfig `yaml:"groups"`
}
type SafeConfig struct {
sync.RWMutex
C *Config
}
type HostConfig struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
func (sc *SafeConfig) ReloadConfig(configFile string) error {
var c = &Config{}
yamlFile, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
if err := yaml.Unmarshal(yamlFile, c); err != nil {
return err
}
sc.Lock()
sc.C = c
sc.Unlock()
return nil
}
func (sc *SafeConfig) HostConfigForTarget(target string) (*HostConfig, error) {
sc.Lock()
defer sc.Unlock()
if hostConfig, ok := sc.C.Hosts[target]; ok {
return &HostConfig{
Username: hostConfig.Username,
Password: hostConfig.Password,
}, nil
}
if hostConfig, ok := sc.C.Hosts["default"]; ok {
return &HostConfig{
Username: hostConfig.Username,
Password: hostConfig.Password,
}, nil
}
return &HostConfig{}, fmt.Errorf("no credentials found for target %s", target)
}
// HostConfigForGroup checks the configuration for a matching group config and returns the configured HostConfig for
// that matched group.
func (sc *SafeConfig) HostConfigForGroup(group string) (*HostConfig, error) {
sc.Lock()
defer sc.Unlock()
if hostConfig, ok := sc.C.Groups[group]; ok {
return &hostConfig, nil
}
return &HostConfig{}, fmt.Errorf("no credentials found for group %s", group)
}