Skip to content

Commit a9cba09

Browse files
authored
Add version subcommand to report gorm-cli build version (#33)
* feat: add version command to CLI * use runtime/debug build info for version * level added * Inline version resolution into `versionCmd`; remove top-level `getVersion`
1 parent a94e55e commit a9cba09

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"runtime/debug"
67

78
"github.com/spf13/cobra"
89
"gorm.io/cli/gorm/internal/gen"
@@ -15,9 +16,27 @@ func main() {
1516
}
1617

1718
rootCmd.AddCommand(gen.New())
19+
rootCmd.AddCommand(versionCmd())
1820

1921
if err := rootCmd.Execute(); err != nil {
2022
fmt.Println(err)
2123
os.Exit(1)
2224
}
2325
}
26+
27+
func versionCmd() *cobra.Command {
28+
return &cobra.Command{
29+
Use: "version",
30+
Short: "Print the version of gorm-cli",
31+
Run: func(cmd *cobra.Command, args []string) {
32+
if info, ok := debug.ReadBuildInfo(); ok {
33+
if info.Main.Version != "" && info.Main.Version != "(devel)" {
34+
fmt.Printf("gorm-cli version %s\n", info.Main.Version)
35+
return
36+
}
37+
}
38+
39+
fmt.Printf("gorm-cli version %s\n", "dev")
40+
},
41+
}
42+
}

0 commit comments

Comments
 (0)