Add --format to pod inspect

Signed-off-by: Boaz Shuster <boaz.shuster.github@gmail.com>
This commit is contained in:
Boaz Shuster
2020-05-20 16:23:26 +03:00
parent 1077d2d0b7
commit 1f8a78747a
5 changed files with 75 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/containers/buildah/pkg/formats"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -36,6 +37,7 @@ func init() {
}) })
flags := inspectCmd.Flags() flags := inspectCmd.Flags()
flags.BoolVarP(&inspectOptions.Latest, "latest", "l", false, "Act on the latest pod podman is aware of") 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() { if registry.IsRemote() {
_ = flags.MarkHidden("latest") _ = flags.MarkHidden("latest")
} }
@ -54,10 +56,11 @@ func inspect(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
b, err := json.MarshalIndent(responses, "", " ") var data interface{} = responses
if err != nil { var out formats.Writer = formats.JSONStruct{Output: data}
return err if inspectOptions.Format != "json" {
out = formats.StdoutTemplate{Output: data, Template: inspectOptions.Format}
} }
fmt.Println(string(b))
return nil return out.Out()
} }

View File

@ -3345,6 +3345,18 @@ _podman_pod_unpause() {
esac esac
} }
_podman_pod_inspect() {
local options_with_args="
--format
-f
--latest
-l
"
_complete_ "$options_with_args"
}
_podman_pod() { _podman_pod() {
local boolean_options=" local boolean_options="
--help --help
@ -3362,6 +3374,7 @@ _podman_pod() {
stop stop
top top
unpause unpause
inspect
" "
local aliases=" local aliases="
list list

View File

@ -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. 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 ## EXAMPLE
``` ```
# podman pod inspect foobar # podman pod inspect foobar
{ {
"Config": {
"id": "3513ca70583dd7ef2bac83331350f6b6c47d7b4e526c908e49d89ebf720e4693", "Id": "3513ca70583dd7ef2bac83331350f6b6c47d7b4e526c908e49d89ebf720e4693",
"name": "foobar", "Name": "foobar",
"labels": {}, "Labels": {},
"cgroupParent": "/libpod_parent", "CgroupParent": "/libpod_parent",
"UsePodCgroup": true, "CreateCgroup": true,
"created": "2018-08-08T11:15:18.823115347-05:00" "Created": "2018-08-08T11:15:18.823115347-05:00"
}, "State": "created",
"State": { "Hostname": "",
"CgroupPath": "" "SharedNamespaces": [
}, "uts",
"ipc",
"net"
]
"CreateInfra": false,
"InfraContainerID": "1020dd70583dd7ff2bac83331350f6b6e007de0d026c908e49d89ebf891d4699"
"CgroupPath": ""
"Containers": [ "Containers": [
{ {
"id": "d53f8bf1e9730281264aac6e6586e327429f62c704abea4b6afb5d8a2b2c9f2c", "id": "d53f8bf1e9730281264aac6e6586e327429f62c704abea4b6afb5d8a2b2c9f2c",

View File

@ -184,6 +184,8 @@ type PodInspectOptions struct {
// Options for the API. // Options for the API.
NameOrID string NameOrID string
Format string
} }
type PodInspectReport struct { type PodInspectReport struct {

View File

@ -170,4 +170,16 @@ function random_ip() {
is "$output" ".*options $dns_opt" "--dns-opt was added" 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 # vim: filetype=sh