mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 19:28:09 +00:00
delay cli update notification
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -20,13 +21,16 @@ func CheckForUpdate() {
|
|||||||
if checkEnv := os.Getenv("INFISICAL_DISABLE_UPDATE_CHECK"); checkEnv != "" {
|
if checkEnv := os.Getenv("INFISICAL_DISABLE_UPDATE_CHECK"); checkEnv != "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
latestVersion, err := getLatestTag("Infisical", "infisical")
|
latestVersion, publishedDate, err := getLatestTag("Infisical", "infisical")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug().Err(err)
|
log.Debug().Err(err)
|
||||||
// do nothing and continue
|
// do nothing and continue
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if latestVersion != CLI_VERSION {
|
|
||||||
|
daysSinceRelease, _ := daysSinceDate(publishedDate)
|
||||||
|
|
||||||
|
if latestVersion != CLI_VERSION && daysSinceRelease > 2 {
|
||||||
yellow := color.New(color.FgYellow).SprintFunc()
|
yellow := color.New(color.FgYellow).SprintFunc()
|
||||||
blue := color.New(color.FgCyan).SprintFunc()
|
blue := color.New(color.FgCyan).SprintFunc()
|
||||||
black := color.New(color.FgBlack).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)
|
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
|
||||||
}
|
}
|
||||||
if resp.StatusCode != 200 {
|
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()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var releaseTag struct {
|
var releaseDetails struct {
|
||||||
TagName string `json:"tag_name"`
|
TagName string `json:"tag_name"`
|
||||||
|
PublishedAt string `json:"published_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(body, &releaseTag); err != nil {
|
if err := json.Unmarshal(body, &releaseDetails); err != nil {
|
||||||
return "", fmt.Errorf("failed to unmarshal github response: %w", err)
|
return "", "", fmt.Errorf("failed to unmarshal github response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tag_prefix := "infisical-cli/v"
|
tag_prefix := "infisical-cli/v"
|
||||||
|
|
||||||
// Extract the version from the first valid tag
|
// 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 {
|
func GetUpdateInstructions() string {
|
||||||
@@ -145,3 +150,16 @@ func IsRunningInDocker() bool {
|
|||||||
|
|
||||||
return strings.Contains(string(cgroup), "docker")
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user