|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package updater |
| 17 | + |
| 18 | +import ( |
| 19 | + "os" |
| 20 | + "strings" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 24 | + "github.com/arduino/arduino-cli/cli/globals" |
| 25 | + "github.com/arduino/arduino-cli/configuration" |
| 26 | + "github.com/arduino/arduino-cli/httpclient" |
| 27 | + "github.com/arduino/arduino-cli/i18n" |
| 28 | + "github.com/arduino/arduino-cli/inventory" |
| 29 | + "github.com/mgutz/ansi" |
| 30 | + semver "go.bug.st/relaxed-semver" |
| 31 | +) |
| 32 | + |
| 33 | +var tr = i18n.Tr |
| 34 | + |
| 35 | +// CheckForUpdate returns the latest available version if greater than |
| 36 | +// the one running and it makes sense to check for an udpate, nil in all other cases |
| 37 | +func CheckForUpdate(currentVersion *semver.Version) *semver.Version { |
| 38 | + if !shouldCheckForUpdate(currentVersion) { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + return checkForUpdate(currentVersion) |
| 43 | +} |
| 44 | + |
| 45 | +// ForceCheckForUpdate always returns the latest available version if greater than |
| 46 | +// the one running, nil in all other cases |
| 47 | +func ForceCheckForUpdate(currentVersion *semver.Version) *semver.Version { |
| 48 | + return checkForUpdate(currentVersion) |
| 49 | +} |
| 50 | + |
| 51 | +func checkForUpdate(currentVersion *semver.Version) *semver.Version { |
| 52 | + defer func() { |
| 53 | + // Always save the last time we checked for updates at the end |
| 54 | + inventory.Store.Set("updater.last_check_time", time.Now()) |
| 55 | + inventory.WriteStore() |
| 56 | + }() |
| 57 | + |
| 58 | + latestVersion, err := semver.Parse(getLatestRelease()) |
| 59 | + if err != nil { |
| 60 | + return nil |
| 61 | + } |
| 62 | + |
| 63 | + if currentVersion.GreaterThanOrEqual(latestVersion) { |
| 64 | + // Current version is already good enough |
| 65 | + return nil |
| 66 | + } |
| 67 | + |
| 68 | + return latestVersion |
| 69 | +} |
| 70 | + |
| 71 | +// NotifyNewVersionIsAvailable prints information about the new latestVersion |
| 72 | +func NotifyNewVersionIsAvailable(latestVersion string) { |
| 73 | + feedback.Errorf("\n\n%s %s → %s\n%s", |
| 74 | + ansi.Color(tr("A new release of arduino-cli is available:"), "yellow"), |
| 75 | + ansi.Color(globals.VersionInfo.VersionString, "cyan"), |
| 76 | + ansi.Color(latestVersion, "cyan"), |
| 77 | + ansi.Color("https://arduino.github.io/arduino-cli/latest/installation/#latest-packages", "yellow")) |
| 78 | +} |
| 79 | + |
| 80 | +// shouldCheckForUpdate return true if it actually makes sense to check for new updates, |
| 81 | +// false in all other cases. |
| 82 | +func shouldCheckForUpdate(currentVersion *semver.Version) bool { |
| 83 | + if strings.Contains(currentVersion.String(), "git") { |
| 84 | + // This is a dev build, no need to check for updates |
| 85 | + return false |
| 86 | + } |
| 87 | + |
| 88 | + if configuration.Settings.GetBool("updater.disable_notification") { |
| 89 | + // Don't check if the user disable the notification |
| 90 | + return false |
| 91 | + } |
| 92 | + |
| 93 | + if inventory.Store.IsSet("updater.last_check_time") && time.Since(inventory.Store.GetTime("updater.last_check_time")).Hours() < 24 { |
| 94 | + // Checked less than 24 hours ago, let's wait |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + // Don't check when running on CI or on non interactive consoles |
| 99 | + return !isCI() && configuration.IsInteractive && configuration.HasConsole |
| 100 | +} |
| 101 | + |
| 102 | +// based on https://github.com/watson/ci-info/blob/HEAD/index.js |
| 103 | +func isCI() bool { |
| 104 | + return os.Getenv("CI") != "" || // GitHub Actions, Travis CI, CircleCI, Cirrus CI, GitLab CI, AppVeyor, CodeShip, dsari |
| 105 | + os.Getenv("BUILD_NUMBER") != "" || // Jenkins, TeamCity |
| 106 | + os.Getenv("RUN_ID") != "" // TaskCluster, dsari |
| 107 | +} |
| 108 | + |
| 109 | +// getLatestRelease queries the official Arduino download server for the latest release, |
| 110 | +// if there are no errors or issues a version string is returned, in all other case an empty string. |
| 111 | +func getLatestRelease() string { |
| 112 | + client, err := httpclient.New() |
| 113 | + if err != nil { |
| 114 | + return "" |
| 115 | + } |
| 116 | + |
| 117 | + // We just use this URL to check if there's a new release available and |
| 118 | + // never show it to the user, so it's fine to use the Linux one for all OSs. |
| 119 | + URL := "https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Linux_64bit.tar.gz" |
| 120 | + res, err := client.Get(URL) |
| 121 | + if err != nil { |
| 122 | + // Yes, we ignore it |
| 123 | + return "" |
| 124 | + } |
| 125 | + |
| 126 | + // Get redirected URL |
| 127 | + location := res.Request.URL.String() |
| 128 | + |
| 129 | + // The location header points to the the latest release of the CLI, it's supposed to be formatted like this: |
| 130 | + // https://downloads.arduino.cc/arduino-cli/arduino-cli_0.18.3_Linux_64bit.tar.gz |
| 131 | + // so we split it to get the version, if there are not enough splits something must have gone wrong. |
| 132 | + split := strings.Split(location, "_") |
| 133 | + if len(split) < 2 { |
| 134 | + return "" |
| 135 | + } |
| 136 | + |
| 137 | + return split[1] |
| 138 | +} |
0 commit comments