summaryrefslogtreecommitdiff
path: root/cmd/podmanV2/containers/inspect.go
blob: 648289f0be162099efcd931209f335b2512ca424 (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
package containers

import (
	"context"
	"fmt"
	"os"
	"strings"
	"text/template"

	"github.com/containers/libpod/cmd/podmanV2/registry"
	"github.com/containers/libpod/pkg/domain/entities"
	jsoniter "github.com/json-iterator/go"
	"github.com/spf13/cobra"
)

var (
	// podman container _inspect_
	inspectCmd = &cobra.Command{
		Use:     "inspect [flags] CONTAINER",
		Short:   "Display the configuration of a container",
		Long:    `Displays the low-level information on a container identified by name or ID.`,
		PreRunE: preRunE,
		RunE:    inspect,
		Example: `podman container inspect myCtr
  podman container inspect -l --format '{{.Id}} {{.Config.Labels}}'`,
	}
)

var (
	inspectOptions entities.ContainerInspectOptions
)

func init() {
	registry.Commands = append(registry.Commands, registry.CliCommand{
		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
		Command: inspectCmd,
		Parent:  containerCmd,
	})
	flags := inspectCmd.Flags()
	flags.StringVarP(&inspectOptions.Format, "format", "f", "", "Change the output format to a Go template")
	flags.BoolVarP(&inspectOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
	flags.BoolVarP(&inspectOptions.Size, "size", "s", false, "Display total file size")
	if registry.IsRemote() {
		_ = flags.MarkHidden("latest")
	}
}

func inspect(cmd *cobra.Command, args []string) error {
	responses, err := registry.ContainerEngine().ContainerInspect(context.Background(), args, inspectOptions)
	if err != nil {
		return err
	}
	if inspectOptions.Format == "" {
		b, err := jsoniter.MarshalIndent(responses, "", "  ")
		if err != nil {
			return err
		}
		fmt.Println(string(b))
		return nil
	}
	format := inspectOptions.Format
	if !strings.HasSuffix(format, "\n") {
		format += "\n"
	}
	tmpl, err := template.New("inspect").Parse(format)
	if err != nil {
		return err
	}
	for _, i := range responses {
		if err := tmpl.Execute(os.Stdout, i); err != nil {
			return err
		}
	}
	return nil
}