From 8810a8f0f6fb11e99d8540345572ed9d3793d621 Mon Sep 17 00:00:00 2001 From: Mmadu Manasseh Date: Sat, 12 Feb 2022 09:58:27 +0100 Subject: [PATCH 1/2] Test config --- charts/charts.go | 2 +- cmd/root.go | 1 + config.example.yaml | 12 ++++++------ config/config.go | 4 ++++ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/charts/charts.go b/charts/charts.go index 3756a01..12b940b 100644 --- a/charts/charts.go +++ b/charts/charts.go @@ -456,7 +456,7 @@ const ( tcellTerminal = "tcell" ) -func charts_main() { +func Main() { log.Debug("Starting Saido Main") t, err := tcell.New(tcell.ColorMode(terminalapi.ColorMode256)) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 1269c00..e16f24f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -54,6 +54,7 @@ var rootCmd = &cobra.Command{ cfg := config.GetConfig() log.Infof("%v", cfg) + // charts.Main() }, } diff --git a/config.example.yaml b/config.example.yaml index a6821b3..4921d56 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -17,15 +17,15 @@ hosts: "192.0.1.4": connection: type: local - children: - eu-west1: + eu-west1: + connection: + type: ssh + privateKeyPath: /path/to/private/key + port: 2222 + children: hosts: "192.0.10.3": "192.0.10.5": - connection: - type: ssh - privateKeyPath: /path/to/private/key - port: 2222 metrics: - memory diff --git a/config/config.go b/config/config.go index c70ad8b..86d01ca 100644 --- a/config/config.go +++ b/config/config.go @@ -62,6 +62,10 @@ func LoadConfig(configPath string) *Config { return config } +func (cf *Config) parse() { + +} + func GetConfig() *Config { return config } From abde2e180e4df4ab8a460c6f931c53371d66709a Mon Sep 17 00:00:00 2001 From: Mmadu Manasseh Date: Thu, 17 Feb 2022 11:59:22 +0100 Subject: [PATCH 2/2] Config --- cmd/root.go | 9 +++-- config.example.yaml | 40 ++++++++++---------- config/config.go | 90 ++++++++++++++++++++++++++++++--------------- go.mod | 1 + go.sum | 2 + 5 files changed, 89 insertions(+), 53 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index e16f24f..2ce248c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) { @@ -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() }, } diff --git a/config.example.yaml b/config.example.yaml index 4921d56..4ac8cc9 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -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": diff --git a/config/config.go b/config/config.go index 86d01ca..1afe901 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { @@ -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 @@ -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) + } } diff --git a/go.mod b/go.mod index 77fcce3..fa71f44 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 7f1dabe..a2e7d9a 100644 --- a/go.sum +++ b/go.sum @@ -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=