Print only the information present in pretty on json

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-09-30 21:43:06 +02:00
parent 604cf0c911
commit 6c883dc4a5
6 changed files with 151 additions and 24 deletions

View File

@ -26,6 +26,7 @@ import (
"github.com/spf13/pflag"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/formatter"
)
@ -56,9 +57,26 @@ func runList(ctx context.Context, opts composeOptions) error {
return err
}
return formatter.Print(stackList, opts.Format, os.Stdout, func(w io.Writer) {
for _, stack := range stackList {
view := viewFromStackList(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)
}
}, "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
}

View File

@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/formatter"
)
@ -58,11 +59,34 @@ func runPs(ctx context.Context, opts composeOptions) error {
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) {
for _, service := range serviceList {
fmt.Fprintf(w, "%s\t%s\t%d/%d\t%s\n", service.ID, service.Name, service.Replicas, service.Desired, strings.Join(service.Ports, ", "))
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, ", "))
}
},
"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
}

View File

@ -97,20 +97,17 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
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) {
for _, c := range contexts {
contextName := c.Name
if c.Name == currentContext {
contextName += " *"
}
for _, c := range view {
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
contextName,
c.Type(),
c.Metadata.Description,
getEndpoint("docker", c.Endpoints),
getEndpoint("kubernetes", c.Endpoints),
c.Metadata.StackOrchestrator)
c.Name,
c.Type,
c.Description,
c.DockerEndpoint,
c.KubernetesEndpoint,
c.Orchestrator)
}
},
"NAME", "TYPE", "DESCRIPTION", "DOCKER ENDPOINT", "KUBERNETES ENDPOINT", "ORCHESTRATOR")
@ -133,3 +130,31 @@ func getEndpoint(name string, meta map[string]interface{}) string {
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
}

View File

@ -92,10 +92,11 @@ func runPs(ctx context.Context, opts psOpts) error {
opts.format = formatter2.JSON
}
return formatter2.Print(containerList, opts.format, os.Stdout, func(w io.Writer) {
for _, c := range containerList {
view := viewFromContainerList(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,
strings.Join(formatter.PortsToStrings(c.Ports, fqdn(c)), ", "))
strings.Join(c.Ports, ", "))
}
}, "CONTAINER ID", "IMAGE", "COMMAND", "STATUS", "PORTS")
}
@ -107,3 +108,25 @@ func fqdn(container containers.Container) string {
}
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
}

View File

@ -123,8 +123,9 @@ func listSecrets() *cobra.Command {
if err != nil {
return err
}
return formatter.Print(secretsList, opts.format, os.Stdout, func(w io.Writer) {
for _, secret := range secretsList {
view := viewFromSecretList(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)
}
}, "ID", "NAME", "DESCRIPTION")
@ -134,6 +135,24 @@ func listSecrets() *cobra.Command {
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 {
recover bool
}

View File

@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/volumes"
"github.com/docker/compose-cli/formatter"
)
@ -46,8 +47,9 @@ func listVolume() *cobra.Command {
if err != nil {
return err
}
return formatter.Print(vols, opts.format, os.Stdout, func(w io.Writer) {
for _, vol := range vols {
view := viewFromVolumeList(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)
}
}, "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)")
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
}