github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/pkg/version/version.go (about) 1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package version is used to define and print a consistent version number across all 16 // the binaries (reproxy, rewrapper, dumpstats and bootstrap) built from re-client 17 // repository. 18 package version 19 20 import ( 21 "flag" 22 "fmt" 23 "os" 24 25 "github.com/bazelbuild/reclient/internal/pkg/rbeflag" 26 27 log "github.com/golang/glog" 28 ) 29 30 const undef = "undefined" 31 32 // All these variables are over-ridden during link time to set the appropriate 33 // version number. Refer to README.md for guidelines on when / how to update 34 // version numbers. 35 var ( 36 // versionMajor denotes the major version number. 37 versionMajor = "0" 38 39 // versionMinor denotes the minor version number. 40 versionMinor = "1" 41 42 // versionPatch denotes the patch version number. 43 versionPatch = "1" 44 45 // versionSHA denotes the SHA of the re-client repository from which 46 // the current version is built. 47 versionSHA = undef 48 49 sdkVersionSHA = undef 50 51 // versionFlag is a boolean flag to determine whether to print version number or not. 52 versionFlag = flag.Bool("version", false, "If provided, print the current binary version and exit. If version_sdk is also provided, it takes precedence.") 53 54 // sdkVersionFlag indicates whether to print the SDK version used in the binary. 55 sdkVersionFlag = flag.Bool("version_sdk", false, "If provided, print the current binary version and the version of the SDK used by this binary and then exit.") 56 ) 57 58 // PrintAndExitOnVersionFlag checks if the VersionFlag is specified, and if it is, then 59 // it prints the current version number and exits. If info is true, the version is also printed to the Info log. 60 func PrintAndExitOnVersionFlag(info bool) { 61 rbeflag.Parse() 62 v := CurrentVersion() 63 if info { 64 log.Infof("Version: %s\n", v) 65 } 66 if *sdkVersionFlag { 67 fmt.Printf("Version: %s\n", v) 68 if sdkVersionSHA != undef && len(sdkVersionSHA) > 7 { 69 fmt.Printf("SDK: %s\n", sdkVersionSHA[:7]) 70 } 71 os.Exit(0) 72 } 73 if *versionFlag { 74 fmt.Printf("Version: %s\n", v) 75 os.Exit(0) 76 } 77 } 78 79 // CurrentVersion returns the current version number in semver format. 80 func CurrentVersion() string { 81 return fmt.Sprintf("%s.%s.%s.%s", versionMajor, versionMinor, versionPatch, versionSHA) 82 }