cmd/version.go
changeset 27 f4af03fd34a5
parent 26 530bd59bae97
child 33 f0b899ce915c
equal deleted inserted replaced
26:530bd59bae97 27:f4af03fd34a5
     4 // Please see the LICENSE file is this directory.
     4 // Please see the LICENSE file is this directory.
     5 
     5 
     6 package cmd
     6 package cmd
     7 
     7 
     8 import (
     8 import (
     9 	"fmt"
       
    10 
       
    11 	"github.com/spf13/cobra"
     9 	"github.com/spf13/cobra"
    12 
    10 
    13 	"github.com/McKael/madon"
    11 	"github.com/McKael/madon"
       
    12 	"github.com/McKael/madonctl/printer"
    14 )
    13 )
       
    14 
       
    15 // madonctlVersion contains the version of the madonctl tool
       
    16 // and the version of the madon library it is linked with.
       
    17 type madonctlVersion struct {
       
    18 	AppName      string `json:"application_name"`
       
    19 	Version      string `json:"version"`
       
    20 	MadonVersion string `json:"madon_version"`
       
    21 }
    15 
    22 
    16 // VERSION of the madonctl application
    23 // VERSION of the madonctl application
    17 var VERSION = "0.3.1"
    24 var VERSION = "0.3.1"
    18 
    25 
    19 var versionCmd = &cobra.Command{
    26 var versionCmd = &cobra.Command{
    20 	Use:   "version",
    27 	Use:   "version",
    21 	Short: "Display " + AppName + " version",
    28 	Short: "Display " + AppName + " version",
    22 	Run: func(cmd *cobra.Command, args []string) {
    29 	RunE: func(cmd *cobra.Command, args []string) error {
    23 		fmt.Printf("This is %s version %s (using madon library version %s).\n",
    30 		const versionTemplate = `This is {{.application_name}} ` +
    24 			AppName, VERSION, madon.MadonVersion)
    31 			`version {{.version}} ` +
       
    32 			`(using madon library version {{.madon_version}}).{{"\n"}}`
       
    33 		var v = madonctlVersion{
       
    34 			AppName:      AppName,
       
    35 			Version:      VERSION,
       
    36 			MadonVersion: madon.MadonVersion,
       
    37 		}
       
    38 		var p printer.ResourcePrinter
       
    39 		var err error
       
    40 		if getOutputFormat() == "plain" {
       
    41 			p, err = printer.NewPrinterTemplate(versionTemplate)
       
    42 		} else {
       
    43 			p, err = getPrinter()
       
    44 		}
       
    45 		if err != nil {
       
    46 			return err
       
    47 		}
       
    48 		return p.PrintObj(v, nil, "")
    25 	},
    49 	},
    26 }
    50 }
    27 
    51 
    28 func init() {
    52 func init() {
    29 	RootCmd.AddCommand(versionCmd)
    53 	RootCmd.AddCommand(versionCmd)