version

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

qiniu/version

A fast way to add assorted version information for your application.

Note this is tailored primarily to Qiniu's internal needs, and may or may not suit your specific case.

License

qiniu/version is licensed under the Apache 2.0 license.

Quick start

If you're in a hurry, just import the easy hook package:

import _ "github.com/qiniu/version/v2/easyHook"

Build it with:

go build -ldflags "-X 'github.com/qiniu/version/v2.BuildDate=$(date)'" .

Then run your app with version, -version or --version, and you will see output like:

✗ ./examples version
Go version:     go1.19.3
Git commit:     56dac6d5e895d5d6474b840d99fb3c7cfbdf26e3
Commit date:    2022-12-06T12:22:24Z
Git state:      dirty
Built date:     Wed Dec  7 14:11:24 CST 2022
OS/Arch:        darwin/amd64
Compiler:       gc

Also, there are some other useful options for your reference. For example:

LDFLAGS="${LDFLAGS} \
	-X \"github.com/qiniu/version/v2.GitTag=$(git describe --tags)\" \
	-X \"github.com/qiniu/version/v2.BuildComments=${BUILDCOMMENTS}\" \
	-X \"github.com/qiniu/version/v2.Name=${NAME}\" \

Check the source code for details.

Advanced usage

The easy hook package used in the above steps can bring about surprises. For example, small programs designed to operate on a single input file path argument will suddenly stop working with a path of version, because the invocation foo version will be intercepted by this package and your func main will not even have a chance to run.

If you want more control, you may simply import github.com/qiniu/version/v2 and call version.Print() or consume the individual values exposed there, to your own liking.

Documentation

Overview

Package version provides access to some of your binary's versioning information.

The easy way to integrate this package, but could bring about surprising behavior (read the source for details, it's just a one-liner!):

import _ "github.com/qiniu/version/v2/easyHook"

And if you want more control and less magic:

import "github.com/qiniu/version/v2"

func main() {
	// make use of one of the prepopulated values
	_ = version.GitCommit
	// or output in the package-standard format in your CLI handler
	version.Print()
}

Index

Constants

View Source
const Compiler = runtime.Compiler

Compiler is a convenient alias for runtime.Compiler.

Variables

View Source
var (
	// GoVersion is the version of the Go toolchain used to build the binary
	// (e.g. "go1.19.2").
	// It defaults to value of runtime.Version() if not explicitly overridden.
	GoVersion = unknownProperty
	// GitCommit is the commit hash of the Git repository's HEAD at
	// build-time.
	// It defaults to the value as collected by the runtime/debug package if
	// not explicitly overridden.
	GitCommit = unknownProperty
	// GitCommitDate is GitCommit's commit date in RFC3339 format.
	// It defaults to the value as collected by the runtime/debug package if
	// not explicitly overridden.
	GitCommitDate = unknownProperty
	// GitTreeState becomes "dirty" if the source tree had local modifications
	// at build-time.
	// It stays empty otherwise and will not be shown in Print if this is the
	// case.
	GitTreeState = unknownProperty
	// GitTag is meant to be injected with the tag name associated with
	// GitCommit, by means of `go -ldflags` at build-time.
	// It stays empty otherwise and will not be shown in Print if this is the
	// case.
	GitTag = unknownProperty
	// BuildDate is meant to be injected with a string denoting the build time
	// of the binary, by means of `go -ldflags` at build-time.
	// It stays empty otherwise and will not be shown in Print if this is the
	// case.
	BuildDate = unknownProperty
	// Platform is a string in the form of "GOOS/GOARCH", e.g. "linux/amd64".
	Platform = unknownProperty
	// BuildComments can be used to associate arbitrary extra information with
	// the binary, by means of injection via `go -ldflags` at build-time.
	BuildComments = unknownProperty
	// Name is meant to be injected with the binary's intended name, by means
	// of `go -ldflags` at build-time.
	// It stays empty otherwise and will not be shown in Print if this is the
	// case.
	Name = unknownProperty
)

Version information

Functions

func Print

func Print()

Print prints out the collected version information.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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