Skip to content

Commit ed582a5

Browse files
feat: check for tool updates when printing current version
1 parent d6bef23 commit ed582a5

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

check_for_updates.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/fatih/color"
10+
semver "go.bug.st/relaxed-semver"
11+
12+
"github.com/arduino/arduino-flasher-cli/feedback"
13+
"github.com/arduino/arduino-flasher-cli/i18n"
14+
"github.com/arduino/arduino-flasher-cli/updater"
15+
)
16+
17+
type FlasherRelease struct {
18+
TagName string `json:"tag_name"`
19+
}
20+
21+
func checkForUpdates() error {
22+
currentVersion, err := semver.Parse(Version)
23+
if err != nil {
24+
return err
25+
}
26+
27+
c := updater.NewClient()
28+
req, err := http.NewRequest("GET", "https://api.github.com/repos/arduino/arduino-flasher-cli/releases/latest", nil)
29+
if err != nil {
30+
return err
31+
}
32+
resp, err := c.HTTPClient.Do(req)
33+
if err != nil {
34+
return err
35+
}
36+
defer resp.Body.Close()
37+
38+
var release FlasherRelease
39+
err = json.NewDecoder(resp.Body).Decode(&release)
40+
if err != nil {
41+
return err
42+
}
43+
44+
release.TagName = strings.TrimPrefix(release.TagName, "v")
45+
latestVersion, err := semver.Parse(release.TagName)
46+
if err != nil {
47+
return err
48+
}
49+
50+
// Do nothing if the Arduino Flasher CLI is up to date
51+
if currentVersion.GreaterThanOrEqual(latestVersion) {
52+
return nil
53+
}
54+
55+
msg := fmt.Sprintf("\n\n%s %s → %s\n%s",
56+
color.YellowString(i18n.Tr("A new release of Arduino Flasher CLI is available:")),
57+
color.CyanString(currentVersion.String()),
58+
color.CyanString(latestVersion.String()),
59+
color.YellowString("https://github.com/arduino/arduino-flasher-cli/releases/latest"))
60+
feedback.Print(msg)
61+
62+
return nil
63+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/arduino/go-paths-helper v1.14.0
77
github.com/arduino/go-windows-runas v1.0.1
88
github.com/codeclysm/extract/v4 v4.0.0
9+
github.com/fatih/color v1.18.0
910
github.com/jedib0t/go-pretty/v6 v6.6.8
1011
github.com/leonelquinteros/gotext v1.7.2
1112
github.com/schollz/progressbar/v3 v3.18.0
@@ -14,6 +15,7 @@ require (
1415
github.com/stretchr/testify v1.10.0
1516
go.bug.st/cleanup v1.0.0
1617
go.bug.st/f v0.4.0
18+
go.bug.st/relaxed-semver v0.15.0
1719
)
1820

1921
require (
@@ -31,7 +33,6 @@ require (
3133
github.com/dominikbraun/graph v0.23.0 // indirect
3234
github.com/elliotchance/orderedmap/v3 v3.1.0 // indirect
3335
github.com/emirpasic/gods v1.18.1 // indirect
34-
github.com/fatih/color v1.18.0 // indirect
3536
github.com/fsnotify/fsnotify v1.9.0 // indirect
3637
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
3738
github.com/go-git/go-billy/v5 v5.6.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ go.bug.st/cleanup v1.0.0 h1:XVj1HZxkBXeq3gMT7ijWUpHyIC1j8XAoNSyQ06CskgA=
171171
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
172172
go.bug.st/f v0.4.0 h1:Vstqb950nMA+PhAlRxUw8QL1ntHy/gXHNyyzjkQLJ10=
173173
go.bug.st/f v0.4.0/go.mod h1:bMo23205ll7UW63KwO1ut5RdlJ9JK8RyEEr88CmOF5Y=
174+
go.bug.st/relaxed-semver v0.15.0 h1:w37+SYQPxF53RQO7QZZuPIMaPouOifdaP0B1ktst2nA=
175+
go.bug.st/relaxed-semver v0.15.0/go.mod h1:bwHiCtYuD2m716tBk2OnOBjelsbXw9el5EIuyxT/ksU=
174176
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
175177
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
176178
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ func main() {
6060
Use: "version",
6161
Short: "Print the version number of Arduino Flasher CLI",
6262
Run: func(cmd *cobra.Command, args []string) {
63-
fmt.Println("Arduino Flasher CLI v" + Version)
63+
fmt.Println("Arduino Flasher CLI " + Version)
64+
err := checkForUpdates()
65+
if err != nil {
66+
feedback.Warning("failed to check for updates: " + err.Error())
67+
}
6468
},
6569
})
6670

0 commit comments

Comments
 (0)