Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9596a7c

Browse files
Add periodical check to notify users of new CLI releases
1 parent 4010bcd commit 9596a7c

File tree

15 files changed

+203
-41
lines changed

15 files changed

+203
-41
lines changed

‎arduino/discovery/discovery_client/go.sum‎

Lines changed: 2 additions & 0 deletions
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=

‎cli/cli.go‎

Lines changed: 36 additions & 8 deletions
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+
}

‎cli/config/validate.go‎

Lines changed: 1 addition & 0 deletions
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) {

‎cli/updater.go‎

Lines changed: 113 additions & 0 deletions
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 license@arduino.cc.
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+
}

‎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
@@ -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(".", "_"))

‎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=

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /