Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 40a670c

Browse files
committed
Support format option in docker app ls
Using either plain JSON or a go template Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 3328d80 commit 40a670c

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

internal/commands/list.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import (
1212
"github.com/docker/cli/cli"
1313
"github.com/docker/cli/cli/command"
1414
"github.com/docker/cli/cli/config"
15+
"github.com/docker/cli/templates"
1516
units "github.com/docker/go-units"
17+
"github.com/docker/go/canonical/json"
18+
"github.com/pkg/errors"
1619
"github.com/spf13/cobra"
1720
)
1821

@@ -36,25 +39,44 @@ var (
3639
)
3740

3841
func listCmd(dockerCli command.Cli) *cobra.Command {
42+
var template string
3943
cmd := &cobra.Command{
4044
Use: "ls [OPTIONS]",
4145
Short: "List running Apps",
4246
Aliases: []string{"list"},
4347
Args: cli.NoArgs,
4448
RunE: func(cmd *cobra.Command, args []string) error {
45-
return runList(dockerCli)
49+
return runList(dockerCli, template)
4650
},
4751
}
4852

53+
cmd.Flags().StringVarP(&template, "format", "f", "", "Format the output using the given syntax or Go template")
54+
cmd.Flags().SetAnnotation("format", "experimentalCLI", []string{"true"}) //nolint:errcheck
4955
return cmd
5056
}
5157

52-
func runList(dockerCli command.Cli) error {
58+
func runList(dockerCli command.Cli, template string) error {
5359
installations, err := getInstallations(dockerCli.CurrentContext(), config.Dir())
5460
if err != nil {
5561
return err
5662
}
5763

64+
if template == "json" {
65+
bytes, err := json.MarshalIndent(installations, "", " ")
66+
if err != nil {
67+
return errors.Errorf("Failed to marshall json: %s", err)
68+
}
69+
_, err = dockerCli.Out().Write(bytes)
70+
return err
71+
}
72+
if template != "" {
73+
tmpl, err := templates.Parse(template)
74+
if err != nil {
75+
return errors.Errorf("Template parsing error: %s", err)
76+
}
77+
return tmpl.Execute(dockerCli.Out(), installations)
78+
}
79+
5880
w := tabwriter.NewWriter(dockerCli.Out(), 0, 0, 1, ' ', 0)
5981
printHeaders(w)
6082

0 commit comments

Comments
 (0)