From c13a4486b9b42f3e4a6f34abd43a87aecf91355e Mon Sep 17 00:00:00 2001 From: Michael Beemer Date: Tue, 22 Oct 2024 14:59:59 -0400 Subject: [PATCH] feat: add version command (#38) Signed-off-by: Michael Beemer --- cmd/root.go | 12 +++++++++++- cmd/version.go | 31 +++++++++++++++++++++++++++++++ main.go | 9 ++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 cmd/version.go diff --git a/cmd/root.go b/cmd/root.go index 2f841d4..dc5f25c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,6 +9,12 @@ import ( "github.com/spf13/cobra" ) +var ( + Version string + Commit string + Date string +) + // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "openfeature", @@ -18,7 +24,10 @@ var rootCmd = &cobra.Command{ // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { +func Execute(version string, commit string, date string) { + Version = version + Commit = commit + Date = date if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) @@ -27,4 +36,5 @@ func Execute() { func init() { rootCmd.AddCommand(generate.Root) + rootCmd.AddCommand(versionCmd) } diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..6a719c1 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + "runtime/debug" + + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of the OpenFeature CLI", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + if Version == "dev" { + details, ok := debug.ReadBuildInfo() + if ok && details.Main.Version != "" && details.Main.Version != "(devel)" { + Version = details.Main.Version + for _, i := range details.Settings { + if i.Key == "vcs.time" { + Date = i.Value + } + if i.Key == "vcs.revision" { + Commit = i.Value + } + } + } + } + fmt.Printf("OpenFeature CLI: %s (%s), built at: %s\n", Version, Commit, Date) + }, +} \ No newline at end of file diff --git a/main.go b/main.go index 8ff55f1..b7bd958 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,13 @@ package main import "codegen/cmd" +var ( + // Overridden by Go Releaser at build time + version = "dev" + commit = "HEAD" + date = "unknown" +) + func main() { - cmd.Execute() + cmd.Execute(version, commit, date) }