@@ -12,7 +12,10 @@ import (
12
12
"github.com/docker/cli/cli"
13
13
"github.com/docker/cli/cli/command"
14
14
"github.com/docker/cli/cli/config"
15
+ "github.com/docker/cli/templates"
15
16
units "github.com/docker/go-units"
17
+ "github.com/docker/go/canonical/json"
18
+ "github.com/pkg/errors"
16
19
"github.com/spf13/cobra"
17
20
)
18
21
@@ -36,25 +39,44 @@ var (
36
39
)
37
40
38
41
func listCmd (dockerCli command.Cli ) * cobra.Command {
42
+ var template string
39
43
cmd := & cobra.Command {
40
44
Use : "ls [OPTIONS]" ,
41
45
Short : "List running Apps" ,
42
46
Aliases : []string {"list" },
43
47
Args : cli .NoArgs ,
44
48
RunE : func (cmd * cobra.Command , args []string ) error {
45
- return runList (dockerCli )
49
+ return runList (dockerCli , template )
46
50
},
47
51
}
48
52
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
49
55
return cmd
50
56
}
51
57
52
- func runList (dockerCli command.Cli ) error {
58
+ func runList (dockerCli command.Cli , template string ) error {
53
59
installations , err := getInstallations (dockerCli .CurrentContext (), config .Dir ())
54
60
if err != nil {
55
61
return err
56
62
}
57
63
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
+
58
80
w := tabwriter .NewWriter (dockerCli .Out (), 0 , 0 , 1 , ' ' , 0 )
59
81
printHeaders (w )
60
82
0 commit comments