From a73fc6de19b62c7a986517ddcaddeeb6a97187e8 Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Tue, 24 Jan 2023 00:08:42 -0800 Subject: [PATCH] add cli version check before every command --- cli/packages/cmd/root.go | 7 +++-- cli/packages/util/check-for-update.go | 42 +++++++++++++++++++++++++++ cli/packages/util/constants.go | 1 + 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 cli/packages/util/check-for-update.go diff --git a/cli/packages/cmd/root.go b/cli/packages/cmd/root.go index 196c20d7d..93a498860 100644 --- a/cli/packages/cmd/root.go +++ b/cli/packages/cmd/root.go @@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{ Short: "Infisical CLI is used to inject environment variables into any process", Long: `Infisical is a simple, end-to-end encrypted service that enables teams to sync and manage their environment variables across their development life cycle.`, CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true}, - Version: "0.2.5", + Version: util.CLI_VERSION, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -33,8 +33,9 @@ func init() { rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") rootCmd.PersistentFlags().BoolVarP(&debugLogging, "debug", "d", false, "Enable verbose logging") rootCmd.PersistentFlags().StringVar(&config.INFISICAL_URL, "domain", util.INFISICAL_DEFAULT_API_URL, "Point the CLI to your own backend [can also set via environment variable name: INFISICAL_API_URL]") - // rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { - // } + rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { + util.CheckForUpdate() + } // if config.INFISICAL_URL is set to the default value, check if INFISICAL_URL is set in the environment // this is used to allow overrides of the default value diff --git a/cli/packages/util/check-for-update.go b/cli/packages/util/check-for-update.go new file mode 100644 index 000000000..bf66f87e4 --- /dev/null +++ b/cli/packages/util/check-for-update.go @@ -0,0 +1,42 @@ +package util + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +func CheckForUpdate() { + latestVersion, err := getLatestTag("infisical", "infisical") + if err != nil { + // do nothing and continue + return + } + if latestVersion != CLI_VERSION { + PrintWarning(fmt.Sprintf("Please update your CLI. You are running version %s but the latest version is %s", CLI_VERSION, latestVersion)) + } +} + +func getLatestTag(repoOwner string, repoName string) (string, error) { + url := fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", repoOwner, repoName) + resp, err := http.Get(url) + if err != nil { + return "", err + } + + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var tags []struct { + Name string `json:"name"` + } + + json.Unmarshal(body, &tags) + + return tags[0].Name, nil +} diff --git a/cli/packages/util/constants.go b/cli/packages/util/constants.go index 1aa98e2a5..0ff5396b0 100644 --- a/cli/packages/util/constants.go +++ b/cli/packages/util/constants.go @@ -11,4 +11,5 @@ const ( KEYRING_SERVICE_NAME = "infisical" PERSONAL_SECRET_TYPE_NAME = "personal" SHARED_SECRET_TYPE_NAME = "shared" + CLI_VERSION = "v0.2.5" )