From e877d5804f12ed0442738b7956c9b94efa740de1 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Tue, 17 Feb 2015 15:32:15 -0700 Subject: [PATCH] Implement `rancherctl config get` --- cmd/control/config.go | 48 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/cmd/control/config.go b/cmd/control/config.go index 1d50a797..a7ebb5ee 100644 --- a/cmd/control/config.go +++ b/cmd/control/config.go @@ -3,8 +3,10 @@ package control import ( "fmt" "io/ioutil" + "strings" log "github.com/Sirupsen/logrus" + "gopkg.in/yaml.v2" "github.com/codegangsta/cli" "github.com/rancherio/os/config" @@ -14,8 +16,9 @@ import ( func configSubcommands() []cli.Command { return []cli.Command{ { - Name: "get", - Usage: "get value", + Name: "get", + Usage: "get value", + Action: configGet, }, { Name: "import", @@ -39,12 +42,53 @@ func configSubcommands() []cli.Command { } } +func configGet(c *cli.Context) { + cfg, err := config.LoadConfig() + if err != nil { + log.Fatal(err) + } + + content, err := cfg.Dump() + if err != nil { + log.Fatal(err) + } + + data := make(map[interface{}]interface{}) + yaml.Unmarshal([]byte(content), data) + + arg := c.Args().Get(0) + if arg == "" { + fmt.Println("") + return + } + + parts := strings.Split(arg, ".") + for i, part := range parts { + if val, ok := data[part]; ok { + if i+1 == len(parts) { + fmt.Println(val) + } else { + if newData, ok := val.(map[interface{}]interface{}); ok { + data = newData + } else { + fmt.Println(val) + break + } + } + } else { + fmt.Println("2") + break + } + } +} + func configSave(c *cli.Context) { cfg, err := config.LoadConfig() if err != nil { log.Fatal(err) } + //TODO: why doesn't this work for _, c := range cfg.SystemContainers { container := docker.NewContainer("", &c) if container.Err != nil {