aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/info.go
blob: 0896e16a42f0a905cd8ea2d80ae28bd98e7c7724 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main

import (
	"fmt"
	rt "runtime"

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

var (
	infoCommand cliconfig.InfoValues

	infoDescription = "Display podman system information"
	_infoCommand    = &cobra.Command{
		Use:   "info",
		Long:  infoDescription,
		Short: `Display information pertaining to the host, current storage stats, and build of podman. Useful for the user and when reporting issues.`,
		RunE: func(cmd *cobra.Command, args []string) error {
			infoCommand.InputArgs = args
			infoCommand.GlobalFlags = MainGlobalOpts
			return infoCmd(&infoCommand)
		},
		Example: "",
	}
)

func init() {
	infoCommand.Command = _infoCommand
	flags := infoCommand.Flags()

	flags.BoolVarP(&infoCommand.Debug, "debug", "D", false, "Display additional debug information")
	flags.StringVarP(&infoCommand.Format, "format", "f", "", "Change the output format to JSON or a Go template")

	rootCmd.AddCommand(infoCommand.Command)
}

func infoCmd(c *cliconfig.InfoValues) error {
	info := map[string]interface{}{}
	remoteClientInfo := map[string]interface{}{}

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

	infoArr, err := runtime.Info()
	if err != nil {
		return errors.Wrapf(err, "error getting info")
	}
	if runtime.Remote {
		remoteClientInfo["RemoteAPI Version"] = version.RemoteAPIVersion
		remoteClientInfo["Podman Version"] = version.Version
		remoteClientInfo["OS Arch"] = fmt.Sprintf("%s/%s", rt.GOOS, rt.GOARCH)
		infoArr = append(infoArr, libpod.InfoData{Type: "client", Data: remoteClientInfo})
	}

	if !runtime.Remote && c.Debug {
		debugInfo := debugInfo(c)
		infoArr = append(infoArr, libpod.InfoData{Type: "debug", Data: debugInfo})
	}

	for _, currInfo := range infoArr {
		info[currInfo.Type] = currInfo.Data
	}

	var out formats.Writer
	infoOutputFormat := c.Format
	switch infoOutputFormat {
	case formats.JSONString:
		out = formats.JSONStruct{Output: info}
	case "":
		out = formats.YAMLStruct{Output: info}
	default:
		out = formats.StdoutTemplate{Output: info, Template: infoOutputFormat}
	}

	formats.Writer(out).Out()

	return nil
}

// top-level "debug" info
func debugInfo(c *cliconfig.InfoValues) map[string]interface{} {
	info := map[string]interface{}{}
	info["compiler"] = rt.Compiler
	info["go version"] = rt.Version()
	info["podman version"] = version.Version
	version, _ := libpod.GetVersion()
	info["git commit"] = version.GitCommit
	return info
}