Skip to content

Commit 00e71bf

Browse files
committed
Add period check to notify users of new CLI releases
1 parent 4010bcd commit 00e71bf

File tree

15 files changed

+203
-41
lines changed

15 files changed

+203
-41
lines changed

Diff for: arduino/discovery/discovery_client/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ 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 h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
145+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
144146
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
145147
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
146148
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=

Diff for: cli/cli.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ import (
4646
"github.com/arduino/arduino-cli/i18n"
4747
"github.com/arduino/arduino-cli/inventory"
4848
"github.com/mattn/go-colorable"
49+
"github.com/mgutz/ansi"
4950
"github.com/rifflock/lfshook"
5051
"github.com/sirupsen/logrus"
5152
"github.com/spf13/cobra"
53+
semver "go.bug.st/relaxed-semver"
5254
)
5355

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

6063
// NewCommand creates a new ArduinoCli command root
@@ -63,11 +66,12 @@ func NewCommand() *cobra.Command {
6366

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

7377
arduinoCli.SetUsageTemplate(usageTemplate)
@@ -142,6 +146,16 @@ func preRun(cmd *cobra.Command, args []string) {
142146
os.Exit(errorcodes.ErrBadArgument)
143147
}
144148

149+
updaterMessageChan = make(chan *semver.Version)
150+
go func() {
151+
// Starts checking for updates
152+
currentVersion, err := semver.Parse(globals.VersionInfo.VersionString)
153+
if err != nil {
154+
updaterMessageChan <- nil
155+
}
156+
updaterMessageChan <- checkForUpdate(currentVersion)
157+
}()
158+
145159
//
146160
// Prepare logging
147161
//
@@ -226,3 +240,17 @@ func preRun(cmd *cobra.Command, args []string) {
226240
})
227241
}
228242
}
243+
244+
func postRun(cmd *cobra.Command, args []string) {
245+
latestVersion := <-updaterMessageChan
246+
if latestVersion != nil {
247+
// Notify the user a new version is available
248+
feedback.Errorf("\n\n%s %s → %s",
249+
ansi.Color("A new release of arduino-cli is available:", "yellow"),
250+
ansi.Color(globals.VersionInfo.VersionString, "cyan"),
251+
ansi.Color(latestVersion.String(), "cyan"))
252+
feedback.Errorf("%s%s",
253+
ansi.Color("https://github.com/arduino/arduino-cli/releases/tag/", "yellow"),
254+
ansi.Color(latestVersion.String(), "yellow"))
255+
}
256+
}

Diff for: cli/config/validate.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var validMap = map[string]reflect.Kind{
3535
"metrics.enabled": reflect.Bool,
3636
"network.proxy": reflect.String,
3737
"network.user_agent_ext": reflect.String,
38+
"updater.disable_notification": reflect.Bool,
3839
}
3940

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

Diff for: cli/updater.go

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 cli
17+
18+
import (
19+
"os"
20+
"strings"
21+
"time"
22+
23+
"github.com/arduino/arduino-cli/configuration"
24+
"github.com/arduino/arduino-cli/httpclient"
25+
"github.com/arduino/arduino-cli/inventory"
26+
semver "go.bug.st/relaxed-semver"
27+
)
28+
29+
// checkForUpdate return the latest available version if greater than
30+
// the one running, nil in all other cases
31+
func checkForUpdate(currentVersion *semver.Version) *semver.Version {
32+
if !shouldCheckForUpdate(currentVersion) {
33+
return nil
34+
}
35+
36+
defer func() {
37+
// Always save the last time we checked for updates at the end
38+
inventory.Store.Set("updater.last_check_time", time.Now())
39+
inventory.WriteStore()
40+
}()
41+
42+
latestVersion, err := semver.Parse(getLatestRelease())
43+
if err != nil {
44+
return nil
45+
}
46+
47+
if currentVersion.GreaterThanOrEqual(latestVersion) {
48+
// Current version is already good enough
49+
return nil
50+
}
51+
52+
return latestVersion
53+
}
54+
55+
// shouldCheckForUpdate return true if it actually makes sense to check for new updates,
56+
// false in all other cases.
57+
func shouldCheckForUpdate(currentVersion *semver.Version) bool {
58+
if strings.Contains(currentVersion.String(), "git") {
59+
// This is a dev build, no need to check for updates
60+
return false
61+
}
62+
63+
if configuration.Settings.GetBool("updater.disable_notification") {
64+
// Don't check if the user disable the notification
65+
return false
66+
}
67+
68+
if inventory.Store.IsSet("updater.last_check_time") && time.Since(inventory.Store.GetTime("updater.last_check_time")).Hours() < 24 {
69+
// Checked less than 24 hours ago, let's wait
70+
return false
71+
}
72+
73+
// Don't check when running on CI or on non interactive consoles
74+
return !isCI() && configuration.IsInteractive && configuration.HasConsole
75+
}
76+
77+
// based on https://github.com/watson/ci-info/blob/HEAD/index.js
78+
func isCI() bool {
79+
return os.Getenv("CI") != "" || // GitHub Actions, Travis CI, CircleCI, Cirrus CI, GitLab CI, AppVeyor, CodeShip, dsari
80+
os.Getenv("BUILD_NUMBER") != "" || // Jenkins, TeamCity
81+
os.Getenv("RUN_ID") != "" // TaskCluster, dsari
82+
}
83+
84+
// getLatestRelease queries the official Arduino download server for the latest release,
85+
// if there are no errors or issues a version string is returned, in all other case an empty string.
86+
func getLatestRelease() string {
87+
client, err := httpclient.New()
88+
if err != nil {
89+
return ""
90+
}
91+
92+
// We just use this URL to check if there's a new release available and
93+
// never show it to the user, so it's fine to use the Linux one for all OSs.
94+
URL := "https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Linux_64bit.tar.gz"
95+
res, err := client.Get(URL)
96+
if err != nil {
97+
// Yes, we ignore it
98+
return ""
99+
}
100+
101+
// Get redirected URL
102+
location := res.Request.URL.String()
103+
104+
// The location header points to the the latest release of the CLI, it's supposed to be formatted like this:
105+
// https://downloads.arduino.cc/arduino-cli/arduino-cli_0.18.3_Linux_64bit.tar.gz
106+
// so we split it to get the version, if there are not enough splits something must have gone wrong.
107+
split := strings.Split(location, "_")
108+
if len(split) < 2 {
109+
return ""
110+
}
111+
112+
return split[1]
113+
}

Diff for: client_example/go.sum

+1
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=

Diff for: commands/daemon/term_example/go.sum

+1
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=

Diff for: configuration/defaults.go

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func SetDefaults(settings *viper.Viper) {
4949
settings.SetDefault("metrics.enabled", true)
5050
settings.SetDefault("metrics.addr", ":9090")
5151

52+
// updater settings
53+
settings.SetDefault("updater.disable_notification", false)
54+
5255
// Bind env vars
5356
settings.SetEnvPrefix("ARDUINO")
5457
settings.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

Diff for: docs/configuration.md

+2
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

Diff for: docs/installation.md

+3
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.

Diff for: docsgen/go.sum

+2
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=

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/mattn/go-colorable v0.1.2
2525
github.com/mattn/go-isatty v0.0.8
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

Diff for: go.sum

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

0 commit comments

Comments
 (0)