Skip to content

Commit 8e5f74c

Browse files
committed
Add ways to let users verify if new CLI released
1 parent ff4eb92 commit 8e5f74c

File tree

17 files changed

+264
-47
lines changed

17 files changed

+264
-47
lines changed

arduino/discovery/discovery_client/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
141141
github.com/mdlayher/genetlink v0.0.0-20190313224034-60417448a851/go.mod h1:EsbsAEUEs15qC1cosAwxgCWV0Qhd8TmkxnA9Kw1Vhl4=
142142
github.com/mdlayher/netlink v0.0.0-20190313131330-258ea9dff42c/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA=
143143
github.com/mdlayher/taskstats v0.0.0-20190313225729-7cbba52ee072/go.mod h1:sGdS7A6CAETR53zkdjGkgoFlh1vSm7MtX+i8XfEsTMA=
144+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
144145
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
145146
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
146147
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=

cli/cli.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/arduino/arduino-cli/cli/output"
4040
"github.com/arduino/arduino-cli/cli/sketch"
4141
"github.com/arduino/arduino-cli/cli/update"
42+
"github.com/arduino/arduino-cli/cli/updater"
4243
"github.com/arduino/arduino-cli/cli/upgrade"
4344
"github.com/arduino/arduino-cli/cli/upload"
4445
"github.com/arduino/arduino-cli/cli/version"
@@ -50,12 +51,14 @@ import (
5051
"github.com/rifflock/lfshook"
5152
"github.com/sirupsen/logrus"
5253
"github.com/spf13/cobra"
54+
semver "go.bug.st/relaxed-semver"
5355
)
5456

5557
var (
56-
verbose bool
57-
outputFormat string
58-
configFile string
58+
verbose bool
59+
outputFormat string
60+
configFile string
61+
updaterMessageChan chan *semver.Version = make(chan *semver.Version)
5962
)
6063

6164
// NewCommand creates a new ArduinoCli command root
@@ -64,11 +67,12 @@ func NewCommand() *cobra.Command {
6467

6568
// ArduinoCli is the root command
6669
arduinoCli := &cobra.Command{
67-
Use: "arduino-cli",
68-
Short: tr("Arduino CLI."),
69-
Long: tr("Arduino Command Line Interface (arduino-cli)."),
70-
Example: fmt.Sprintf(" %s <%s> [%s...]", os.Args[0], tr("command"), tr("flags")),
71-
PersistentPreRun: preRun,
70+
Use: "arduino-cli",
71+
Short: tr("Arduino CLI."),
72+
Long: tr("Arduino Command Line Interface (arduino-cli)."),
73+
Example: fmt.Sprintf(" %s <%s> [%s...]", os.Args[0], tr("command"), tr("flags")),
74+
PersistentPreRun: preRun,
75+
PersistentPostRun: postRun,
7276
}
7377

7478
arduinoCli.SetUsageTemplate(usageTemplate)
@@ -151,6 +155,20 @@ func preRun(cmd *cobra.Command, args []string) {
151155
feedback.SetOut(colorable.NewColorableStdout())
152156
feedback.SetErr(colorable.NewColorableStderr())
153157

158+
updaterMessageChan = make(chan *semver.Version)
159+
go func() {
160+
if cmd.Name() == "version" {
161+
// The version command checks by itself if there's a new available version
162+
updaterMessageChan <- nil
163+
}
164+
// Starts checking for updates
165+
currentVersion, err := semver.Parse(globals.VersionInfo.VersionString)
166+
if err != nil {
167+
updaterMessageChan <- nil
168+
}
169+
updaterMessageChan <- updater.CheckForUpdate(currentVersion)
170+
}()
171+
154172
//
155173
// Prepare logging
156174
//
@@ -236,3 +254,11 @@ func preRun(cmd *cobra.Command, args []string) {
236254
})
237255
}
238256
}
257+
258+
func postRun(cmd *cobra.Command, args []string) {
259+
latestVersion := <-updaterMessageChan
260+
if latestVersion != nil {
261+
// Notify the user a new version is available
262+
updater.NotifyNewVersionIsAvailable(latestVersion.String())
263+
}
264+
}

cli/config/validate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var validMap = map[string]reflect.Kind{
3636
"network.proxy": reflect.String,
3737
"network.user_agent_ext": reflect.String,
3838
"output.no_color": reflect.Bool,
39+
"updater.disable_notification": reflect.Bool,
3940
}
4041

4142
func typeOf(key string) (reflect.Kind, error) {

cli/updater/updater.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 license@arduino.cc.
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+
}

cli/version/version.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ package version
1717

1818
import (
1919
"os"
20+
"strings"
2021

22+
"github.com/arduino/arduino-cli/cli/errorcodes"
2123
"github.com/arduino/arduino-cli/cli/feedback"
2224
"github.com/arduino/arduino-cli/cli/globals"
25+
"github.com/arduino/arduino-cli/cli/updater"
2326
"github.com/arduino/arduino-cli/i18n"
2427
"github.com/spf13/cobra"
28+
semver "go.bug.st/relaxed-semver"
2529
)
2630

2731
var tr = i18n.Tr
@@ -39,5 +43,29 @@ func NewCommand() *cobra.Command {
3943
}
4044

