version

package module
v0.0.0-...-fee8dd1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2018 License: Apache-2.0 Imports: 2 Imported by: 0

README

= 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:

[source,go]
----
package main

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

var (
	shortened  = false
	version    = "dev"
	commit     = "none"
	date       = "unknown"
	versionCmd = &cobra.Command{
		Use:   "version",
		Short: "Version will output the current build information",
		Long:  ``,
		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
		},
	}
)

func init() {
	versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Use shortened output for version information.")
	rootCmd.AddCommand(versionCmd)
}
----

When you do this then you can pass in these flags at build time.

[source,shell]
----
go build -ldflags "-X main.commit=<SOMEHASH> -X main.date=<SOMEDATE>"
----

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

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

Or to make this human readible you could use:

[source,shell]
----
$ ./my-cli -s

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

== Contributing

If you want to contribute check out
https://github.com/christopherhein/go-version/blob/master/CONTRIBUTING.adoc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	Version string `json:"Version,omitempty"`
	Commit  string `json:"Commit,omitempty"`
	Date    string `json:"Date,omitempty"`
}

Info creates a formattable struct for output

func New

func New(version string, commit string, date string) *Info

New will create a pointer to a new version object

func (*Info) ToJSON

func (v *Info) ToJSON() string

ToJSON converts the Info into a JSON String

func (*Info) ToShortened

func (v *Info) ToShortened() (str string)

ToShortened converts the Info into a JSON String

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL