Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 17 additions & 35 deletions build/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,37 @@ package build

import (
"bytes"
"fmt"
"runtime/debug"
"strings"
)

// semanticAlphabet
const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"

// These constants define the application version and follow the semantic
// versioning 2.0.0 spec (http://semver.org/).
const (
appMajor uint = 0
appMinor uint = 15
appPatch uint = 1

// appPreRelease MUST only contain characters from semanticAlphabet
// per the semantic versioning spec.
appPreRelease = "alpha"
)

// appBuild is defined as a variable so it can be overridden during the build
// process with '-ldflags "-X main.appBuild foo' if needed. It MUST only
// contain characters from semanticAlphabet per the semantic versioning spec.
var appBuild string
// appVersion is defined as a variable so it can be overridden during the build
// process if needed. It MUST only contain characters from semanticAlphabet per
// the semantic versioning spec.
//
// Example:
// go build -ldflags "-X github.com/btcsuite/btcwallet/build.appVersion=v1.0.0" ./...
var appVersion string

// Version returns the application version as a properly formed string per the
// semantic versioning 2.0.0 spec (http://semver.org/).
//
// May panic if the version is poorly configured on build.
func Version() string {
// Start with the major, minor, and path versions.
version := fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch)

// Append pre-release version if there is one. The hyphen called for
// by the semantic versioning spec is automatically appended and should
// not be contained in the pre-release string. The pre-release version
// is not appended if it contains invalid characters.
preRelease := normalizeVerString(appPreRelease)
if preRelease != "" {
version = fmt.Sprintf("%s-%s", version, preRelease)
// If set the module version must overridden.
if appVersion != "" {
return normalizeVerString(appVersion)
}

// Append build metadata if there is any. The plus called for
// by the semantic versioning spec is automatically appended and should
// not be contained in the build metadata string. The build metadata
// string is not appended if it contains invalid characters.
build := normalizeVerString(appBuild)
if build != "" {
version = fmt.Sprintf("%s+%s", version, build)
info, ok := debug.ReadBuildInfo()
if ok {
return info.Main.Version
}

return version
panic("Application version is not set")
}

// normalizeVerString returns the passed string stripped of all characters which
Expand Down