2020-11-17 13:25:01 +00:00
|
|
|
/*
|
|
|
|
Package version contains all build time metadata (version, build time, git commit, etc).
|
|
|
|
*/
|
2020-07-21 16:02:03 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2021-03-20 11:33:13 +00:00
|
|
|
"strings"
|
2022-03-09 18:12:52 +00:00
|
|
|
|
|
|
|
"github.com/anchore/syft/internal"
|
2020-07-21 16:02:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const valueNotProvided = "[not provided]"
|
|
|
|
|
2020-08-12 15:04:39 +00:00
|
|
|
// all variables here are provided as build-time arguments, with clear default values
|
2020-07-21 16:02:03 +00:00
|
|
|
var version = valueNotProvided
|
|
|
|
var gitCommit = valueNotProvided
|
2022-02-04 17:41:37 +00:00
|
|
|
var gitDescription = valueNotProvided
|
2020-07-21 16:02:03 +00:00
|
|
|
var buildDate = valueNotProvided
|
|
|
|
var platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
|
|
|
|
|
2020-08-12 15:04:39 +00:00
|
|
|
// Version defines the application version details (generally from build information)
|
2020-07-21 16:02:03 +00:00
|
|
|
type Version struct {
|
2022-03-09 18:12:52 +00:00
|
|
|
Version string `json:"version"` // application semantic version
|
|
|
|
JSONSchemaVersion string `json:"jsonSchemaVersion"` // application semantic JSON schema version
|
|
|
|
GitCommit string `json:"gitCommit"` // git SHA at build-time
|
|
|
|
GitDescription string `json:"gitDescription"` // output of 'git describe --dirty --always --tags'
|
|
|
|
BuildDate string `json:"buildDate"` // date of the build
|
|
|
|
GoVersion string `json:"goVersion"` // go runtime version at build-time
|
|
|
|
Compiler string `json:"compiler"` // compiler used at build-time
|
|
|
|
Platform string `json:"platform"` // GOOS and GOARCH at build-time
|
2020-07-21 16:02:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 11:33:13 +00:00
|
|
|
func (v Version) IsProductionBuild() bool {
|
|
|
|
if strings.Contains(v.Version, "SNAPSHOT") || strings.Contains(v.Version, valueNotProvided) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-12 15:04:39 +00:00
|
|
|
// FromBuild provides all version details
|
2020-07-21 16:02:03 +00:00
|
|
|
func FromBuild() Version {
|
|
|
|
return Version{
|
2022-03-09 18:12:52 +00:00
|
|
|
Version: version,
|
|
|
|
JSONSchemaVersion: internal.JSONSchemaVersion,
|
|
|
|
GitCommit: gitCommit,
|
|
|
|
GitDescription: gitDescription,
|
|
|
|
BuildDate: buildDate,
|
|
|
|
GoVersion: runtime.Version(),
|
|
|
|
Compiler: runtime.Compiler,
|
|
|
|
Platform: platform,
|
2020-07-21 16:02:03 +00:00
|
|
|
}
|
|
|
|
}
|