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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  	"text/tabwriter"
     9  	"time"
    10  
    11  	"github.com/containers/buildah/pkg/formats"
    12  	"github.com/containers/libpod/cmd/podman/cliconfig"
    13  	"github.com/containers/libpod/libpod/define"
    14  	"github.com/containers/libpod/pkg/adapter"
    15  	"github.com/pkg/errors"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  var (
    20  	versionCommand  cliconfig.VersionValues
    21  	_versionCommand = &cobra.Command{
    22  		Use:   "version",
    23  		Args:  noSubArgs,
    24  		Short: "Display the Podman Version Information",
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			versionCommand.InputArgs = args
    27  			versionCommand.GlobalFlags = MainGlobalOpts
    28  			versionCommand.Remote = remoteclient
    29  			return versionCmd(&versionCommand)
    30  		},
    31  	}
    32  )
    33  
    34  func init() {
    35  	versionCommand.Command = _versionCommand
    36  	versionCommand.SetUsageTemplate(UsageTemplate())
    37  	flags := versionCommand.Flags()
    38  	flags.StringVarP(&versionCommand.Format, "format", "f", "", "Change the output format to JSON or a Go template")
    39  }
    40  func getRemoteVersion(c *cliconfig.VersionValues) (version define.Version, err error) {
    41  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    42  	if err != nil {
    43  		return version, errors.Wrapf(err, "could not get runtime")
    44  	}
    45  	defer runtime.DeferredShutdown(false)
    46  
    47  	return runtime.GetVersion()
    48  }
    49  
    50  type versionStruct struct {
    51  	Client define.Version
    52  	Server define.Version
    53  }
    54  
    55  // versionCmd gets and prints version info for version command
    56  func versionCmd(c *cliconfig.VersionValues) error {
    57  
    58  	var (
    59  		v   versionStruct
    60  		err error
    61  	)
    62  	v.Client, err = define.GetVersion()
    63  	if err != nil {
    64  		return errors.Wrapf(err, "unable to determine version")
    65  	}
    66  	if remote {
    67  		v.Server, err = getRemoteVersion(c)
    68  		if err != nil {
    69  			return err
    70  		}
    71  	} else {
    72  		v.Server = v.Client
    73  	}
    74  
    75  	versionOutputFormat := c.Format
    76  	if versionOutputFormat != "" {
    77  		if strings.Join(strings.Fields(versionOutputFormat), "") == "{{json.}}" {
    78  			versionOutputFormat = formats.JSONString
    79  		}
    80  		var out formats.Writer
    81  		switch versionOutputFormat {
    82  		case formats.JSONString:
    83  			out = formats.JSONStruct{Output: v}
    84  			return out.Out()
    85  		default:
    86  			out = formats.StdoutTemplate{Output: v, Template: versionOutputFormat}
    87  			err := out.Out()
    88  			if err != nil {
    89  				// On Failure, assume user is using older version of podman version --format and check client
    90  				out = formats.StdoutTemplate{Output: v.Client, Template: versionOutputFormat}
    91  				if err1 := out.Out(); err1 != nil {
    92  					return err
    93  				}
    94  			}
    95  		}
    96  		return nil
    97  	}
    98  	w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
    99  	defer w.Flush()
   100  
   101  	if remote {
   102  		if _, err := fmt.Fprintf(w, "Client:\n"); err != nil {
   103  			return err
   104  		}
   105  		formatVersion(w, v.Client)
   106  		if _, err := fmt.Fprintf(w, "\nServer:\n"); err != nil {
   107  			return err
   108  		}
   109  		formatVersion(w, v.Server)
   110  	} else {
   111  		formatVersion(w, v.Client)
   112  	}
   113  	return nil
   114  }
   115  
   116  func formatVersion(writer io.Writer, version define.Version) {
   117  	fmt.Fprintf(writer, "Version:\t%s\n", version.Version)
   118  	fmt.Fprintf(writer, "RemoteAPI Version:\t%d\n", version.RemoteAPIVersion)
   119  	fmt.Fprintf(writer, "Go Version:\t%s\n", version.GoVersion)
   120  	if version.GitCommit != "" {
   121  		fmt.Fprintf(writer, "Git Commit:\t%s\n", version.GitCommit)
   122  	}
   123  	// Prints out the build time in readable format
   124  	if version.Built != 0 {
   125  		fmt.Fprintf(writer, "Built:\t%s\n", time.Unix(version.Built, 0).Format(time.ANSIC))
   126  	}
   127  
   128  	fmt.Fprintf(writer, "OS/Arch:\t%s\n", version.OsArch)
   129  }