version: export getter functions

Future-proof the version package's exported interface by only making the
data available through getter functions. This affords us the flexibility
to e.g. implement them in terms of "runtime/debug".ReadBuildInfo() in
the future.

Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Cory Snider
2023-12-19 13:02:44 -05:00
parent ab27c9d5f1
commit a74cacff04
7 changed files with 35 additions and 17 deletions

View File

@@ -6,6 +6,24 @@ import (
"os"
)
// Package returns the overall, canonical project import path under
// which the package was built.
func Package() string {
return mainpkg
}
// Version returns returns the module version the running binary was
// built from.
func Version() string {
return version
}
// Revision returns the VCS (e.g. git) revision being used to build
// the program at linking time.
func Revision() string {
return revision
}
// FprintVersion outputs the version string to the writer, in the following
// format, followed by a newline:
//
@@ -16,7 +34,7 @@ import (
//
// registry github.com/distribution/distribution v2.0
func FprintVersion(w io.Writer) {
fmt.Fprintln(w, os.Args[0], Package, Version)
fmt.Fprintln(w, os.Args[0], Package(), Version())
}
// PrintVersion outputs the version information, from Fprint, to stdout.

View File

@@ -1,15 +1,15 @@
package version
// Package is the overall, canonical project import path under which the
// mainpkg is the overall, canonical project import path under which the
// package was built.
var Package = "github.com/distribution/distribution/v3"
var mainpkg = "github.com/distribution/distribution/v3"
// Version indicates which version of the binary is running. This is set to
// version indicates which version of the binary is running. This is set to
// the latest release tag by hand, always suffixed by "+unknown". During
// build, it will be replaced by the actual version. The value here will be
// used if the registry is run after a go get based install.
var Version = "v3.0.0-alpha.1"
var version = "v3.0.0-alpha.1.m+unknown"
// Revision is filled with the VCS (e.g. git) revision being used to build
// revision is filled with the VCS (e.g. git) revision being used to build
// the program at linking time.
var Revision = ""
var revision = ""

View File

@@ -10,17 +10,17 @@ set -e
cat <<EOF
package version
// Package is the overall, canonical project import path under which the
// mainpkg is the overall, canonical project import path under which the
// package was built.
var Package = "$(go list -m)"
var mainpkg = "$(go list -m)"
// Version indicates which version of the binary is running. This is set to
// version indicates which version of the binary is running. This is set to
// the latest release tag by hand, always suffixed by "+unknown". During
// build, it will be replaced by the actual version. The value here will be
// used if the registry is run after a go get based install.
var Version = "$(git describe --match 'v[0-9]*' --dirty='.m' --always)+unknown"
var version = "$(git describe --match 'v[0-9]*' --dirty='.m' --always)+unknown"
// Revision is filled with the VCS (e.g. git) revision being used to build
// revision is filled with the VCS (e.g. git) revision being used to build
// the program at linking time.
var Revision = ""
var revision = ""
EOF