Skip to content

Commit abde2e1

Browse files
committed
Config
1 parent 8810a8f commit abde2e1

File tree

5 files changed

+89
-53
lines changed

5 files changed

+89
-53
lines changed

cmd/root.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ var rootCmd = &cobra.Command{
4343
if verbose {
4444
log.SetLevel(log.DebugLevel)
4545
} else {
46-
log.SetLevel(log.InfoLevel)
46+
// log.SetLevel(log.InfoLevel)
47+
log.SetLevel(log.DebugLevel)
4748
}
4849
},
4950
PersistentPostRun: func(cmd *cobra.Command, args []string) {
@@ -52,9 +53,9 @@ var rootCmd = &cobra.Command{
5253
Run: func(cmd *cobra.Command, args []string) {
5354
log.Info("Saido is running ...")
5455

55-
cfg := config.GetConfig()
56-
log.Infof("%v", cfg)
57-
// charts.Main()
56+
_ = config.GetConfig()
57+
// log.Infof("%v", cfg)
58+
// charts.Main()
5859
},
5960
}
6061

config.example.yaml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ hosts:
44
type: ssh
55
username: root
66
password: somethingSecret
7-
x.example.net:
8-
y.example.net:
9-
z.example.net:
10-
"192.0.1.5":
11-
alias: home-server
12-
connection:
13-
type: ssh
14-
username: root
15-
password: somethingSecret
16-
port: 33
17-
"192.0.1.4":
18-
connection:
19-
type: local
20-
eu-west1:
21-
connection:
22-
type: ssh
23-
privateKeyPath: /path/to/private/key
24-
port: 2222
25-
children:
26-
hosts:
7+
children:
8+
x.example.net:
9+
y.example.net:
10+
z.example.net:
11+
"192.0.1.5":
12+
alias: home-server
13+
connection:
14+
type: ssh
15+
username: root
16+
password: somethingSecret
17+
port: 33
18+
"192.0.1.4":
19+
connection:
20+
type: local
21+
eu-west1:
22+
connection:
23+
type: ssh
24+
private_key_path: /path/to/private/key
25+
port: 2222
26+
children:
2727
"192.0.10.3":
2828
"192.0.10.5":
2929

config/config.go

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,35 @@
11
package config
22

33
import (
4+
"fmt"
45
"io/ioutil"
5-
"strings"
66

7+
"github.com/mitchellh/mapstructure"
78
log "github.com/sirupsen/logrus"
89

910
"gopkg.in/yaml.v2"
1011
)
1112

1213
var config = &Config{}
14+
var allHosts []Host
1315

1416
type Connection struct {
15-
Type string
16-
Username string
17-
Password string
18-
PrivateKeyPath string
19-
}
20-
21-
// Group represents group specified under children
22-
type Group struct {
23-
Name string
24-
Hosts map[string]*Host
25-
Children map[string]*Group
26-
Connection *Connection
17+
Type string `mapstructure:"type"`
18+
Username string `mapstructure:"type"`
19+
Password string `mapstructure:"type"`
20+
PrivateKeyPath string `mapstructure:"private_key_path"`
21+
Port int32 `mapstructure:"port"`
2722
}
2823

2924
type Host struct {
30-
Alias string
31-
Port int32
32-
Connection *Connection
33-
Hosts map[string]*Host
34-
directGroups map[string]*Group
25+
Address string
26+
Alias string
27+
Connection *Connection
3528
}
3629

3730
type Config struct {
38-
Hosts map[string]interface{} `yaml:"hosts"`
39-
Metrics []string `yaml:"metrics"`
31+
Hosts map[interface{}]interface{} `yaml:"hosts"`
32+
Metrics []string `yaml:"metrics"`
4033
}
4134

4235
func LoadConfig(configPath string) *Config {
@@ -49,14 +42,10 @@ func LoadConfig(configPath string) *Config {
4942
log.Fatalf("error: %v", err)
5043
}
5144

52-
for k, v := range config.Hosts {
53-
// Parser Children
54-
if strings.ToLower(k) == "children" {
55-
56-
} else {
45+
parseConfig("root", "", config.Hosts, &Connection{})
5746

58-
}
59-
log.Info(k, v)
47+
for _, i := range allHosts {
48+
log.Infof("Name: %s, Connection: %+v", i.Address, i.Connection)
6049
}
6150

6251
return config
@@ -70,6 +59,49 @@ func GetConfig() *Config {
7059
return config
7160
}
7261

73-
func parseHosts(h interface{}) []*Host {
74-
return []*Host{}
62+
func parseConnection(conn map[interface{}]interface{}) *Connection {
63+
var c Connection
64+
mapstructure.Decode(conn, &c)
65+
return &c
66+
}
67+
68+
func parseConfig(name string, host string, group map[interface{}]interface{}, currentConnection *Connection) {
69+
currentConn := currentConnection
70+
log.Infof("Loading config for %s and host: %s with Connection: %+v", name, host, currentConn)
71+
isParent := false // Set to true for groups that contain just children data i.e children
72+
if conn, ok := group["connection"]; ok {
73+
v, ok := conn.(map[interface{}]interface{})
74+
if !ok {
75+
log.Errorf("Failed to parse connection for %s", name)
76+
}
77+
78+
currentConn = parseConnection(v)
79+
}
80+
81+
if children, ok := group["children"]; ok {
82+
isParent = true
83+
parsedChildren, ok := children.(map[interface{}]interface{})
84+
if !ok {
85+
log.Fatalf("Failed to parse children of %s", name)
86+
return
87+
}
88+
89+
for k, v := range parsedChildren {
90+
host := make(map[interface{}]interface{})
91+
host, ok := v.(map[interface{}]interface{})
92+
if !ok && v != nil { // some leaf nodes do not contain extra data under
93+
log.Errorf("Failed to parse children of %s", name)
94+
}
95+
parseConfig(fmt.Sprintf("%s:%s", name, k), fmt.Sprintf("%s", k), host, currentConn)
96+
}
97+
}
98+
99+
if !isParent {
100+
newHost := Host{
101+
Address: host,
102+
Connection: currentConn,
103+
}
104+
105+
allHosts = append(allHosts, newHost)
106+
}
75107
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.14
55
require (
66
github.com/kr/pretty v0.2.0 // indirect
77
github.com/melbahja/goph v1.2.1
8+
github.com/mitchellh/mapstructure v1.4.3 // indirect
89
github.com/mum4k/termdash v0.16.0
910
github.com/sirupsen/logrus v1.8.1
1011
github.com/spf13/cobra v1.2.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
200200
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
201201
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
202202
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
203+
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
204+
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
203205
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
204206
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
205207
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=

0 commit comments

Comments
 (0)