Skip to content

Commit

Permalink
add cli version check before every command
Browse files Browse the repository at this point in the history
  • Loading branch information
maidul98 committed Jan 24, 2023
1 parent 0bb7504 commit a73fc6d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cli/packages/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
42 changes: 42 additions & 0 deletions cli/packages/util/check-for-update.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions cli/packages/util/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const (
KEYRING_SERVICE_NAME = "infisical"
PERSONAL_SECRET_TYPE_NAME = "personal"
SHARED_SECRET_TYPE_NAME = "shared"
CLI_VERSION = "v0.2.5"
)

0 comments on commit a73fc6d

Please sign in to comment.