summaryrefslogtreecommitdiff
path: root/cmd/podman/pod_inspect.go
blob: c7bbf31cde6638b1b1144d06a6a56e5abe236d52 (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
package main

import (
	"encoding/json"

	"fmt"
	"github.com/containers/libpod/cmd/podman/libpodruntime"
	"github.com/containers/libpod/libpod"
	"github.com/pkg/errors"
	"github.com/urfave/cli"
)

var (
	podInspectFlags = []cli.Flag{
		LatestPodFlag,
	}
	podInspectDescription = "Display the configuration for a pod by name or id"
	podInspectCommand     = cli.Command{
		Name:                   "inspect",
		Usage:                  "Displays a pod configuration",
		Description:            podInspectDescription,
		Flags:                  sortFlags(podInspectFlags),
		Action:                 podInspectCmd,
		UseShortOptionHandling: true,
		ArgsUsage:              "[POD_NAME_OR_ID]",
		OnUsageError:           usageErrorHandler,
	}
)

func podInspectCmd(c *cli.Context) error {
	var (
		pod *libpod.Pod
	)
	if err := checkMutuallyExclusiveFlags(c); err != nil {
		return err
	}
	args := c.Args()
	runtime, err := libpodruntime.GetRuntime(c)
	if err != nil {
		return errors.Wrapf(err, "could not get runtime")
	}
	defer runtime.Shutdown(false)

	if c.Bool("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
}