Skip to content

Commit

Permalink
Config
Browse files Browse the repository at this point in the history
  • Loading branch information
MeNsaaH committed Feb 17, 2022
1 parent 8810a8f commit abde2e1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 53 deletions.
9 changes: 5 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var rootCmd = &cobra.Command{
if verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
// log.SetLevel(log.InfoLevel)
log.SetLevel(log.DebugLevel)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
Expand All @@ -52,9 +53,9 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
log.Info("Saido is running ...")

cfg := config.GetConfig()
log.Infof("%v", cfg)
// charts.Main()
_ = config.GetConfig()
// log.Infof("%v", cfg)
// charts.Main()
},
}

Expand Down
40 changes: 20 additions & 20 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ hosts:
type: ssh
username: root
password: somethingSecret
x.example.net:
y.example.net:
z.example.net:
"192.0.1.5":
alias: home-server
connection:
type: ssh
username: root
password: somethingSecret
port: 33
"192.0.1.4":
connection:
type: local
eu-west1:
connection:
type: ssh
privateKeyPath: /path/to/private/key
port: 2222
children:
hosts:
children:
x.example.net:
y.example.net:
z.example.net:
"192.0.1.5":
alias: home-server
connection:
type: ssh
username: root
password: somethingSecret
port: 33
"192.0.1.4":
connection:
type: local
eu-west1:
connection:
type: ssh
private_key_path: /path/to/private/key
port: 2222
children:
"192.0.10.3":
"192.0.10.5":

Expand Down
90 changes: 61 additions & 29 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
package config

import (
"fmt"
"io/ioutil"
"strings"

"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"

"gopkg.in/yaml.v2"
)

var config = &Config{}
var allHosts []Host

type Connection struct {
Type string
Username string
Password string
PrivateKeyPath string
}

// Group represents group specified under children
type Group struct {
Name string
Hosts map[string]*Host
Children map[string]*Group
Connection *Connection
Type string `mapstructure:"type"`
Username string `mapstructure:"type"`
Password string `mapstructure:"type"`
PrivateKeyPath string `mapstructure:"private_key_path"`
Port int32 `mapstructure:"port"`
}

type Host struct {
Alias string
Port int32
Connection *Connection
Hosts map[string]*Host
directGroups map[string]*Group
Address string
Alias string
Connection *Connection
}

type Config struct {
Hosts map[string]interface{} `yaml:"hosts"`
Metrics []string `yaml:"metrics"`
Hosts map[interface{}]interface{} `yaml:"hosts"`
Metrics []string `yaml:"metrics"`
}

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

for k, v := range config.Hosts {
// Parser Children
if strings.ToLower(k) == "children" {

} else {
parseConfig("root", "", config.Hosts, &Connection{})

}
log.Info(k, v)
for _, i := range allHosts {
log.Infof("Name: %s, Connection: %+v", i.Address, i.Connection)
}

return config
Expand All @@ -70,6 +59,49 @@ func GetConfig() *Config {
return config
}

func parseHosts(h interface{}) []*Host {
return []*Host{}
func parseConnection(conn map[interface{}]interface{}) *Connection {
var c Connection
mapstructure.Decode(conn, &c)
return &c
}

func parseConfig(name string, host string, group map[interface{}]interface{}, currentConnection *Connection) {
currentConn := currentConnection
log.Infof("Loading config for %s and host: %s with Connection: %+v", name, host, currentConn)
isParent := false // Set to true for groups that contain just children data i.e children
if conn, ok := group["connection"]; ok {
v, ok := conn.(map[interface{}]interface{})
if !ok {
log.Errorf("Failed to parse connection for %s", name)
}

currentConn = parseConnection(v)
}

if children, ok := group["children"]; ok {
isParent = true
parsedChildren, ok := children.(map[interface{}]interface{})
if !ok {
log.Fatalf("Failed to parse children of %s", name)
return
}

for k, v := range parsedChildren {
host := make(map[interface{}]interface{})
host, ok := v.(map[interface{}]interface{})
if !ok && v != nil { // some leaf nodes do not contain extra data under
log.Errorf("Failed to parse children of %s", name)
}
parseConfig(fmt.Sprintf("%s:%s", name, k), fmt.Sprintf("%s", k), host, currentConn)
}
}

if !isParent {
newHost := Host{
Address: host,
Connection: currentConn,
}

allHosts = append(allHosts, newHost)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
github.com/kr/pretty v0.2.0 // indirect
github.com/melbahja/goph v1.2.1
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mum4k/termdash v0.16.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down

0 comments on commit abde2e1

Please sign in to comment.