cmd/version.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 21 Mar 2018 22:47:40 +0100
changeset 214 78fe649d7fc9
parent 185 564d92b54b00
child 216 3e017cfad3cc
permissions -rw-r--r--
Version 2.3.0

// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net>
//
// Licensed under the MIT license.
// Please see the LICENSE file is this directory.

package cmd

import (
	"os"

	"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 = "2.3.0"

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Display " + AppName + " version",
	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
		of := getOutputFormat()
		if of == "template" {
			p, err = getPrinter()
		} else { // Default
			pOptions := printer.Options{"template": versionTemplate}
			p, err = printer.NewPrinterTemplate(pOptions)
		}
		if err != nil {
			errPrint("Error: %s", err.Error())
			os.Exit(1)
		}
		return p.PrintObj(v, nil, "")
	},
}

func init() {
	RootCmd.AddCommand(versionCmd)
}