2020-08-06 20:58:47 +03:00
|
|
|
// Package buildinfo hosts build info variables populated via ldflags.
|
|
|
|
package buildinfo
|
|
|
|
|
2022-12-30 20:10:56 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultVersion is the default value for Version if not
|
2020-08-09 16:46:46 +03:00
|
|
|
// set via ldflags.
|
2022-12-30 20:10:56 +03:00
|
|
|
const DefaultVersion = "v0.0.0-dev"
|
2020-08-09 16:46:46 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
var (
|
2020-08-09 16:46:46 +03:00
|
|
|
// Version is the build version. If not set at build time via
|
2022-12-30 20:10:56 +03:00
|
|
|
// ldflags, Version takes the value of DefaultVersion.
|
|
|
|
Version = DefaultVersion
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// Commit is the commit hash.
|
|
|
|
Commit string
|
|
|
|
|
|
|
|
// Timestamp is the timestamp of when the cli was built.
|
|
|
|
Timestamp string
|
|
|
|
)
|
2022-12-30 20:10:56 +03:00
|
|
|
|
|
|
|
// BuildInfo encapsulates Version, Commit and Timestamp.
|
|
|
|
type BuildInfo struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Commit string `json:"commit,omitempty"`
|
|
|
|
Timestamp string `json:"timestamp,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string representation of BuildInfo.
|
|
|
|
func (bi BuildInfo) String() string {
|
|
|
|
s := bi.Version
|
|
|
|
if bi.Commit != "" {
|
|
|
|
s += " " + bi.Commit
|
|
|
|
}
|
|
|
|
if bi.Timestamp != "" {
|
|
|
|
s += " " + bi.Timestamp
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info returns BuildInfo.
|
|
|
|
func Info() BuildInfo {
|
|
|
|
return BuildInfo{
|
|
|
|
Version: Version,
|
|
|
|
Commit: Commit,
|
|
|
|
Timestamp: Timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() { //nolint:gochecknoinits
|
|
|
|
if strings.HasSuffix(Version, "~dev") {
|
|
|
|
Version = strings.Replace(Version, "~dev", "-dev", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if Version != "" && !semver.IsValid(Version) {
|
|
|
|
// We want to panic here because it is a pipeline/build failure
|
|
|
|
// to have an invalid non-empty Version.
|
|
|
|
panic(fmt.Sprintf("Invalid BuildInfo.Version value: %q", Version))
|
|
|
|
}
|
|
|
|
|
|
|
|
if Timestamp != "" {
|
|
|
|
// Make sure Timestamp is normalized
|
|
|
|
t := stringz.TimestampToRFC3339(Timestamp)
|
|
|
|
if t != "" {
|
|
|
|
Timestamp = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDefaultVersion returns true if Version is empty or DefaultVersion.
|
|
|
|
func IsDefaultVersion() bool {
|
|
|
|
return Version == "" || Version == DefaultVersion
|
|
|
|
}
|