Fix docker context ls for retrocompatibility

It writes each context as an independent object line

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-10-01 03:41:55 +02:00
parent 178ac40dba
commit 3e9095a873
5 changed files with 42 additions and 52 deletions

View File

@ -93,21 +93,33 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
return nil return nil
} }
if opts.json { view := viewFromContextList(contexts, currentContext)
opts.format = formatter.JSON
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, formatter.PRETTY, os.Stdout,
return formatter.Print(view, opts.format, os.Stdout,
func(w io.Writer) { func(w io.Writer) {
for _, c := range view { 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", _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
c.Name, contextName,
c.Type, c.Type,
c.Description, c.Description,
c.DockerEndpoint, c.DockerEndpoint,
c.KubernetesEndpoint, c.KubernetesEndpoint,
c.Orchestrator) c.StackOrchestrator)
} }
}, },
"NAME", "TYPE", "DESCRIPTION", "DOCKER ENDPOINT", "KUBERNETES ENDPOINT", "ORCHESTRATOR") "NAME", "TYPE", "DESCRIPTION", "DOCKER ENDPOINT", "KUBERNETES ENDPOINT", "ORCHESTRATOR")
@ -132,28 +144,26 @@ func getEndpoint(name string, meta map[string]interface{}) string {
} }
type contextView struct { type contextView struct {
Name string Current bool
Type string
Description string Description string
DockerEndpoint string DockerEndpoint string
KubernetesEndpoint string KubernetesEndpoint string
Orchestrator string Type string
Name string
StackOrchestrator string
} }
func viewFromContextList(contextList []*store.DockerContext, currentContext string) []contextView { func viewFromContextList(contextList []*store.DockerContext, currentContext string) []contextView {
retList := make([]contextView, len(contextList)) retList := make([]contextView, len(contextList))
for i, c := range contextList { for i, c := range contextList {
contextName := c.Name
if c.Name == currentContext {
contextName += " *"
}
retList[i] = contextView{ retList[i] = contextView{
Name: contextName, Current: c.Name == currentContext,
Type: c.Type(),
Description: c.Metadata.Description, Description: c.Metadata.Description,
DockerEndpoint: getEndpoint("docker", c.Endpoints), DockerEndpoint: getEndpoint("docker", c.Endpoints),
KubernetesEndpoint: getEndpoint("kubernetes", c.Endpoints), KubernetesEndpoint: getEndpoint("kubernetes", c.Endpoints),
Orchestrator: c.Metadata.StackOrchestrator, Name: c.Name,
Type: c.Type(),
StackOrchestrator: c.Metadata.StackOrchestrator,
} }
} }
return retList return retList

View File

@ -112,8 +112,8 @@ func fqdn(container containers.Container) string {
type containerView struct { type containerView struct {
ID string ID string
Image string Image string
Command string
Status string Status string
Command string
Ports []string Ports []string
} }
@ -123,8 +123,8 @@ func viewFromContainerList(containerList []containers.Container) []containerView
retList[i] = containerView{ retList[i] = containerView{
ID: c.ID, ID: c.ID,
Image: c.Image, Image: c.Image,
Command: c.Command,
Status: c.Status, Status: c.Status,
Command: c.Command,
Ports: formatter.PortsToStrings(c.Ports, fqdn(c)), Ports: formatter.PortsToStrings(c.Ports, fqdn(c)),
} }
} }

View File

@ -30,3 +30,12 @@ func ToStandardJSON(i interface{}) (string, error) {
} }
return string(b), nil 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
}

View File

@ -1,16 +1 @@
[ {"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}
{
"Name": "default",
"Metadata": {
"Description": "Current DOCKER_HOST based configuration",
"StackOrchestrator": "swarm",
"Type": "moby"
},
"Endpoints": {
"docker": {
"Host": "unix:///var/run/docker.sock"
},
"kubernetes": {}
}
}
]

View File

@ -1,30 +1,16 @@
[ [
{ {
"ID": "id", "ID": "id",
"Status": "",
"Image": "nginx", "Image": "nginx",
"Status": "",
"Command": "", "Command": "",
"CPUTime": 0, "Ports": []
"CPULimit": 0,
"MemoryUsage": 0,
"MemoryLimit": 0,
"PidsCurrent": 0,
"PidsLimit": 0,
"Platform": "",
"RestartPolicyCondition": ""
}, },
{ {
"ID": "1234", "ID": "1234",
"Status": "",
"Image": "alpine", "Image": "alpine",
"Status": "",
"Command": "", "Command": "",
"CPUTime": 0, "Ports": []
"CPULimit": 0,
"MemoryUsage": 0,
"MemoryLimit": 0,
"PidsCurrent": 0,
"PidsLimit": 0,
"Platform": "",
"RestartPolicyCondition": ""
} }
] ]