mirror of
				https://github.com/docker/compose.git
				synced 2025-11-03 21:25:21 +01:00 
			
		
		
		
	Print only the information present in pretty on json
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
		
							parent
							
								
									604cf0c911
								
							
						
					
					
						commit
						6c883dc4a5
					
				@ -26,6 +26,7 @@ import (
 | 
				
			|||||||
	"github.com/spf13/pflag"
 | 
						"github.com/spf13/pflag"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/docker/compose-cli/api/client"
 | 
						"github.com/docker/compose-cli/api/client"
 | 
				
			||||||
 | 
						"github.com/docker/compose-cli/api/compose"
 | 
				
			||||||
	"github.com/docker/compose-cli/formatter"
 | 
						"github.com/docker/compose-cli/formatter"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -56,9 +57,26 @@ func runList(ctx context.Context, opts composeOptions) error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return formatter.Print(stackList, opts.Format, os.Stdout, func(w io.Writer) {
 | 
						view := viewFromStackList(stackList)
 | 
				
			||||||
		for _, stack := range stackList {
 | 
						return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
 | 
				
			||||||
 | 
							for _, stack := range view {
 | 
				
			||||||
			_, _ = fmt.Fprintf(w, "%s\t%s\n", stack.Name, stack.Status)
 | 
								_, _ = fmt.Fprintf(w, "%s\t%s\n", stack.Name, stack.Status)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}, "NAME", "STATUS")
 | 
						}, "NAME", "STATUS")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type stackView struct {
 | 
				
			||||||
 | 
						Name   string
 | 
				
			||||||
 | 
						Status string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func viewFromStackList(stackList []compose.Stack) []stackView {
 | 
				
			||||||
 | 
						retList := make([]stackView, len(stackList))
 | 
				
			||||||
 | 
						for i, s := range stackList {
 | 
				
			||||||
 | 
							retList[i] = stackView{
 | 
				
			||||||
 | 
								Name:   s.Name,
 | 
				
			||||||
 | 
								Status: s.Status,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return retList
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -26,6 +26,7 @@ import (
 | 
				
			|||||||
	"github.com/spf13/cobra"
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/docker/compose-cli/api/client"
 | 
						"github.com/docker/compose-cli/api/client"
 | 
				
			||||||
 | 
						"github.com/docker/compose-cli/api/compose"
 | 
				
			||||||
	"github.com/docker/compose-cli/formatter"
 | 
						"github.com/docker/compose-cli/formatter"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -58,11 +59,34 @@ func runPs(ctx context.Context, opts composeOptions) error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return formatter.Print(serviceList, opts.Format, os.Stdout,
 | 
						view := viewFromServiceStatusList(serviceList)
 | 
				
			||||||
 | 
						return formatter.Print(view, opts.Format, os.Stdout,
 | 
				
			||||||
		func(w io.Writer) {
 | 
							func(w io.Writer) {
 | 
				
			||||||
			for _, service := range serviceList {
 | 
								for _, service := range view {
 | 
				
			||||||
				fmt.Fprintf(w, "%s\t%s\t%d/%d\t%s\n", service.ID, service.Name, service.Replicas, service.Desired, strings.Join(service.Ports, ", "))
 | 
									_, _ = fmt.Fprintf(w, "%s\t%s\t%d/%d\t%s\n", service.ID, service.Name, service.Replicas, service.Desired, strings.Join(service.Ports, ", "))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		"ID", "NAME", "REPLICAS", "PORTS")
 | 
							"ID", "NAME", "REPLICAS", "PORTS")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type serviceStatusView struct {
 | 
				
			||||||
 | 
						ID       string
 | 
				
			||||||
 | 
						Name     string
 | 
				
			||||||
 | 
						Replicas int
 | 
				
			||||||
 | 
						Desired  int
 | 
				
			||||||
 | 
						Ports    []string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func viewFromServiceStatusList(serviceStatusList []compose.ServiceStatus) []serviceStatusView {
 | 
				
			||||||
 | 
						retList := make([]serviceStatusView, len(serviceStatusList))
 | 
				
			||||||
 | 
						for i, s := range serviceStatusList {
 | 
				
			||||||
 | 
							retList[i] = serviceStatusView{
 | 
				
			||||||
 | 
								ID:       s.ID,
 | 
				
			||||||
 | 
								Name:     s.Name,
 | 
				
			||||||
 | 
								Replicas: s.Replicas,
 | 
				
			||||||
 | 
								Desired:  s.Desired,
 | 
				
			||||||
 | 
								Ports:    s.Ports,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return retList
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -97,20 +97,17 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
 | 
				
			|||||||
		opts.format = formatter.JSON
 | 
							opts.format = formatter.JSON
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return formatter.Print(contexts, opts.format, os.Stdout,
 | 
						view := viewFromContextList(contexts, currentContext)
 | 
				
			||||||
 | 
						return formatter.Print(view, opts.format, os.Stdout,
 | 
				
			||||||
		func(w io.Writer) {
 | 
							func(w io.Writer) {
 | 
				
			||||||
			for _, c := range contexts {
 | 
								for _, c := range view {
 | 
				
			||||||
				contextName := c.Name
 | 
					 | 
				
			||||||
				if c.Name == currentContext {
 | 
					 | 
				
			||||||
					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",
 | 
				
			||||||
					contextName,
 | 
										c.Name,
 | 
				
			||||||
					c.Type(),
 | 
										c.Type,
 | 
				
			||||||
					c.Metadata.Description,
 | 
										c.Description,
 | 
				
			||||||
					getEndpoint("docker", c.Endpoints),
 | 
										c.DockerEndpoint,
 | 
				
			||||||
					getEndpoint("kubernetes", c.Endpoints),
 | 
										c.KubernetesEndpoint,
 | 
				
			||||||
					c.Metadata.StackOrchestrator)
 | 
										c.Orchestrator)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		"NAME", "TYPE", "DESCRIPTION", "DOCKER ENDPOINT", "KUBERNETES ENDPOINT", "ORCHESTRATOR")
 | 
							"NAME", "TYPE", "DESCRIPTION", "DOCKER ENDPOINT", "KUBERNETES ENDPOINT", "ORCHESTRATOR")
 | 
				
			||||||
@ -133,3 +130,31 @@ func getEndpoint(name string, meta map[string]interface{}) string {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return result
 | 
						return result
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type contextView struct {
 | 
				
			||||||
 | 
						Name               string
 | 
				
			||||||
 | 
						Type               string
 | 
				
			||||||
 | 
						Description        string
 | 
				
			||||||
 | 
						DockerEndpoint     string
 | 
				
			||||||
 | 
						KubernetesEndpoint string
 | 
				
			||||||
 | 
						Orchestrator       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(),
 | 
				
			||||||
 | 
								Description:        c.Metadata.Description,
 | 
				
			||||||
 | 
								DockerEndpoint:     getEndpoint("docker", c.Endpoints),
 | 
				
			||||||
 | 
								KubernetesEndpoint: getEndpoint("kubernetes", c.Endpoints),
 | 
				
			||||||
 | 
								Orchestrator:       c.Metadata.StackOrchestrator,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return retList
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -92,10 +92,11 @@ func runPs(ctx context.Context, opts psOpts) error {
 | 
				
			|||||||
		opts.format = formatter2.JSON
 | 
							opts.format = formatter2.JSON
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return formatter2.Print(containerList, opts.format, os.Stdout, func(w io.Writer) {
 | 
						view := viewFromContainerList(containerList)
 | 
				
			||||||
		for _, c := range containerList {
 | 
						return formatter2.Print(view, opts.format, os.Stdout, func(w io.Writer) {
 | 
				
			||||||
 | 
							for _, c := range view {
 | 
				
			||||||
			_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", c.ID, c.Image, c.Command, c.Status,
 | 
								_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", c.ID, c.Image, c.Command, c.Status,
 | 
				
			||||||
				strings.Join(formatter.PortsToStrings(c.Ports, fqdn(c)), ", "))
 | 
									strings.Join(c.Ports, ", "))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}, "CONTAINER ID", "IMAGE", "COMMAND", "STATUS", "PORTS")
 | 
						}, "CONTAINER ID", "IMAGE", "COMMAND", "STATUS", "PORTS")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -107,3 +108,25 @@ func fqdn(container containers.Container) string {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return fqdn
 | 
						return fqdn
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type containerView struct {
 | 
				
			||||||
 | 
						ID      string
 | 
				
			||||||
 | 
						Image   string
 | 
				
			||||||
 | 
						Command string
 | 
				
			||||||
 | 
						Status  string
 | 
				
			||||||
 | 
						Ports   []string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func viewFromContainerList(containerList []containers.Container) []containerView {
 | 
				
			||||||
 | 
						retList := make([]containerView, len(containerList))
 | 
				
			||||||
 | 
						for i, c := range containerList {
 | 
				
			||||||
 | 
							retList[i] = containerView{
 | 
				
			||||||
 | 
								ID:      c.ID,
 | 
				
			||||||
 | 
								Image:   c.Image,
 | 
				
			||||||
 | 
								Command: c.Command,
 | 
				
			||||||
 | 
								Status:  c.Status,
 | 
				
			||||||
 | 
								Ports:   formatter.PortsToStrings(c.Ports, fqdn(c)),
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return retList
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -123,8 +123,9 @@ func listSecrets() *cobra.Command {
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			return formatter.Print(secretsList, opts.format, os.Stdout, func(w io.Writer) {
 | 
								view := viewFromSecretList(secretsList)
 | 
				
			||||||
				for _, secret := range secretsList {
 | 
								return formatter.Print(view, opts.format, os.Stdout, func(w io.Writer) {
 | 
				
			||||||
 | 
									for _, secret := range view {
 | 
				
			||||||
					_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", secret.ID, secret.Name, secret.Description)
 | 
										_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", secret.ID, secret.Name, secret.Description)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}, "ID", "NAME", "DESCRIPTION")
 | 
								}, "ID", "NAME", "DESCRIPTION")
 | 
				
			||||||
@ -134,6 +135,24 @@ func listSecrets() *cobra.Command {
 | 
				
			|||||||
	return cmd
 | 
						return cmd
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type secretView struct {
 | 
				
			||||||
 | 
						ID          string
 | 
				
			||||||
 | 
						Name        string
 | 
				
			||||||
 | 
						Description string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func viewFromSecretList(secretList []secrets.Secret) []secretView {
 | 
				
			||||||
 | 
						retList := make([]secretView, len(secretList))
 | 
				
			||||||
 | 
						for i, s := range secretList {
 | 
				
			||||||
 | 
							retList[i] = secretView{
 | 
				
			||||||
 | 
								ID:          s.ID,
 | 
				
			||||||
 | 
								Name:        s.Name,
 | 
				
			||||||
 | 
								Description: s.Description,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return retList
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type deleteSecretOptions struct {
 | 
					type deleteSecretOptions struct {
 | 
				
			||||||
	recover bool
 | 
						recover bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,7 @@ import (
 | 
				
			|||||||
	"github.com/spf13/cobra"
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/docker/compose-cli/api/client"
 | 
						"github.com/docker/compose-cli/api/client"
 | 
				
			||||||
 | 
						"github.com/docker/compose-cli/api/volumes"
 | 
				
			||||||
	"github.com/docker/compose-cli/formatter"
 | 
						"github.com/docker/compose-cli/formatter"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -46,8 +47,9 @@ func listVolume() *cobra.Command {
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			return formatter.Print(vols, opts.format, os.Stdout, func(w io.Writer) {
 | 
								view := viewFromVolumeList(vols)
 | 
				
			||||||
				for _, vol := range vols {
 | 
								return formatter.Print(view, opts.format, os.Stdout, func(w io.Writer) {
 | 
				
			||||||
 | 
									for _, vol := range view {
 | 
				
			||||||
					_, _ = fmt.Fprintf(w, "%s\t%s\n", vol.ID, vol.Description)
 | 
										_, _ = fmt.Fprintf(w, "%s\t%s\n", vol.ID, vol.Description)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}, "ID", "DESCRIPTION")
 | 
								}, "ID", "DESCRIPTION")
 | 
				
			||||||
@ -56,3 +58,19 @@ func listVolume() *cobra.Command {
 | 
				
			|||||||
	cmd.Flags().StringVar(&opts.format, "format", formatter.PRETTY, "Format the output. Values: [pretty | json]. (Default: pretty)")
 | 
						cmd.Flags().StringVar(&opts.format, "format", formatter.PRETTY, "Format the output. Values: [pretty | json]. (Default: pretty)")
 | 
				
			||||||
	return cmd
 | 
						return cmd
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type volumeView struct {
 | 
				
			||||||
 | 
						ID          string
 | 
				
			||||||
 | 
						Description string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func viewFromVolumeList(volumeList []volumes.Volume) []volumeView {
 | 
				
			||||||
 | 
						retList := make([]volumeView, len(volumeList))
 | 
				
			||||||
 | 
						for i, v := range volumeList {
 | 
				
			||||||
 | 
							retList[i] = volumeView{
 | 
				
			||||||
 | 
								ID:          v.ID,
 | 
				
			||||||
 | 
								Description: v.Description,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return retList
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user