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
}
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

View File

@ -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)),
}
}

View File

@ -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
}

View File

@ -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": {}
}
}
]
{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}

View File

@ -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": []
}
]