summaryrefslogtreecommitdiff
path: root/version.go
blob: d4f60a99091481de4df724c76e1d226c32b60f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package tube

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

const (
	defaultVersion = "0.0.0"
	defaultCommit  = "HEAD"
	defaultBuild   = "0000-01-01:00:00+00:00"
)

var (
	// Version is the tagged release version in the form <major>.<minor>.<patch>
	// following semantic versioning and is overwritten by the build system.
	Version = defaultVersion

	// Commit is the commit sha of the build (normally from Git) and is overwritten
	// by the build system.
	Commit = defaultCommit

	// Build is the date and time of the build as an RFC3339 formatted string
	// and is overwritten by the build system.
	Build = defaultBuild
)

// FullVersion display the full version and build
func FullVersion() string {
	var sb strings.Builder

	isDefault := Version == defaultVersion && Commit == defaultCommit && Build == defaultBuild

	if !isDefault {
		sb.WriteString(fmt.Sprintf("%s@%s %s", Version, Commit, Build))
	}

	if info, ok := runtime.ReadBuildInfo(); ok {
		if isDefault {
			sb.WriteString(fmt.Sprintf(" %s", info.Main.Version))
		}
		sb.WriteString(fmt.Sprintf(" %s", info.GoVersion))
		if info.Main.Sum != "" {
			sb.WriteString(fmt.Sprintf(" %s", info.Main.Sum))
		}
	}

	return sb.String()
}