11package connection
22
33import (
4+ "fmt"
45 "os"
56
67 "github.com/containers/common/pkg/completion"
78 "github.com/containers/common/pkg/config"
89 "github.com/containers/common/pkg/report"
10+ "github.com/containers/podman/v3/cmd/podman/common"
911 "github.com/containers/podman/v3/cmd/podman/registry"
1012 "github.com/containers/podman/v3/cmd/podman/system"
1113 "github.com/containers/podman/v3/cmd/podman/validate"
@@ -14,13 +16,14 @@ import (
1416
1517var (
1618 listCmd = & cobra.Command {
17- Use : "list" ,
19+ Use : "list [options] " ,
1820 Aliases : []string {"ls" },
1921 Args : validate .NoArgs ,
2022 Short : "List destination for the Podman service(s)" ,
2123 Long : `List destination information for the Podman service(s) in podman configuration` ,
2224 Example : `podman system connection list
23- podman system connection ls` ,
25+ podman system connection ls
26+ podman system connection ls --format=json` ,
2427 ValidArgsFunction : completion .AutocompleteNone ,
2528 RunE : list ,
2629 TraverseChildren : false ,
@@ -32,14 +35,17 @@ func init() {
3235 Command : listCmd ,
3336 Parent : system .ConnectionCmd ,
3437 })
38+
39+ listCmd .Flags ().String ("format" , "" , "Custom Go template for printing connections" )
40+ _ = listCmd .RegisterFlagCompletionFunc ("format" , common .AutocompleteFormat (namedDestination {}))
3541}
3642
3743type namedDestination struct {
3844 Name string
3945 config.Destination
4046}
4147
42- func list (_ * cobra.Command , _ []string ) error {
48+ func list (cmd * cobra.Command , _ []string ) error {
4349 cfg , err := config .ReadCustomConfig ()
4450 if err != nil {
4551 return err
@@ -71,8 +77,22 @@ func list(_ *cobra.Command, _ []string) error {
7177 rows = append (rows , r )
7278 }
7379
74- // TODO: Allow user to override format
75- format := "{{range . }}{{.Name}}\t {{.Identity}}\t {{.URI}}\n {{end -}}"
80+ format := "{{.Name}}\t {{.Identity}}\t {{.URI}}\n "
81+ switch {
82+ case report .IsJSON (cmd .Flag ("format" ).Value .String ()):
83+ buf , err := registry .JSONLibrary ().MarshalIndent (rows , "" , " " )
84+ if err == nil {
85+ fmt .Println (string (buf ))
86+ }
87+ return err
88+ default :
89+ if cmd .Flag ("format" ).Changed {
90+ format = cmd .Flag ("format" ).Value .String ()
91+ format = report .NormalizeFormat (format )
92+ }
93+ }
94+ format = report .EnforceRange (format )
95+
7696 tmpl , err := report .NewTemplate ("list" ).Parse (format )
7797 if err != nil {
7898 return err
@@ -84,6 +104,9 @@ func list(_ *cobra.Command, _ []string) error {
84104 }
85105 defer w .Flush ()
86106
87- _ = tmpl .Execute (w , hdrs )
107+ isTable := report .HasTable (cmd .Flag ("format" ).Value .String ())
108+ if ! cmd .Flag ("format" ).Changed || isTable {
109+ _ = tmpl .Execute (w , hdrs )
110+ }
88111 return tmpl .Execute (w , rows )
89112}
0 commit comments