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

     1  package system
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"text/template"
     8  
     9  	"github.com/containers/libpod/cmd/podmanV2/registry"
    10  	"github.com/containers/libpod/pkg/domain/entities"
    11  	"github.com/spf13/cobra"
    12  	"gopkg.in/yaml.v2"
    13  )
    14  
    15  var (
    16  	infoDescription = `Display information pertaining to the host, current storage stats, and build of podman.
    17  
    18    Useful for the user and when reporting issues.
    19  `
    20  	infoCommand = &cobra.Command{
    21  		Use:     "info",
    22  		Args:    cobra.NoArgs,
    23  		Long:    infoDescription,
    24  		Short:   "Display podman system information",
    25  		PreRunE: preRunE,
    26  		RunE:    info,
    27  		Example: `podman info`,
    28  	}
    29  )
    30  
    31  var (
    32  	inFormat string
    33  	debug    bool
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: infoCommand,
    40  	})
    41  	flags := infoCommand.Flags()
    42  	flags.BoolVarP(&debug, "debug", "D", false, "Display additional debug information")
    43  	flags.StringVarP(&inFormat, "format", "f", "", "Change the output format to JSON or a Go template")
    44  }
    45  
    46  func info(cmd *cobra.Command, args []string) error {
    47  	info, err := registry.ContainerEngine().Info(registry.GetContext())
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	if inFormat == "json" {
    53  		b, err := json.MarshalIndent(info, "", "  ")
    54  		if err != nil {
    55  			return err
    56  		}
    57  		fmt.Println(string(b))
    58  		return nil
    59  	}
    60  	if !cmd.Flag("format").Changed {
    61  		b, err := yaml.Marshal(info)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		fmt.Println(string(b))
    66  		return nil
    67  	}
    68  	tmpl, err := template.New("info").Parse(inFormat)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	err = tmpl.Execute(os.Stdout, info)
    73  	return err
    74  }