diff --git a/cmd/root.go b/cmd/root.go index 534c33a..d14377d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,8 +26,9 @@ var ( method string // TODO: headers map[string]string - opts = model.PostMeasurement{} - ctx = model.Context{} + opts = model.PostMeasurement{} + ctx = model.Context{} + version string ) // rootCmd represents the base command when called without any subcommands @@ -40,7 +41,8 @@ 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(ver string) { + version = ver err := rootCmd.Execute() if err != nil { os.Exit(1) diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..753b979 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of Globalping CLI", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Globalping CLI " + version) + }, +} diff --git a/main.go b/main.go index 0bf99a5..523a6c1 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,11 @@ package main import "github.com/jsdelivr/globalping-cli/cmd" +var ( + // https://goreleaser.com/cookbooks/using-main.version/ + version = "dev" +) + func main() { - cmd.Execute() + cmd.Execute(version) }