|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package util |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "regexp" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/fatih/color" |
| 16 | + "github.com/spf13/viper" |
| 17 | + |
| 18 | + vrs "github.com/hashicorp/go-version" |
| 19 | +) |
| 20 | + |
| 21 | +type updateCheckerReleasesResponse struct { |
| 22 | + // Only thing we really care about here is the tag name |
| 23 | + TagName string `json:"tag_name"` |
| 24 | +} |
| 25 | + |
| 26 | +// Check Github's Releases API to see if we're running the latest version. |
| 27 | +// If there's any errors, quietly allow it to fail. |
| 28 | +func CheckForUpdatesAndPrintNotice() { |
| 29 | + // Don't bother running this if current application is built from source |
| 30 | + if strings.EqualFold(GetVersion(), "source") { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // Don't run if program is running in CI/CD (CI env variable will be set), or if TWITCH_DISABLE_UPDATE_CHECKS is set to true. |
| 35 | + if viper.GetBool("disable_update_checks") || strings.EqualFold(os.Getenv("CI"), "true") { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + // Don't run if this already ran successfully today |
| 40 | + today, _ := time.Parse(time.DateOnly, time.Now().Format(time.DateOnly)) |
| 41 | + lastRunDate, _ := time.Parse(time.DateOnly, viper.GetString("last_update_check")) |
| 42 | + if !today.After(lastRunDate) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + runningLatestVersion, latestVersionTag, err := areWeRunningLatestVersion() |
| 47 | + if err != nil { |
| 48 | + return // Drop errors without notifying |
| 49 | + } |
| 50 | + |
| 51 | + if !runningLatestVersion { |
| 52 | + // Messages to be displayed |
| 53 | + messages := []string{ |
| 54 | + " A new Twitch CLI release is available: " + latestVersionTag + " ", |
| 55 | + "", |
| 56 | + " See upgrade instructions at: ", |
| 57 | + " https://github.com/twitchdev/twitch-cli/blob/main/README.md#update ", |
| 58 | + "", |
| 59 | + " Check out the release notes at: ", |
| 60 | + " https://github.com/twitchdev/twitch-cli/releases/latest ", |
| 61 | + } |
| 62 | + |
| 63 | + // Find longest message |
| 64 | + longestMessageLength := 0 |
| 65 | + for _, str := range messages { |
| 66 | + if len(str) > longestMessageLength { |
| 67 | + longestMessageLength = len(str) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Print messages |
| 72 | + shaded := color.New(color.BgWhite, color.FgBlack).SprintfFunc() |
| 73 | + |
| 74 | + fmt.Println() |
| 75 | + for _, str := range messages { |
| 76 | + fmt.Printf(" %v\n", shaded(str+strings.Repeat(" ", longestMessageLength-len(str)))) |
| 77 | + } |
| 78 | + fmt.Println() |
| 79 | + |
| 80 | + // Update config so this isn't repeated until tomorrow |
| 81 | + viper.Set("last_update_check", GetTimestamp().Format(time.DateOnly)) |
| 82 | + configPath, err := GetConfigPath() |
| 83 | + if err != nil { |
| 84 | + return |
| 85 | + } |
| 86 | + _ = viper.WriteConfigAs(configPath) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Makes the call to Github's Releases API to check for the latest release version, and compares it to the current version. |
| 91 | +func areWeRunningLatestVersion() (bool, string, error) { |
| 92 | + REGEX_TAG_NAME_VERSION := regexp.MustCompile("^(?:v)(.+)") // Removes the v from the start of the tag, if it exists |
| 93 | + |
| 94 | + client := &http.Client{ |
| 95 | + Timeout: time.Second * 2, |
| 96 | + } |
| 97 | + |
| 98 | + req, err := http.NewRequest("GET", "https://api.github.com/repos/twitchdev/twitch-cli/releases/latest", nil) |
| 99 | + if err != nil { |
| 100 | + return false, "", err |
| 101 | + } |
| 102 | + req.Header.Set("User-Agent", "twitch-cli/"+GetVersion()) |
| 103 | + |
| 104 | + response, err := client.Do(req) |
| 105 | + if err != nil { |
| 106 | + return false, "", err |
| 107 | + } |
| 108 | + defer response.Body.Close() |
| 109 | + |
| 110 | + body, err := io.ReadAll(response.Body) |
| 111 | + if err != nil { |
| 112 | + return false, "", err |
| 113 | + } |
| 114 | + |
| 115 | + var obj updateCheckerReleasesResponse |
| 116 | + err = json.Unmarshal(body, &obj) |
| 117 | + if err != nil { |
| 118 | + return false, "", err |
| 119 | + } |
| 120 | + |
| 121 | + latestReleaseVersion, err := vrs.NewVersion(REGEX_TAG_NAME_VERSION.FindAllStringSubmatch(obj.TagName, -1)[0][1]) |
| 122 | + if err != nil { |
| 123 | + return false, "", err |
| 124 | + } |
| 125 | + |
| 126 | + currentVersion, err := vrs.NewVersion(GetVersion()) |
| 127 | + if err != nil { |
| 128 | + return false, "", err |
| 129 | + } |
| 130 | + |
| 131 | + return currentVersion.GreaterThanOrEqual(latestReleaseVersion), obj.TagName, nil |
| 132 | +} |
0 commit comments