Skip to content

Commit

Permalink
feat: add version command (#38)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com>
  • Loading branch information
beeme1mr authored Oct 22, 2024
1 parent 4b329dc commit c13a448
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -27,4 +36,5 @@ func Execute() {

func init() {
rootCmd.AddCommand(generate.Root)
rootCmd.AddCommand(versionCmd)
}
31 changes: 31 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit c13a448

Please sign in to comment.