|
| 1 | +package basecli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "runtime" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +var short bool |
| 11 | + |
| 12 | +var ( |
| 13 | + // version is the semantic version of Iter8 |
| 14 | + // this variable is intended to be set using LDFLAGS at build time |
| 15 | + version = "v0.8.8" |
| 16 | + // version is the current major/minor version of Iter8 |
| 17 | + // set this manually whenever the major or minor version changes |
| 18 | + majorMinor = "v0.8" |
| 19 | + // metadata is extra build time data |
| 20 | + metadata = "" |
| 21 | + // gitCommit is the git sha1 |
| 22 | + gitCommit = "" |
| 23 | + // gitTreeState is the state of the git tree |
| 24 | + gitTreeState = "" |
| 25 | +) |
| 26 | + |
| 27 | +// BuildInfo describes the compile time information. |
| 28 | +type BuildInfo struct { |
| 29 | + // Version is the current semver. |
| 30 | + Version string `json:"version,omitempty"` |
| 31 | + // GitCommit is the git sha1. |
| 32 | + GitCommit string `json:"git_commit,omitempty"` |
| 33 | + // GitTreeState is the state of the git tree. |
| 34 | + GitTreeState string `json:"git_tree_state,omitempty"` |
| 35 | + // GoVersion is the version of the Go compiler used to compile Iter8. |
| 36 | + GoVersion string `json:"go_version,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +// versionCmd represents the version command |
| 40 | +var versionCmd = &cobra.Command{ |
| 41 | + Use: "version", |
| 42 | + Short: "Print Iter8 version information", |
| 43 | + Long: ` |
| 44 | +Show the version for Iter8. |
| 45 | +
|
| 46 | +The output will look something like this: |
| 47 | +
|
| 48 | +version.BuildInfo{Version:"v0.8.10", GitCommit:"fe51cd1e31e6a202cba7aliv9552a6d418ded79a", GitTreeState:"clean", GoVersion:"go1.16.10"} |
| 49 | +
|
| 50 | +- Version is the semantic version of the release. |
| 51 | +- GitCommit is the SHA for the commit that this version was built from. |
| 52 | +- GitTreeState is "clean" if there are no local code changes when this binary was |
| 53 | + built, and "dirty" if the binary was built from locally modified code. |
| 54 | +- GoVersion is the version of Go that was used to compile Iter8. |
| 55 | +`, |
| 56 | + Run: func(cmd *cobra.Command, args []string) { |
| 57 | + v := get() |
| 58 | + if short { |
| 59 | + if len(v.GitCommit) >= 7 { |
| 60 | + fmt.Printf("%s+g%s", v.Version, v.GitCommit[:7]) |
| 61 | + fmt.Println() |
| 62 | + return |
| 63 | + } |
| 64 | + fmt.Println(getVersion()) |
| 65 | + return |
| 66 | + } |
| 67 | + fmt.Printf("%#v", v) |
| 68 | + fmt.Println() |
| 69 | + }, |
| 70 | +} |
| 71 | + |
| 72 | +// getVersion returns the semver string of the version |
| 73 | +func getVersion() string { |
| 74 | + if metadata == "" { |
| 75 | + return version |
| 76 | + } |
| 77 | + return version + "+" + metadata |
| 78 | +} |
| 79 | + |
| 80 | +// get returns build info |
| 81 | +func get() BuildInfo { |
| 82 | + v := BuildInfo{ |
| 83 | + Version: getVersion(), |
| 84 | + GitCommit: gitCommit, |
| 85 | + GitTreeState: gitTreeState, |
| 86 | + GoVersion: runtime.Version(), |
| 87 | + } |
| 88 | + return v |
| 89 | +} |
| 90 | + |
| 91 | +func init() { |
| 92 | + RootCmd.AddCommand(versionCmd) |
| 93 | + f := versionCmd.Flags() |
| 94 | + f.BoolVar(&short, "short", false, "print the version number") |
| 95 | +} |
0 commit comments