Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --format to pod inspect #6372

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions cmd/podman/pods/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/containers/buildah/pkg/formats"
"github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors"
Expand Down Expand Up @@ -36,6 +37,7 @@ func init() {
})
flags := inspectCmd.Flags()
flags.BoolVarP(&inspectOptions.Latest, "latest", "l", false, "Act on the latest pod podman is aware of")
flags.StringVarP(&inspectOptions.Format, "format", "f", "json", "Format the output to a Go template or json")
if registry.IsRemote() {
_ = flags.MarkHidden("latest")
}
Expand All @@ -54,10 +56,11 @@ func inspect(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
b, err := json.MarshalIndent(responses, "", " ")
if err != nil {
return err
var data interface{} = responses
var out formats.Writer = formats.JSONStruct{Output: data}
if inspectOptions.Format != "json" {
out = formats.StdoutTemplate{Output: data, Template: inspectOptions.Format}
}
fmt.Println(string(b))
return nil

return out.Out()
}
13 changes: 13 additions & 0 deletions completions/bash/podman
Original file line number Diff line number Diff line change
Expand Up @@ -3345,6 +3345,18 @@ _podman_pod_unpause() {
esac
}

_podman_pod_inspect() {
local options_with_args="
--format
-f
--latest
-l
"

_complete_ "$options_with_args"
}


_podman_pod() {
local boolean_options="
--help
Expand All @@ -3362,6 +3374,7 @@ _podman_pod() {
stop
top
unpause
inspect
"
local aliases="
list
Expand Down
51 changes: 40 additions & 11 deletions docs/source/markdown/podman-pod-inspect.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,50 @@ to run pods such as CRI-O, the last started pod could be from either of those me

The latest option is not supported on the remote client.

**-f**, **--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 are listed below:

| **Placeholder** | **Description** |
| ----------------- | ----------------------------------------------------------------------------- |
| .ID | Pod ID |
| .Name | Pod name |
| .State | Pod state |
| .Hostname | Pod hostname |
| .Labels | Pod labels |
| .Created | Time when the pod was created |
| .CreateCgroup | Whether cgroup was created |
| .CgroupParent | Pod cgroup parent |
| .CgroupPath | Pod cgroup path |
| .CreateInfra | Whether infrastructure created |
| .InfraContainerID | Pod infrastructure ID |
| .SharedNamespaces | Pod shared namespaces |
| .NumContainers | Number of containers in the pod |
| .Containers | Pod containers |

## EXAMPLE
```
# podman pod inspect foobar
{
"Config": {
"id": "3513ca70583dd7ef2bac83331350f6b6c47d7b4e526c908e49d89ebf720e4693",
"name": "foobar",
"labels": {},
"cgroupParent": "/libpod_parent",
"UsePodCgroup": true,
"created": "2018-08-08T11:15:18.823115347-05:00"
},
"State": {
"CgroupPath": ""
},

"Id": "3513ca70583dd7ef2bac83331350f6b6c47d7b4e526c908e49d89ebf720e4693",
"Name": "foobar",
"Labels": {},
"CgroupParent": "/libpod_parent",
"CreateCgroup": true,
"Created": "2018-08-08T11:15:18.823115347-05:00"
"State": "created",
"Hostname": "",
"SharedNamespaces": [
"uts",
"ipc",
"net"
]
"CreateInfra": false,
"InfraContainerID": "1020dd70583dd7ff2bac83331350f6b6e007de0d026c908e49d89ebf891d4699"
"CgroupPath": ""
"Containers": [
{
"id": "d53f8bf1e9730281264aac6e6586e327429f62c704abea4b6afb5d8a2b2c9f2c",
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/entities/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ type PodInspectOptions struct {

// Options for the API.
NameOrID string

Format string
}

type PodInspectReport struct {
Expand Down
12 changes: 12 additions & 0 deletions test/system/200-pod.bats
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,16 @@ function random_ip() {
is "$output" ".*options $dns_opt" "--dns-opt was added"
}

@test "podman pod inspect - format" {
skip_if_remote "podman-pod does not work with podman-remote"

run_podman pod create --name podtest
podid=$output

run_podman pod inspect --format '-> {{.Name}}: {{.NumContainers}}' podtest
is "$output" "-> podtest: 1"

run_podman pod rm -f podtest
}

# vim: filetype=sh