github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/info.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"html/template"
     6  	"os"
     7  	rt "runtime"
     8  	"strings"
     9  
    10  	"github.com/containers/buildah/pkg/formats"
    11  	"github.com/containers/libpod/cmd/podman/cliconfig"
    12  	"github.com/containers/libpod/libpod/define"
    13  	"github.com/containers/libpod/pkg/adapter"
    14  	"github.com/containers/libpod/version"
    15  	"github.com/pkg/errors"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  var (
    20  	infoCommand cliconfig.InfoValues
    21  
    22  	infoDescription = `Display information pertaining to the host, current storage stats, and build of podman.
    23  
    24    Useful for the user and when reporting issues.
    25  `
    26  	_infoCommand = &cobra.Command{
    27  		Use:   "info",
    28  		Args:  noSubArgs,
    29  		Long:  infoDescription,
    30  		Short: "Display podman system information",
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			infoCommand.InputArgs = args
    33  			infoCommand.GlobalFlags = MainGlobalOpts
    34  			infoCommand.Remote = remoteclient
    35  			return infoCmd(&infoCommand)
    36  		},
    37  		Example: `podman info`,
    38  	}
    39  )
    40  
    41  func init() {
    42  	infoCommand.Command = _infoCommand
    43  	infoCommand.SetHelpTemplate(HelpTemplate())
    44  	infoCommand.SetUsageTemplate(UsageTemplate())
    45  	flags := infoCommand.Flags()
    46  
    47  	flags.BoolVarP(&infoCommand.Debug, "debug", "D", false, "Display additional debug information")
    48  	flags.StringVarP(&infoCommand.Format, "format", "f", "", "Change the output format to JSON or a Go template")
    49  
    50  }
    51  
    52  func infoCmd(c *cliconfig.InfoValues) error {
    53  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    54  	if err != nil {
    55  		return errors.Wrapf(err, "could not get runtime")
    56  	}
    57  	defer runtime.DeferredShutdown(false)
    58  
    59  	i, err := runtime.Info()
    60  	if err != nil {
    61  		return errors.Wrapf(err, "error getting info")
    62  	}
    63  
    64  	info := infoWithExtra{Info: i}
    65  	if runtime.Remote {
    66  		endpoint, err := runtime.RemoteEndpoint()
    67  		if err != nil {
    68  			return err
    69  		}
    70  		info.Remote = getRemote(endpoint)
    71  	}
    72  
    73  	if !runtime.Remote && c.Debug {
    74  		d, err := getDebug()
    75  		if err != nil {
    76  			return err
    77  		}
    78  		info.Debug = d
    79  	}
    80  
    81  	var out formats.Writer
    82  	infoOutputFormat := c.Format
    83  	if strings.Join(strings.Fields(infoOutputFormat), "") == "{{json.}}" {
    84  		infoOutputFormat = formats.JSONString
    85  	}
    86  	switch infoOutputFormat {
    87  	case formats.JSONString:
    88  		out = formats.JSONStruct{Output: info}
    89  	case "":
    90  		out = formats.YAMLStruct{Output: info}
    91  	default:
    92  		tmpl, err := template.New("info").Parse(c.Format)
    93  		if err != nil {
    94  			return err
    95  		}
    96  		err = tmpl.Execute(os.Stdout, info)
    97  		return err
    98  	}
    99  
   100  	return out.Out()
   101  }
   102  
   103  // top-level "debug" info
   104  func getDebug() (*debugInfo, error) {
   105  	v, err := define.GetVersion()
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	return &debugInfo{
   110  		Compiler:      rt.Compiler,
   111  		GoVersion:     rt.Version(),
   112  		PodmanVersion: v.Version,
   113  		GitCommit:     v.GitCommit,
   114  	}, nil
   115  }
   116  
   117  func getRemote(endpoint *adapter.Endpoint) *remoteInfo {
   118  	return &remoteInfo{
   119  		Connection:       endpoint.Connection,
   120  		ConnectionType:   endpoint.Type.String(),
   121  		RemoteAPIVersion: string(version.RemoteAPIVersion),
   122  		PodmanVersion:    version.Version,
   123  		OSArch:           fmt.Sprintf("%s/%s", rt.GOOS, rt.GOARCH),
   124  	}
   125  }
   126  
   127  type infoWithExtra struct {
   128  	*define.Info
   129  	Remote *remoteInfo `json:"remote,omitempty"`
   130  	Debug  *debugInfo  `json:"debug,omitempty"`
   131  }
   132  
   133  type remoteInfo struct {
   134  	Connection       string `json:"connection"`
   135  	ConnectionType   string `json:"connectionType"`
   136  	RemoteAPIVersion string `json:"remoteAPIVersion"`
   137  	PodmanVersion    string `json:"podmanVersion"`
   138  	OSArch           string `json:"OSArch"`
   139  }
   140  
   141  type debugInfo struct {
   142  	Compiler      string `json:"compiler"`
   143  	GoVersion     string `json:"goVersion"`
   144  	PodmanVersion string `json:"podmanVersion"`
   145  	GitCommit     string `json:"gitCommit"`
   146  }