Skip to content

Commit

Permalink
Merge pull request #10747 from jwhonce/wip/report
Browse files Browse the repository at this point in the history
Add --format to connection list
  • Loading branch information
openshift-merge-robot authored Jun 23, 2021
2 parents d95ff1a + 1f388ed commit e1a7a0e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
35 changes: 29 additions & 6 deletions cmd/podman/system/connection/list.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
Expand All @@ -32,14 +35,17 @@ 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 {
Name string
config.Destination
}

func list(_ *cobra.Command, _ []string) error {
func list(cmd *cobra.Command, _ []string) error {
cfg, err := config.ReadCustomConfig()
if err != nil {
return err
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
19 changes: 17 additions & 2 deletions docs/source/markdown/podman-system-connection-list.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/system_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit e1a7a0e

Please sign in to comment.