From 3e9095a873bd12f963ecb83e2ec44f8a59ddff74 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Thu, 1 Oct 2020 03:41:55 +0200 Subject: [PATCH] Fix docker context ls for retrocompatibility It writes each context as an independent object line Signed-off-by: Ulysses Souza --- cli/cmd/context/ls.go | 42 ++++++++++++------- cli/cmd/ps.go | 4 +- formatter/json.go | 9 ++++ tests/e2e/testdata/ls-out-json.golden | 17 +------- tests/e2e/testdata/ps-out-example-json.golden | 22 ++-------- 5 files changed, 42 insertions(+), 52 deletions(-) diff --git a/cli/cmd/context/ls.go b/cli/cmd/context/ls.go index 5cb865574..5e9cd21da 100644 --- a/cli/cmd/context/ls.go +++ b/cli/cmd/context/ls.go @@ -93,21 +93,33 @@ func runList(cmd *cobra.Command, opts lsOpts) error { return nil } - if opts.json { - opts.format = formatter.JSON + view := viewFromContextList(contexts, currentContext) + + if opts.json || opts.format == formatter.JSON { + for _, l := range view { + outJSON, err := formatter.ToCompressedJSON(l) + if err != nil { + return err + } + _, _ = fmt.Fprintln(os.Stdout, outJSON) + } + return nil } - view := viewFromContextList(contexts, currentContext) - return formatter.Print(view, opts.format, os.Stdout, + return formatter.Print(view, formatter.PRETTY, os.Stdout, func(w io.Writer) { for _, c := range view { + contextName := c.Name + if c.Current { + contextName += " *" + } _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", - c.Name, + contextName, c.Type, c.Description, c.DockerEndpoint, c.KubernetesEndpoint, - c.Orchestrator) + c.StackOrchestrator) } }, "NAME", "TYPE", "DESCRIPTION", "DOCKER ENDPOINT", "KUBERNETES ENDPOINT", "ORCHESTRATOR") @@ -132,28 +144,26 @@ func getEndpoint(name string, meta map[string]interface{}) string { } type contextView struct { - Name string - Type string + Current bool Description string DockerEndpoint string KubernetesEndpoint string - Orchestrator string + Type string + Name string + StackOrchestrator string } func viewFromContextList(contextList []*store.DockerContext, currentContext string) []contextView { retList := make([]contextView, len(contextList)) for i, c := range contextList { - contextName := c.Name - if c.Name == currentContext { - contextName += " *" - } retList[i] = contextView{ - Name: contextName, - Type: c.Type(), + Current: c.Name == currentContext, Description: c.Metadata.Description, DockerEndpoint: getEndpoint("docker", c.Endpoints), KubernetesEndpoint: getEndpoint("kubernetes", c.Endpoints), - Orchestrator: c.Metadata.StackOrchestrator, + Name: c.Name, + Type: c.Type(), + StackOrchestrator: c.Metadata.StackOrchestrator, } } return retList diff --git a/cli/cmd/ps.go b/cli/cmd/ps.go index 77b6fc875..56d6ab720 100644 --- a/cli/cmd/ps.go +++ b/cli/cmd/ps.go @@ -112,8 +112,8 @@ func fqdn(container containers.Container) string { type containerView struct { ID string Image string - Command string Status string + Command string Ports []string } @@ -123,8 +123,8 @@ func viewFromContainerList(containerList []containers.Container) []containerView retList[i] = containerView{ ID: c.ID, Image: c.Image, - Command: c.Command, Status: c.Status, + Command: c.Command, Ports: formatter.PortsToStrings(c.Ports, fqdn(c)), } } diff --git a/formatter/json.go b/formatter/json.go index dc68e50da..a86e3f205 100644 --- a/formatter/json.go +++ b/formatter/json.go @@ -30,3 +30,12 @@ func ToStandardJSON(i interface{}) (string, error) { } return string(b), nil } + +// ToCompressedJSON return a string with the JSON representation of the interface{} +func ToCompressedJSON(i interface{}) (string, error) { + b, err := json.Marshal(i) + if err != nil { + return "", err + } + return string(b), nil +} diff --git a/tests/e2e/testdata/ls-out-json.golden b/tests/e2e/testdata/ls-out-json.golden index 319e7f8ed..34d53cb0a 100644 --- a/tests/e2e/testdata/ls-out-json.golden +++ b/tests/e2e/testdata/ls-out-json.golden @@ -1,16 +1 @@ -[ - { - "Name": "default", - "Metadata": { - "Description": "Current DOCKER_HOST based configuration", - "StackOrchestrator": "swarm", - "Type": "moby" - }, - "Endpoints": { - "docker": { - "Host": "unix:///var/run/docker.sock" - }, - "kubernetes": {} - } - } -] \ No newline at end of file +{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"} diff --git a/tests/e2e/testdata/ps-out-example-json.golden b/tests/e2e/testdata/ps-out-example-json.golden index 34699ba3a..c34e5d21b 100644 --- a/tests/e2e/testdata/ps-out-example-json.golden +++ b/tests/e2e/testdata/ps-out-example-json.golden @@ -1,30 +1,16 @@ [ { "ID": "id", - "Status": "", "Image": "nginx", + "Status": "", "Command": "", - "CPUTime": 0, - "CPULimit": 0, - "MemoryUsage": 0, - "MemoryLimit": 0, - "PidsCurrent": 0, - "PidsLimit": 0, - "Platform": "", - "RestartPolicyCondition": "" + "Ports": [] }, { "ID": "1234", - "Status": "", "Image": "alpine", + "Status": "", "Command": "", - "CPUTime": 0, - "CPULimit": 0, - "MemoryUsage": 0, - "MemoryLimit": 0, - "PidsCurrent": 0, - "PidsLimit": 0, - "Platform": "", - "RestartPolicyCondition": "" + "Ports": [] } ] \ No newline at end of file