github.com/jenkins-x/jx/v2@v2.1.155/pkg/version/info.go (about) 1 // Copyright 2015 The Prometheus Authors 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package version 15 16 import ( 17 "strings" 18 19 "github.com/pkg/errors" 20 21 "github.com/blang/semver" 22 "github.com/jenkins-x/jx-logging/pkg/log" 23 ) 24 25 // Build information. Populated at build-time. 26 var ( 27 Version string 28 Revision string 29 BuildDate string 30 GoVersion string 31 GitTreeState string 32 ) 33 34 // Map provides the iterable version information. 35 var Map = map[string]string{ 36 "version": Version, 37 "revision": Revision, 38 "buildDate": BuildDate, 39 "goVersion": GoVersion, 40 "gitTreeState": GitTreeState, 41 } 42 43 const ( 44 // VersionPrefix string for setting pre-release etc 45 VersionPrefix = "" 46 47 // ExampleVersion shows an example version in the help 48 // if no version could be found (which should never really happen!) 49 ExampleVersion = "1.1.59" 50 51 // TestVersion used in test cases for the current version if no 52 // version can be found - such as if the version property is not properly 53 // included in the go test flags. 54 TestVersion = "2.0.404" 55 56 // TestRevision can be used in tests if no revision is passed in the test flags 57 TestRevision = "04b628f48" 58 59 // TestTreeState can be used in tests if no tree state is passed in the test flags 60 TestTreeState = "clean" 61 62 // TestBuildDate can be used in tests if no build date is passed in the test flags 63 TestBuildDate = "2020-05-31T14:51:38Z" 64 65 // TestGoVersion can be used in tests if no version is passed in the test flags 66 TestGoVersion = "1.13.8" 67 ) 68 69 // GetVersion gets the current version string 70 func GetVersion() string { 71 v := Map["version"] 72 if v == "" { 73 v = TestVersion 74 } 75 return v 76 } 77 78 // GetSemverVersion returns a semver.Version struct representing the current version 79 func GetSemverVersion() (semver.Version, error) { 80 text := strings.TrimPrefix(GetVersion(), VersionPrefix) 81 v, err := semver.Make(text) 82 if err != nil { 83 return v, errors.Wrapf(err, "failed to parse version %s", text) 84 } 85 return v, nil 86 } 87 88 // GetRevision returns the short SHA1 hashes given a given revision 89 func GetRevision() string { 90 v := Map["revision"] 91 if v == "" { 92 v = TestRevision 93 } 94 return v 95 } 96 97 // GetTreeState returns the state of the working tree 98 func GetTreeState() string { 99 v := Map["gitTreeState"] 100 if v == "" { 101 v = TestTreeState 102 } 103 return v 104 } 105 106 // GetBuildDate returns the build date for the binary 107 func GetBuildDate() string { 108 v := Map["buildDate"] 109 if v == "" { 110 v = TestBuildDate 111 } 112 return v 113 } 114 115 // GetGoVersion returns the version of go used to build the binary 116 func GetGoVersion() string { 117 v := Map["goVersion"] 118 if v == "" { 119 v = TestGoVersion 120 } 121 return v 122 } 123 124 // StringDefault returns the current version string or returns a dummy 125 // default value if there is an error 126 func StringDefault(defaultValue string) string { 127 v, err := GetSemverVersion() 128 if err == nil { 129 return v.String() 130 } 131 log.Logger().Warnf("Warning failed to load version: %s", err) 132 return defaultValue 133 }