github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/cmd/fossa/version/version.go (about) 1 package version 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/blang/semver" 9 ) 10 11 // These are set by ldflags in the Makefile and goreleaser. They may be empty 12 // if built without the ldflags set (e.g. by using `go get`). 13 var ( 14 buildType string 15 version string 16 commit string 17 goversion string 18 ) 19 20 // ErrIsDevelopment is returned whenever functions are called that require a 21 // valid release version (generally, a valid semantic version) to parse. 22 var ErrIsDevelopment = errors.New("this development binary has no semantic version") 23 24 // IsDevelopment returns true if the build type is not "release", and true 25 // otherwise. Note that checking whether the build type is "development" is not 26 // sufficient, because buildType may be empty when no ldflags are used. 27 func IsDevelopment() bool { 28 return buildType != "release" 29 } 30 31 // String returns a long, human-readable version string. 32 func String() string { 33 return fmt.Sprintf("%s (revision %s compiled with %s)", version, commit, goversion) 34 } 35 36 // ShortString returns a machine-readable version string. If this is a release 37 // build, it returns the release version; otherwise, it returns the commit. 38 func ShortString() string { 39 if IsDevelopment() { 40 return commit 41 } 42 return version 43 } 44 45 // Semver returns the build's parsed release version. 46 func Semver() (semver.Version, error) { 47 if IsDevelopment() { 48 return semver.Version{}, ErrIsDevelopment 49 } 50 return semver.Parse(strings.TrimPrefix(version, "v")) 51 }