Skip to content

Commit 1f388ed

Browse files
committed
Add --format to connection list
Add support for the --format option to podman system connection list. Signed-off-by: Jhon Honce <[email protected]>
1 parent be15e69 commit 1f388ed

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

cmd/podman/system/connection/list.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package connection
22

33
import (
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

1517
var (
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

3743
type 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
}

docs/source/markdown/podman-system-connection-list.1.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44
podman\-system\-connection\-list - List the destination for the Podman service(s)
55

66
## SYNOPSIS
7-
**podman system connection list**
7+
**podman system connection list** [*options*]
88

9-
**podman system connection ls**
9+
**podman system connection ls** [*options*]
1010

1111
## DESCRIPTION
1212
List ssh destination(s) for podman service(s).
1313

14+
## OPTIONS
15+
16+
#### **--format**=*format*
17+
18+
Change the default output format. This can be of a supported type like 'json' or a Go template.
19+
Valid placeholders for the Go template listed below:
20+
21+
| **Placeholder** | **Description** |
22+
| --------------- | ----------------------------------------------------------------------------- |
23+
| *.Name* | Connection Name/Identifier |
24+
| *.Identity* | Path to file containing SSH identity |
25+
| *.URI* | URI to podman service. Valid schemes are ssh://[user@]*host*[:port]*Unix domain socket*[?secure=True], unix://*Unix domain socket*, and tcp://localhost[:*port*] |
26+
27+
An asterisk is appended to the default connection.
28+
1429
## EXAMPLE
1530
```
1631
$ podman system connection list

test/e2e/system_connection_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ var _ = Describe("podman system connection", func() {
147147
session.WaitWithDefaultTimeout()
148148
Expect(session).Should(Exit(0))
149149
Expect(session.Out).Should(Say("Name *Identity *URI"))
150+
151+
cmd = []string{"system", "connection", "list", "--format", "{{.Name}}"}
152+
session = podmanTest.Podman(cmd)
153+
session.WaitWithDefaultTimeout()
154+
Expect(session).Should(Exit(0))
155+
Expect(session.OutputToString()).Should(Equal("devl* qe"))
150156
})
151157

152158
It("failed default", func() {

0 commit comments

Comments
 (0)