From e42422407ccd13e7136092965e1003ab73e6c745 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Wed, 13 Mar 2024 11:38:30 +0530 Subject: [PATCH] [cli] Pick version from the github tag --- cli/cmd/root.go | 5 +++-- cli/cmd/version.go | 2 +- cli/main.go | 4 +++- cli/release.sh | 13 ++++++++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 0b53f4162..e7f3378a4 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -const AppVersion = "0.1.11" +var version string var ctrl *pkg.ClICtrl @@ -34,8 +34,9 @@ func GenerateDocs() error { // 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(controller *pkg.ClICtrl) { +func Execute(controller *pkg.ClICtrl, ver string) { ctrl = controller + version = ver err := rootCmd.Execute() if err != nil { os.Exit(1) diff --git a/cli/cmd/version.go b/cli/cmd/version.go index 9d5194bc7..3431bcfc3 100644 --- a/cli/cmd/version.go +++ b/cli/cmd/version.go @@ -12,7 +12,7 @@ var versionCmd = &cobra.Command{ Short: "Prints the current version", Long: ``, Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("Version %s\n", AppVersion) + fmt.Printf("Version %s\n", version) }, } diff --git a/cli/main.go b/cli/main.go index f9ad0fa06..157c11fd8 100644 --- a/cli/main.go +++ b/cli/main.go @@ -15,6 +15,8 @@ import ( "strings" ) +var AppVersion = "0.1.12" + func main() { cliDBPath, err := GetCLIConfigPath() if secrets.IsRunningInContainer() { @@ -73,7 +75,7 @@ func main() { } return } - cmd.Execute(&ctrl) + cmd.Execute(&ctrl, AppVersion) } func initConfig(cliConfigPath string) { diff --git a/cli/release.sh b/cli/release.sh index ac0106bd0..b3946c381 100755 --- a/cli/release.sh +++ b/cli/release.sh @@ -1,5 +1,16 @@ #!/bin/bash +# Fetch the latest tag that starts with "cli-" +# shellcheck disable=SC2046 +# shellcheck disable=SC2006 +LATEST_TAG=$(git describe --tags `git rev-list --tags='cli-*' --max-count=1`) + +# Check if the LATEST_TAG variable is empty +if [ -z "$LATEST_TAG" ]; then + echo "No 'cli-' tag found. Exiting..." + exit 1 +fi +VERSION=${LATEST_TAG#cli-} # Create a "bin" directory if it doesn't exist mkdir -p bin @@ -29,7 +40,7 @@ do fi # Build the binary and place it in the "bin" directory - go build -ldflags="-s -w" -trimpath -o "bin/$BINARY_NAME" main.go + go build -ldflags="-X main.AppVersion=${VERSION} -s -w" -trimpath -o "bin/$BINARY_NAME" main.go # Print a message indicating the build is complete for the current OS and architecture echo "Built for $OS ($ARCH) as bin/$BINARY_NAME"