cmd/version.go
changeset 27 f4af03fd34a5
parent 26 530bd59bae97
child 33 f0b899ce915c
--- a/cmd/version.go	Fri Apr 28 22:00:07 2017 +0200
+++ b/cmd/version.go	Sat Apr 29 13:28:05 2017 +0200
@@ -6,22 +6,46 @@
 package cmd
 
 import (
-	"fmt"
-
 	"github.com/spf13/cobra"
 
 	"github.com/McKael/madon"
+	"github.com/McKael/madonctl/printer"
 )
 
+// madonctlVersion contains the version of the madonctl tool
+// and the version of the madon library it is linked with.
+type madonctlVersion struct {
+	AppName      string `json:"application_name"`
+	Version      string `json:"version"`
+	MadonVersion string `json:"madon_version"`
+}
+
 // VERSION of the madonctl application
 var VERSION = "0.3.1"
 
 var versionCmd = &cobra.Command{
 	Use:   "version",
 	Short: "Display " + AppName + " version",
-	Run: func(cmd *cobra.Command, args []string) {
-		fmt.Printf("This is %s version %s (using madon library version %s).\n",
-			AppName, VERSION, madon.MadonVersion)
+	RunE: func(cmd *cobra.Command, args []string) error {
+		const versionTemplate = `This is {{.application_name}} ` +
+			`version {{.version}} ` +
+			`(using madon library version {{.madon_version}}).{{"\n"}}`
+		var v = madonctlVersion{
+			AppName:      AppName,
+			Version:      VERSION,
+			MadonVersion: madon.MadonVersion,
+		}
+		var p printer.ResourcePrinter
+		var err error
+		if getOutputFormat() == "plain" {
+			p, err = printer.NewPrinterTemplate(versionTemplate)
+		} else {
+			p, err = getPrinter()
+		}
+		if err != nil {
+			return err
+		}
+		return p.PrintObj(v, nil, "")
 	},
 }