github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/internal/version/build.go (about)

     1  /*
     2  Package version contains all build time metadata (version, build time, git commit, etc).
     3  */
     4  package version
     5  
     6  import (
     7  	"fmt"
     8  	"runtime"
     9  	"strings"
    10  
    11  	"github.com/nextlinux/gosbom/internal"
    12  )
    13  
    14  const valueNotProvided = "[not provided]"
    15  
    16  // all variables here are provided as build-time arguments, with clear default values
    17  var version = valueNotProvided
    18  var gitCommit = valueNotProvided
    19  var gitDescription = valueNotProvided
    20  var buildDate = valueNotProvided
    21  var platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
    22  
    23  // Version defines the application version details (generally from build information)
    24  type Version struct {
    25  	Version           string `json:"version"`           // application semantic version
    26  	JSONSchemaVersion string `json:"jsonSchemaVersion"` // application semantic JSON schema version
    27  	GitCommit         string `json:"gitCommit"`         // git SHA at build-time
    28  	GitDescription    string `json:"gitDescription"`    // output of 'git describe --dirty --always --tags'
    29  	BuildDate         string `json:"buildDate"`         // date of the build
    30  	GoVersion         string `json:"goVersion"`         // go runtime version at build-time
    31  	Compiler          string `json:"compiler"`          // compiler used at build-time
    32  	Platform          string `json:"platform"`          // GOOS and GOARCH at build-time
    33  }
    34  
    35  func (v Version) IsProductionBuild() bool {
    36  	if strings.Contains(v.Version, "SNAPSHOT") || strings.Contains(v.Version, valueNotProvided) {
    37  		return false
    38  	}
    39  	return true
    40  }
    41  
    42  // FromBuild provides all version details
    43  func FromBuild() Version {
    44  	return Version{
    45  		Version:           version,
    46  		JSONSchemaVersion: internal.JSONSchemaVersion,
    47  		GitCommit:         gitCommit,
    48  		GitDescription:    gitDescription,
    49  		BuildDate:         buildDate,
    50  		GoVersion:         runtime.Version(),
    51  		Compiler:          runtime.Compiler,
    52  		Platform:          platform,
    53  	}
    54  }