summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJhon Honce <jhonce@redhat.com>2021-06-21 14:52:41 -0700
committerJhon Honce <jhonce@redhat.com>2021-06-22 11:07:12 -0700
commit1f388ede6f1425ecc6e2a6fddba1d811bc41e2c0 (patch)
treed1dfa4e33924f5ed3ef37660174868692296a0a8
parentbe15e69a6189685ac1afba06cb67013e960065ed (diff)
downloadpodman-1f388ede6f1425ecc6e2a6fddba1d811bc41e2c0.tar.gz
podman-1f388ede6f1425ecc6e2a6fddba1d811bc41e2c0.tar.bz2
podman-1f388ede6f1425ecc6e2a6fddba1d811bc41e2c0.zip
Add --format to connection list
Add support for the --format option to podman system connection list. Signed-off-by: Jhon Honce <jhonce@redhat.com>
-rw-r--r--cmd/podman/system/connection/list.go35
-rw-r--r--docs/source/markdown/podman-system-connection-list.1.md19
-rw-r--r--test/e2e/system_connection_test.go6
3 files changed, 52 insertions, 8 deletions
diff --git a/cmd/podman/system/connection/list.go b/cmd/podman/system/connection/list.go
index b0b15b57d..2f74215c1 100644
--- a/cmd/podman/system/connection/list.go
+++ b/cmd/podman/system/connection/list.go
@@ -1,11 +1,13 @@
package connection
import (
+ "fmt"
"os"
"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/report"
+ "github.com/containers/podman/v3/cmd/podman/common"
"github.com/containers/podman/v3/cmd/podman/registry"
"github.com/containers/podman/v3/cmd/podman/system"
"github.com/containers/podman/v3/cmd/podman/validate"
@@ -14,13 +16,14 @@ import (
var (
listCmd = &cobra.Command{
- Use: "list",
+ Use: "list [options]",
Aliases: []string{"ls"},
Args: validate.NoArgs,
Short: "List destination for the Podman service(s)",
Long: `List destination information for the Podman service(s) in podman configuration`,
Example: `podman system connection list
- podman system connection ls`,
+ podman system connection ls
+ podman system connection ls --format=json`,
ValidArgsFunction: completion.AutocompleteNone,
RunE: list,
TraverseChildren: false,
@@ -32,6 +35,9 @@ func init() {
Command: listCmd,
Parent: system.ConnectionCmd,
})
+
+ listCmd.Flags().String("format", "", "Custom Go template for printing connections")
+ _ = listCmd.RegisterFlagCompletionFunc("format", common.AutocompleteFormat(namedDestination{}))
}
type namedDestination struct {
@@ -39,7 +45,7 @@ type namedDestination struct {
config.Destination
}
-func list(_ *cobra.Command, _ []string) error {
+func list(cmd *cobra.Command, _ []string) error {
cfg, err := config.ReadCustomConfig()
if err != nil {
return err
@@ -71,8 +77,22 @@ func list(_ *cobra.Command, _ []string) error {
rows = append(rows, r)
}
- // TODO: Allow user to override format
- format := "{{range . }}{{.Name}}\t{{.Identity}}\t{{.URI}}\n{{end -}}"
+ format := "{{.Name}}\t{{.Identity}}\t{{.URI}}\n"
+ switch {
+ case report.IsJSON(cmd.Flag("format").Value.String()):
+ buf, err := registry.JSONLibrary().MarshalIndent(rows, "", " ")
+ if err == nil {
+ fmt.Println(string(buf))
+ }
+ return err
+ default:
+ if cmd.Flag("format").Changed {
+ format = cmd.Flag("format").Value.String()
+ format = report.NormalizeFormat(format)
+ }
+ }
+ format = report.EnforceRange(format)
+
tmpl, err := report.NewTemplate("list").Parse(format)
if err != nil {
return err
@@ -84,6 +104,9 @@ func list(_ *cobra.Command, _ []string) error {
}
defer w.Flush()
- _ = tmpl.Execute(w, hdrs)
+ isTable := report.HasTable(cmd.Flag("format").Value.String())
+ if !cmd.Flag("format").Changed || isTable {
+ _ = tmpl.Execute(w, hdrs)
+ }
return tmpl.Execute(w, rows)
}
diff --git a/docs/source/markdown/podman-system-connection-list.1.md b/docs/source/markdown/podman-system-connection-list.1.md
index f5fb5c8e3..6b25a045d 100644
--- a/docs/source/markdown/podman-system-connection-list.1.md
+++ b/docs/source/markdown/podman-system-connection-list.1.md
@@ -4,13 +4,28 @@
podman\-system\-connection\-list - List the destination for the Podman service(s)
## SYNOPSIS
-**podman system connection list**
+**podman system connection list** [*options*]
-**podman system connection ls**
+**podman system connection ls** [*options*]
## DESCRIPTION
List ssh destination(s) for podman service(s).
+## OPTIONS
+
+#### **--format**=*format*
+
+Change the default output format. This can be of a supported type like 'json' or a Go template.
+Valid placeholders for the Go template listed below:
+
+| **Placeholder** | **Description** |
+| --------------- | ----------------------------------------------------------------------------- |
+| *.Name* | Connection Name/Identifier |
+| *.Identity* | Path to file containing SSH identity |
+| *.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*] |
+
+An asterisk is appended to the default connection.
+
## EXAMPLE
```
$ podman system connection list
diff --git a/test/e2e/system_connection_test.go b/test/e2e/system_connection_test.go
index 4697cf860..7c922a648 100644
--- a/test/e2e/system_connection_test.go
+++ b/test/e2e/system_connection_test.go
@@ -147,6 +147,12 @@ var _ = Describe("podman system connection", func() {
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(session.Out).Should(Say("Name *Identity *URI"))
+
+ cmd = []string{"system", "connection", "list", "--format", "{{.Name}}"}
+ session = podmanTest.Podman(cmd)
+ session.WaitWithDefaultTimeout()
+ Expect(session).Should(Exit(0))
+ Expect(session.OutputToString()).Should(Equal("devl* qe"))
})
It("failed default", func() {