github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/cli/version.go (about)

     1  package cli
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"runtime"
     8  
     9  	"github.com/apprenda/kismatic/pkg/install"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  type versionOut struct {
    14  	Version   string
    15  	BuildDate string
    16  	GoVersion string
    17  }
    18  
    19  // NewCmdVersion returns the version command
    20  func NewCmdVersion(buildDate string, out io.Writer) *cobra.Command {
    21  	var outFormat string
    22  	cmd := &cobra.Command{
    23  		Use:   "version",
    24  		Short: "display the Kismatic CLI version",
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			v := versionOut{
    27  				Version:   install.KismaticVersion.String(),
    28  				BuildDate: buildDate,
    29  				GoVersion: runtime.Version(),
    30  			}
    31  			if outFormat == "json" {
    32  				b, err := json.MarshalIndent(v, "", "    ")
    33  				if err != nil {
    34  					return fmt.Errorf("error marshaling data: %v", err)
    35  				}
    36  				fmt.Fprintf(out, string(b))
    37  				return nil
    38  			}
    39  			fmt.Fprintln(out, "Kismatic:")
    40  			fmt.Fprintf(out, "  Version: %s\n", install.KismaticVersion)
    41  			fmt.Fprintf(out, "  Built: %s\n", buildDate)
    42  			fmt.Fprintf(out, "  Go Version: %s\n", runtime.Version())
    43  			return nil
    44  		},
    45  	}
    46  	cmd.Flags().StringVarP(&outFormat, "output", "o", "simple", `output format (options "simple"|"json")`)
    47  	return cmd
    48  }