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

     1  package system
     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/podmanV2/registry"
    13  	"github.com/containers/libpod/libpod/define"
    14  	"github.com/containers/libpod/pkg/domain/entities"
    15  	"github.com/pkg/errors"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  var (
    20  	versionCommand = &cobra.Command{
    21  		Use:               "version",
    22  		Args:              cobra.NoArgs,
    23  		Short:             "Display the Podman Version Information",
    24  		RunE:              version,
    25  		PersistentPreRunE: preRunE,
    26  	}
    27  	versionFormat string
    28  )
    29  
    30  type versionStruct struct {
    31  	Client define.Version
    32  	Server define.Version
    33  }
    34  
    35  func init() {
    36  	registry.Commands = append(registry.Commands, registry.CliCommand{
    37  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    38  		Command: versionCommand,
    39  	})
    40  	flags := versionCommand.Flags()
    41  	flags.StringVarP(&versionFormat, "format", "f", "", "Change the output format to JSON or a Go template")
    42  }
    43  
    44  func version(cmd *cobra.Command, args []string) error {
    45  	var (
    46  		v   versionStruct
    47  		err error
    48  	)
    49  	v.Client, err = define.GetVersion()
    50  	if err != nil {
    51  		return errors.Wrapf(err, "unable to determine version")
    52  	}
    53  	// TODO we need to discuss how to implement
    54  	// this more. current endpoints dont have a
    55  	// version endpoint.  maybe we use info?
    56  	//if remote {
    57  	//	v.Server, err = getRemoteVersion(c)
    58  	//	if err != nil {
    59  	//		return err
    60  	//	}
    61  	//} else {
    62  	v.Server = v.Client
    63  	//}
    64  
    65  	versionOutputFormat := versionFormat
    66  	if versionOutputFormat != "" {
    67  		if strings.Join(strings.Fields(versionOutputFormat), "") == "{{json.}}" {
    68  			versionOutputFormat = formats.JSONString
    69  		}
    70  		var out formats.Writer
    71  		switch versionOutputFormat {
    72  		case formats.JSONString:
    73  			out = formats.JSONStruct{Output: v}
    74  			return out.Out()
    75  		default:
    76  			out = formats.StdoutTemplate{Output: v, Template: versionOutputFormat}
    77  			err := out.Out()
    78  			if err != nil {
    79  				// On Failure, assume user is using older version of podman version --format and check client
    80  				out = formats.StdoutTemplate{Output: v.Client, Template: versionOutputFormat}
    81  				if err1 := out.Out(); err1 != nil {
    82  					return err
    83  				}
    84  			}
    85  		}
    86  		return nil
    87  	}
    88  	w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
    89  	defer w.Flush()
    90  
    91  	if registry.IsRemote() {
    92  		if _, err := fmt.Fprintf(w, "Client:\n"); err != nil {
    93  			return err
    94  		}
    95  		formatVersion(w, v.Client)
    96  		if _, err := fmt.Fprintf(w, "\nServer:\n"); err != nil {
    97  			return err
    98  		}
    99  		formatVersion(w, v.Server)
   100  	} else {
   101  		formatVersion(w, v.Client)
   102  	}
   103  	return nil
   104  }
   105  
   106  func formatVersion(writer io.Writer, version define.Version) {
   107  	fmt.Fprintf(writer, "Version:\t%s\n", version.Version)
   108  	fmt.Fprintf(writer, "RemoteAPI Version:\t%d\n", version.RemoteAPIVersion)
   109  	fmt.Fprintf(writer, "Go Version:\t%s\n", version.GoVersion)
   110  	if version.GitCommit != "" {
   111  		fmt.Fprintf(writer, "Git Commit:\t%s\n", version.GitCommit)
   112  	}
   113  	// Prints out the build time in readable format
   114  	if version.Built != 0 {
   115  		fmt.Fprintf(writer, "Built:\t%s\n", time.Unix(version.Built, 0).Format(time.ANSIC))
   116  	}
   117  
   118  	fmt.Fprintf(writer, "OS/Arch:\t%s\n", version.OsArch)
   119  }