From 9dac06744b8abb8e73087bd70a5a68be97cbd179 Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Wed, 2 Aug 2023 10:57:04 -0400 Subject: [PATCH] delay cli update notification --- cli/packages/util/check-for-update.go | 42 +++++++++++++++++++-------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/cli/packages/util/check-for-update.go b/cli/packages/util/check-for-update.go index e59c043266..9921cef0d8 100644 --- a/cli/packages/util/check-for-update.go +++ b/cli/packages/util/check-for-update.go @@ -11,6 +11,7 @@ import ( "os/exec" "runtime" "strings" + "time" "github.com/fatih/color" "github.com/rs/zerolog/log" @@ -20,13 +21,16 @@ func CheckForUpdate() { if checkEnv := os.Getenv("INFISICAL_DISABLE_UPDATE_CHECK"); checkEnv != "" { return } - latestVersion, err := getLatestTag("Infisical", "infisical") + latestVersion, publishedDate, err := getLatestTag("Infisical", "infisical") if err != nil { log.Debug().Err(err) // do nothing and continue return } - if latestVersion != CLI_VERSION { + + daysSinceRelease, _ := daysSinceDate(publishedDate) + + if latestVersion != CLI_VERSION && daysSinceRelease > 2 { yellow := color.New(color.FgYellow).SprintFunc() blue := color.New(color.FgCyan).SprintFunc() black := color.New(color.FgBlack).SprintFunc() @@ -50,37 +54,38 @@ func CheckForUpdate() { } } -func getLatestTag(repoOwner string, repoName string) (string, error) { +func getLatestTag(repoOwner string, repoName string) (string, string, error) { url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", repoOwner, repoName) resp, err := http.Get(url) if err != nil { - return "", err + return "", "", err } if resp.StatusCode != 200 { - return "", errors.New(fmt.Sprintf("gitHub API returned status code %d", resp.StatusCode)) + return "", "", errors.New(fmt.Sprintf("gitHub API returned status code %d", resp.StatusCode)) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - return "", err + return "", "", err } - var releaseTag struct { - TagName string `json:"tag_name"` + var releaseDetails struct { + TagName string `json:"tag_name"` + PublishedAt string `json:"published_at"` } - if err := json.Unmarshal(body, &releaseTag); err != nil { - return "", fmt.Errorf("failed to unmarshal github response: %w", err) + if err := json.Unmarshal(body, &releaseDetails); err != nil { + return "", "", fmt.Errorf("failed to unmarshal github response: %w", err) } tag_prefix := "infisical-cli/v" // Extract the version from the first valid tag - version := strings.TrimPrefix(releaseTag.TagName, tag_prefix) + version := strings.TrimPrefix(releaseDetails.TagName, tag_prefix) - return version, nil + return version, releaseDetails.PublishedAt, nil } func GetUpdateInstructions() string { @@ -145,3 +150,16 @@ func IsRunningInDocker() bool { return strings.Contains(string(cgroup), "docker") } + +func daysSinceDate(dateString string) (int, error) { + layout := "2006-01-02T15:04:05Z" + parsedDate, err := time.Parse(layout, dateString) + if err != nil { + return 0, err + } + + currentTime := time.Now() + difference := currentTime.Sub(parsedDate) + days := int(difference.Hours() / 24) + return days, nil +}