4145
func run(cmd *cobra.Command, args []string) {
42-
feedback.Print(globals.VersionInfo)
46+
if strings.Contains(globals.VersionInfo.VersionString, "git-snapshot") {
47+
// We're using a development version, no need to check if there's a
48+
// new release available
49+
feedback.Print(globals.VersionInfo)
50+
return
51+
}
52+
53+
currentVersion, err := semver.Parse(globals.VersionInfo.VersionString)
54+
if err != nil {
55+
feedback.Errorf("Error parsing current version: %s", err)
56+
os.Exit(errorcodes.ErrGeneric)
57+
}
58+
latestVersion := updater.ForceCheckForUpdate(currentVersion)
59+
60+
versionInfo := globals.VersionInfo
61+
if feedback.GetFormat() == feedback.JSON && latestVersion != nil {
62+
// Set this only we managed to get the latest version
63+
versionInfo.LatestVersion = latestVersion.String()
64+
}
65+
66+
feedback.Print(versionInfo)
67+
68+
if feedback.GetFormat() == feedback.Text && latestVersion != nil {
69+
updater.NotifyNewVersionIsAvailable(latestVersion.String())
70+
}
4371
}

client_example/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
129129
github.com/mdlayher/genetlink v0.0.0-20190313224034-60417448a851/go.mod h1:EsbsAEUEs15qC1cosAwxgCWV0Qhd8TmkxnA9Kw1Vhl4=
130130
github.com/mdlayher/netlink v0.0.0-20190313131330-258ea9dff42c/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA=
131131
github.com/mdlayher/taskstats v0.0.0-20190313225729-7cbba52ee072/go.mod h1:sGdS7A6CAETR53zkdjGkgoFlh1vSm7MtX+i8XfEsTMA=
132+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
132133
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
133134
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
134135
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=

commands/daemon/term_example/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
129129
github.com/mdlayher/genetlink v0.0.0-20190313224034-60417448a851/go.mod h1:EsbsAEUEs15qC1cosAwxgCWV0Qhd8TmkxnA9Kw1Vhl4=
130130
github.com/mdlayher/netlink v0.0.0-20190313131330-258ea9dff42c/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA=
131131
github.com/mdlayher/taskstats v0.0.0-20190313225729-7cbba52ee072/go.mod h1:sGdS7A6CAETR53zkdjGkgoFlh1vSm7MtX+i8XfEsTMA=
132+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
132133
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
133134
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
134135
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=

configuration/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func SetDefaults(settings *viper.Viper) {
5252
// output settings
5353
settings.SetDefault("output.no_color", false)
5454

55+
// updater settings
56+
settings.SetDefault("updater.disable_notification", false)
57+
5558
// Bind env vars
5659
settings.SetEnvPrefix("ARDUINO")
5760
settings.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

docs/configuration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- `sketch` - configuration options relating to [Arduino sketches][sketch specification].
2525
- `always_export_binaries` - set to `true` to make [`arduino-cli compile`][arduino-cli compile] always save binaries
2626
to the sketch folder. This is the equivalent of using the [`--export-binaries`][arduino-cli compile options] flag.
27+
- `updater` - configuration options related to Arduino CLI updates
28+
- `disable_notification` - set to `true` to disable notifications of new Arduino CLI releases, defaults to `false`
2729

2830
## Configuration methods
2931

docs/installation.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ as a parameter like this:
4040
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh -s 0.9.0
4141
```
4242

43+
The Arduino CLI verifies every 24 hours if there are new releases, if you don't like this behaviour you can disable it
44+
by setting the `updater.disable_notification` config or the env var `ARDUINO_UPDATER_DISABLE_NOTIFICATION` to `true`.
45+
4346
### Download
4447

4548
Pre-built binaries for all the supported platforms are available for download from the links below.

docsgen/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
171171
github.com/mdlayher/genetlink v0.0.0-20190313224034-60417448a851/go.mod h1:EsbsAEUEs15qC1cosAwxgCWV0Qhd8TmkxnA9Kw1Vhl4=
172172
github.com/mdlayher/netlink v0.0.0-20190313131330-258ea9dff42c/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA=
173173
github.com/mdlayher/taskstats v0.0.0-20190313225729-7cbba52ee072/go.mod h1:sGdS7A6CAETR53zkdjGkgoFlh1vSm7MtX+i8XfEsTMA=
174+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
175+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
174176
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
175177
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
176178
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/mattn/go-colorable v0.1.8
2525
github.com/mattn/go-isatty v0.0.12
2626
github.com/mattn/go-runewidth v0.0.9 // indirect
27+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
2728
github.com/miekg/dns v1.1.43 // indirect
2829
github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228 // indirect
2930
github.com/pkg/errors v0.9.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
171171
github.com/mdlayher/genetlink v0.0.0-20190313224034-60417448a851/go.mod h1:EsbsAEUEs15qC1cosAwxgCWV0Qhd8TmkxnA9Kw1Vhl4=
172172
github.com/mdlayher/netlink v0.0.0-20190313131330-258ea9dff42c/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA=
173173
github.com/mdlayher/taskstats v0.0.0-20190313225729-7cbba52ee072/go.mod h1:sGdS7A6CAETR53zkdjGkgoFlh1vSm7MtX+i8XfEsTMA=
174+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
175+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
174176
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
175177
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
176178
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=

0 commit comments

Comments
 (0)