aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/info.go
blob: 79417b85d96359a20591e5c2585138827eb0fcc0 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main

import (
	"fmt"
	"html/template"
	"os"
	rt "runtime"
	"strings"

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

var (
	infoCommand cliconfig.InfoValues

	infoDescription = `Display information pertaining to the host, current storage stats, and build of podman.

  Useful for the user and when reporting issues.
`
	_infoCommand = &cobra.Command{
		Use:   "info",
		Args:  noSubArgs,
		Long:  infoDescription,
		Short: "Display podman system information",
		RunE: func(cmd *cobra.Command, args []string) error {
			infoCommand.InputArgs = args
			infoCommand.GlobalFlags = MainGlobalOpts
			infoCommand.Remote = remoteclient
			return infoCmd(&infoCommand)
		},
		Example: `podman info`,
	}
)

func init() {
	infoCommand.Command = _infoCommand
	infoCommand.SetHelpTemplate(HelpTemplate())
	infoCommand.SetUsageTemplate(UsageTemplate())
	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")

}

func infoCmd(c *cliconfig.InfoValues) error {
	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
	if err != nil {
		return errors.Wrapf(err, "could not get runtime")
	}
	defer runtime.DeferredShutdown(false)

	i, err := runtime.Info()
	if err != nil {
		return errors.Wrapf(err, "error getting info")
	}

	info := infoWithExtra{Info: i}
	if runtime.Remote {
		endpoint, err := runtime.RemoteEndpoint()
		if err != nil {
			return err
		}
		info.Remote = getRemote(endpoint)
	}

	if !runtime.Remote && c.Debug {
		d, err := getDebug()
		if err != nil {
			return err
		}
		info.Debug = d
	}

	var out formats.Writer
	infoOutputFormat := c.Format
	if strings.Join(strings.Fields(infoOutputFormat), "") == "{{json.}}" {
		infoOutputFormat = formats.JSONString
	}
	switch infoOutputFormat {
	case formats.JSONString:
		out = formats.JSONStruct{Output: info}
	case "":
		out = formats.YAMLStruct{Output: info}
	default:
		tmpl, err := template.New("info").Parse(c.Format)
		if err != nil {
			return err
		}
		err = tmpl.Execute(os.Stdout, info)
		return err
	}

	return out.Out()
}

// top-level "debug" info
func getDebug() (*debugInfo, error) {
	v, err := define.GetVersion()
	if err != nil {
		return nil, err
	}
	return &debugInfo{
		Compiler:      rt.Compiler,
		GoVersion:     rt.Version(),
		PodmanVersion: v.Version,
		GitCommit:     v.GitCommit,
	}, nil
}

func getRemote(endpoint *adapter.Endpoint) *remoteInfo {
	return &remoteInfo{
		Connection:       endpoint.Connection,
		ConnectionType:   endpoint.Type.String(),
		RemoteAPIVersion: string(version.RemoteAPIVersion),
		PodmanVersion:    version.Version,
		OSArch:           fmt.Sprintf("%s/%s", rt.GOOS, rt.GOARCH),
	}
}

type infoWithExtra struct {
	*define.Info
	Remote *remoteInfo `json:"remote,omitempty"`
	Debug  *debugInfo  `json:"debug,omitempty"`
}

type remoteInfo struct {
	Connection       string `json:"connection"`
	ConnectionType   string `json:"connectionType"`
	RemoteAPIVersion string `json:"remoteAPIVersion"`
	PodmanVersion    string `json:"podmanVersion"`
	OSArch           string `json:"OSArch"`
}

type debugInfo struct {
	Compiler      string `json:"compiler"`
	GoVersion     string `json:"goVersion"`
	PodmanVersion string `json:"podmanVersion"`
	GitCommit     string `json:"gitCommit"`
}