goversion

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2020 License: Apache-2.0 Imports: 9 Imported by: 3

README

Goversion

Simplified versioning for Go applications.

The project consists out of two parts:

  1. The version library github.com/erwinvaneyk/goversion for providing version info and corresponding operations.
  2. The optional goversion CLI to simplify generating ldflags and versioning fields.

Installation

To use this library in your Go project, all you need to do is in the context of the project:

go get -u github.com/erwinvaneyk/goversion

To install the optional goversion CLI, use one of the following options:

Using a pre-built release to get the goversion CLI

To download a stable release of goversion, see the Github Releases. For example, to download the latest version run the following:

# Change to get an older version.
VERSION=v0.1.2

# Download goversion
curl -L https://github.com/erwinvaneyk/goversion/releases/download/${VERSION}/goversion_$(echo $VERSION | sed 's/v//')_$(uname)_$(uname -m).tar.gz > goversion.tar.gz
tar -xvf goversion.tar.gz
mv ./goversion /usr/local/bin/goversion

# Check if goversion is working
which goversion && goversion version 
Using Go tools to get the goversion CLI

To pin your project to a specific version of goversion, add the following tools.go file to your (module-enabled) Go project:

// +build tools

//go:generate go install github.com/erwinvaneyk/goversion
package main

import (
	_ "github.com/erwinvaneyk/goversion"
)

The first time, run the following to add goversion as dependency to your project:

go get -u github.com/erwinvaneyk/goversion/cmd/goversion

Finally, to install the binary on your path run the go generator on this file:

go generate -tags=tools ./tools.go

Note: ensure that you have added your GOBIN directory to your PATH.

Build the goversion CLI from source

To build and install the CLI from source, clone the repo and run make:

git clone git@github.com:erwinvaneyk/goversion.git
cd goversion

# Builds goversion and stores it in the ./bin directory.
make goversion

# Build and add goversion to your PATH.
# Note: ensure that you have added your `GOBIN` directory to your `PATH`.
make install

Usage

There are two ways to make use of goversion in your project. Both assume that you have added the package to your module or GOPATH. If you haven't:

go get github.com/erwinvaneyk/goversion
Using the imported package.

To use goversion, import and use the package somewhere in your application.
For example:

package main

import (
	"fmt"

	"github.com/erwinvaneyk/goversion"
)

func main() {
	fmt.Println(goversion.Get())
}
# With goversion:
go run $(goversion ldflags --version v1.0.1) ./simple

# Or, manually:
go run -ldflags ' \
		-X "github.com/erwinvaneyk/goversion.version=v1.0.1" \
		-X "github.com/erwinvaneyk/goversion.gitCommit=$(git rev-parse HEAD)" \
		-X "github.com/erwinvaneyk/goversion.buildDate=$(date)"' \
	    ./simple
Using generated fields

Using the package does require a long package name to be added, and is not that extensible. So, if you have goversion installed, you could also generate the ldflag fields in your main package:

goversion generate -o /path/to/your/main/package/version.gen.go

Or use the go tooling for the generation process, add the following:

//go:generate goversion generate -o version.gen.go
package main

// ...

And run

go generate

Both options will generate a file with the versioninfo fields in the main package:

// Generated by goversion
package main

import github.com/erwinvaneyk/goversion

// The following variables should be filled with goversion ldflags 
var (
	version   string
	gitCommit string
	buildDate string
	goVersion string
)

func init() {
	goversion.Set(goversion.Info{
		Version:   version,
		GitCommit: gitCommit,
		BuildDate: buildDate,
		GoVersion: goVersion,
	})
}

Using the generated code, you can now set the ldflags using the shorter main instead of the full package name:

# With goversion:
go run $(goversion ldflags --pkg main --version v1.0.4) ./generated 

# Or, manually:
go run -ldflags ' \
    		-X "main.version=v1.0.3" \
    		-X "main.gitCommit=$(git rev-parse HEAD)" \
    		-X "main.buildDate=$(date)"' \
    		./generated

See the examples for complete, functioning examples.

Using Cobra

This project contains a default command to include into your Cobra based CLI. Just add the following line to the setup of your root command:

import (
	goversionext "github.com/erwinvaneyk/goversion/pkg/extensions"
)

func init() {
    // ...
	cmd.AddCommand(goversionext.NewCobraCmd())
    // ...
}
Reproducible Builds

To make your builds, checksums, and signatures reproducible, you will need to make the following modifications when generating the ldflags:

  • Manually set the --build-date to a specific date and time at which the build should be done.

Documentation

Overview

Generated by goversion v0.1.2-SNAPSHOT

Index

Constants

View Source
const (
	GitTreeStateDirty = "dirty"
	GitTreeStateClean = "clean"
)

Variables

View Source
var (
	PackageName = reflect.TypeOf(versionInfo).PkgPath()
)

Functions

func Set

func Set(updatedVersion Info)

func ValidateStrict

func ValidateStrict(versionInfo Info) error

Types

type Info

type Info struct {
	// Version is the semantic version of the application.
	Version string `json:"version"`

	// BuildDate contains the RFC3339 timestamp normalized to UTC of when the binary was built.
	BuildDate string `json:"buildDate"`

	// BuildArch is the system architecture that was used to build the binary.
	BuildArch string `json:"buildArch"`

	// BuildOS is the operating system that was used to build the binary.
	BuildOS string `json:"buildOS"`

	// BuildBy is a free-form field that contains info about who or what was responsible for the build.
	BuildBy string `json:"buildBy"`

	// GoVersion the go version that was used to build the binary.
	GoVersion string `json:"goVersion"`

	// GitCommit is the HEAD commit at the moment of building.
	GitCommit string `json:"gitCommit"`

	// GitCommitDate contains the RFC3339 timestamp normalized to UTC of the GitCommit.
	GitCommitDate string `json:"gitCommitDate"`

	// GitBranch is the git branch that was checked out at time of building.
	GitBranch string `json:"gitBranch"`

	// GitTreeState indicates whether there where uncommitted changes when the binary was built.
	//
	// If there uncommitted changes, this field will be "dirty". Otherwise, if
	// there are no uncommitted changes, this field will be "clean".
	GitTreeState string `json:"gitTreeState"`
}

Info contains all the version-related information.

TODO add version parsing and comparing.

func AugmentFromEnv

func AugmentFromEnv(info Info) Info

AugmentFromEnv will try to infer versioning information from the local environment and augment the Info struct with it.

func Get

func Get() Info

func (Info) IsEmpty

func (i Info) IsEmpty() bool

func (Info) String

func (i Info) String() string

func (Info) ToJSON

func (i Info) ToJSON() string

func (Info) ToLDFlags

func (i Info) ToLDFlags(pkg string) string

func (Info) ToPrettyJSON

func (i Info) ToPrettyJSON() string

func (Info) ToYAML

func (i Info) ToYAML() string

Directories

Path Synopsis
cmd
goversion command
pkg

Jump to

Keyboard shortcuts

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