Skip to content

Commit

Permalink
delay cli update notification
Browse files Browse the repository at this point in the history
  • Loading branch information
maidul98 committed Aug 2, 2023
1 parent bd80c2c commit 9dac067
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions cli/packages/util/check-for-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os/exec"
"runtime"
"strings"
"time"

"github.com/fatih/color"
"github.com/rs/zerolog/log"
Expand All @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 9dac067

Please sign in to comment.