Skip to content

Commit c13a448

Browse files
authored
feat: add version command (open-feature#38)
Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com>
1 parent 4b329dc commit c13a448

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

cmd/root.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import (
99
"github.com/spf13/cobra"
1010
)
1111

12+
var (
13+
Version string
14+
Commit string
15+
Date string
16+
)
17+
1218
// rootCmd represents the base command when called without any subcommands
1319
var rootCmd = &cobra.Command{
1420
Use: "openfeature",
@@ -18,7 +24,10 @@ var rootCmd = &cobra.Command{
1824

1925
// Execute adds all child commands to the root command and sets flags appropriately.
2026
// This is called by main.main(). It only needs to happen once to the rootCmd.
21-
func Execute() {
27+
func Execute(version string, commit string, date string) {
28+
Version = version
29+
Commit = commit
30+
Date = date
2231
if err := rootCmd.Execute(); err != nil {
2332
fmt.Println(err)
2433
os.Exit(1)
@@ -27,4 +36,5 @@ func Execute() {
2736

2837
func init() {
2938
rootCmd.AddCommand(generate.Root)
39+
rootCmd.AddCommand(versionCmd)
3040
}

cmd/version.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"runtime/debug"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var versionCmd = &cobra.Command{
11+
Use: "version",
12+
Short: "Print the version number of the OpenFeature CLI",
13+
Long: ``,
14+
Run: func(cmd *cobra.Command, args []string) {
15+
if Version == "dev" {
16+
details, ok := debug.ReadBuildInfo()
17+
if ok && details.Main.Version != "" && details.Main.Version != "(devel)" {
18+
Version = details.Main.Version
19+
for _, i := range details.Settings {
20+
if i.Key == "vcs.time" {
21+
Date = i.Value
22+
}
23+
if i.Key == "vcs.revision" {
24+
Commit = i.Value
25+
}
26+
}
27+
}
28+
}
29+
fmt.Printf("OpenFeature CLI: %s (%s), built at: %s\n", Version, Commit, Date)
30+
},
31+
}

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ package main
22

33
import "codegen/cmd"
44

5+
var (
6+
// Overridden by Go Releaser at build time
7+
version = "dev"
8+
commit = "HEAD"
9+
date = "unknown"
10+
)
11+
512
func main() {
6-
cmd.Execute()
13+
cmd.Execute(version, commit, date)
714
}

0 commit comments

Comments
 (0)