update check for CLI update

This commit is contained in:
Maidul Islam
2023-05-08 16:43:28 -04:00
parent cf9e8b8a6b
commit 69fa4a80c5
2 changed files with 23 additions and 18 deletions
+3 -1
View File
@@ -34,7 +34,9 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&debugLogging, "debug", "d", false, "Enable verbose logging") 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.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 !util.IsRunningInDocker() {
util.CheckForUpdate()
}
} }
// if config.INFISICAL_URL is set to the default value, check if INFISICAL_URL is set in the environment // if config.INFISICAL_URL is set to the default value, check if INFISICAL_URL is set in the environment
+20 -17
View File
@@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@@ -51,7 +52,7 @@ func CheckForUpdate() {
} }
func getLatestTag(repoOwner string, repoName string) (string, error) { func getLatestTag(repoOwner string, repoName string) (string, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", repoOwner, repoName) url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", repoOwner, repoName)
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
return "", err return "", err
@@ -67,29 +68,18 @@ func getLatestTag(repoOwner string, repoName string) (string, error) {
return "", err return "", err
} }
var tags []struct { var releaseTag struct {
Name string `json:"name"` TagName string `json:"tag_name"`
} }
if err := json.Unmarshal(body, &tags); err != nil { if err := json.Unmarshal(body, &releaseTag); err != nil {
return "", fmt.Errorf("failed to unmarshal github response: %w", err) return "", fmt.Errorf("failed to unmarshal github response: %w", err)
} }
// Filter tags with prefix "infisical-cli/" tag_prefix := "infisical-cli/v"
prefix := "infisical-cli/v"
var validTags []string
for _, tag := range tags {
if strings.HasPrefix(tag.Name, prefix) {
validTags = append(validTags, tag.Name)
}
}
if len(validTags) == 0 {
return "", errors.New("no tags for the CLI is found")
}
// Extract the version from the first valid tag // Extract the version from the first valid tag
version := strings.TrimPrefix(validTags[0], prefix) version := strings.TrimPrefix(releaseTag.TagName, tag_prefix)
return version, nil return version, nil
} }
@@ -143,3 +133,16 @@ func getLinuxPackageManager() string {
return "" return ""
} }
func IsRunningInDocker() bool {
if _, err := os.Stat("/.dockerenv"); err == nil {
return true
}
cgroup, err := ioutil.ReadFile("/proc/self/cgroup")
if err != nil {
return false
}
return strings.Contains(string(cgroup), "docker")
}