Skip to content

christopherhein/go-version

Repository files navigation

Go Version

go version

This package gives allows you to use https://github.com/spf13/cobra and https://github.com/goreleaser/goreleaser together to output your version in a simple way. Supporting multiple flags for mutating the output.

Usage

To use this package you will need to create cobra command for version such as this.

package main

import (
	"fmt"
	goVersion "go.hein.dev/go-version"
	"github.com/spf13/cobra"
)

var (
	shortened  = false
	version    = "dev"
	commit     = "none"
	date       = "unknown"
	output     = "json"
	versionCmd = &cobra.Command{
		Use:   "version",
		Short: "Version will output the current build information",
		Long:  ``,
		Run: func(_ *cobra.Command, _ []string) {
			resp := goVersion.FuncWithOutput(shortened, version, commit, date, output)
			fmt.Print(resp)
			return
		},
	}
)

func init() {
	versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number.")
	versionCmd.Flags().StringVarP(&output, "output", "o", "json", "Output format. One of 'yaml' or 'json'.")
	rootCmd.AddCommand(versionCmd)
}

When you do this then you can pass in these flags at build time. _If you’d like more control of the output you can change the Run function to something more like this.

Run: func(_ *cobra.Command, _ []string) {
	var response string
	versionOutput := goVersion.New(version, commit, date)

	if shortened {
		response = versionOutput.ToShortened()
	} else {
		response = versionOutput.ToJSON()
	}
	fmt.Printf("%+v", response)
	return
},
go build -ldflags "-X main.commit=<SOMEHASH> -X main.date=<SOMEDATE>"

This then gives your CLI the ability to use this for the JSON output:

$ ./my-cli version
{"Version":"dev","Commit":"<SOMEHASH>","Date":"<SOMEDATE>"}

Or to make this human readable you could use:

$ ./my-cli -s

Version: dev
Commit: <SOMEHASH>
Date: <SOMEDATE>

Testing

To run the test suite all you need to do is run.

go test -v ./...

Contributing