summaryrefslogtreecommitdiff
path: root/cmd/podman/pod_inspect.go
blob: 46ac30c2a431b4283523e078ea9e705f68da372c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main

import (
	"encoding/json"
	"fmt"

	"github.com/containers/libpod/cmd/podman/cliconfig"
	"github.com/containers/libpod/pkg/adapter"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

var (
	podInspectCommand     cliconfig.PodInspectValues
	podInspectDescription = `Display the configuration for a pod by name or id

  By default, this will render all results in a JSON array. If the container and image have the same name, this command returns the container JSON.`

	_podInspectCommand = &cobra.Command{
		Use:   "inspect [flags] POD",
		Short: "Displays a pod configuration",
		Long:  podInspectDescription,
		RunE: func(cmd *cobra.Command, args []string) error {
			podInspectCommand.InputArgs = args
			podInspectCommand.GlobalFlags = MainGlobalOpts
			return podInspectCmd(&podInspectCommand)
		},
		Example: `podman pod inspect podID`,
	}
)

func init() {
	podInspectCommand.Command = _podInspectCommand
	podInspectCommand.SetHelpTemplate(HelpTemplate())
	podInspectCommand.SetUsageTemplate(UsageTemplate())
	flags := podInspectCommand.Flags()
	flags.BoolVarP(&podInspectCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")

	markFlagHiddenForRemoteClient("latest", flags)
}

func podInspectCmd(c *cliconfig.PodInspectValues) error {
	var (
		pod *adapter.Pod
	)
	args := c.InputArgs

	if len(args) < 1 && !c.Latest {
		return errors.Errorf("you must provide the name or id of a pod")
	}

	runtime, err := adapter.GetRuntime(&c.PodmanCommand)
	if err != nil {
		return errors.Wrapf(err, "could not get runtime")
	}
	defer runtime.Shutdown(false)

	if c.Latest {
		pod, err = runtime.GetLatestPod()
		if err != nil {
			return errors.Wrapf(err, "unable to get latest pod")
		}
	} else {
		pod, err = runtime.LookupPod(args[0])
		if err != nil {
			return err
		}
	}

	podInspectData, err := pod.Inspect()
	if err != nil {
		return err
	}
	b, err := json.MarshalIndent(&podInspectData, "", "     ")
	if err != nil {
		return err
	}
	fmt.Println(string(b))
	return nil
}