Movatterモバイル変換


[0]ホーム

URL:


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

Commited582a5

Browse files
feat: check for tool updates when printing current version
1 parentd6bef23 commited582a5

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+
typeFlasherReleasestruct {
18+
TagNamestring`json:"tag_name"`
19+
}
20+
21+
funccheckForUpdates()error {
22+
currentVersion,err:=semver.Parse(Version)
23+
iferr!=nil {
24+
returnerr
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+
iferr!=nil {
30+
returnerr
31+
}
32+
resp,err:=c.HTTPClient.Do(req)
33+
iferr!=nil {
34+
returnerr
35+
}
36+
deferresp.Body.Close()
37+
38+
varreleaseFlasherRelease
39+
err=json.NewDecoder(resp.Body).Decode(&release)
40+
iferr!=nil {
41+
returnerr
42+
}
43+
44+
release.TagName=strings.TrimPrefix(release.TagName,"v")
45+
latestVersion,err:=semver.Parse(release.TagName)
46+
iferr!=nil {
47+
returnerr
48+
}
49+
50+
// Do nothing if the Arduino Flasher CLI is up to date
51+
ifcurrentVersion.GreaterThanOrEqual(latestVersion) {
52+
returnnil
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+
returnnil
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-helperv1.14.0
77
github.com/arduino/go-windows-runasv1.0.1
88
github.com/codeclysm/extract/v4v4.0.0
9+
github.com/fatih/colorv1.18.0
910
github.com/jedib0t/go-pretty/v6v6.6.8
1011
github.com/leonelquinteros/gotextv1.7.2
1112
github.com/schollz/progressbar/v3v3.18.0
@@ -14,6 +15,7 @@ require (
1415
github.com/stretchr/testifyv1.10.0
1516
go.bug.st/cleanupv1.0.0
1617
go.bug.st/fv0.4.0
18+
go.bug.st/relaxed-semverv0.15.0
1719
)
1820

1921
require (
@@ -31,7 +33,6 @@ require (
3133
github.com/dominikbraun/graphv0.23.0// indirect
3234
github.com/elliotchance/orderedmap/v3v3.1.0// indirect
3335
github.com/emirpasic/godsv1.18.1// indirect
34-
github.com/fatih/colorv1.18.0// indirect
3536
github.com/fsnotify/fsnotifyv1.9.0// indirect
3637
github.com/go-git/gcfgv1.5.1-0.20230307220236-3a3c6141e376// indirect
3738
github.com/go-git/go-billy/v5v5.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/cleanupv1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
172172
go.bug.st/fv0.4.0 h1:Vstqb950nMA+PhAlRxUw8QL1ntHy/gXHNyyzjkQLJ10=
173173
go.bug.st/fv0.4.0/go.mod h1:bMo23205ll7UW63KwO1ut5RdlJ9JK8RyEEr88CmOF5Y=
174+
go.bug.st/relaxed-semverv0.15.0 h1:w37+SYQPxF53RQO7QZZuPIMaPouOifdaP0B1ktst2nA=
175+
go.bug.st/relaxed-semverv0.15.0/go.mod h1:bwHiCtYuD2m716tBk2OnOBjelsbXw9el5EIuyxT/ksU=
174176
golang.org/x/cryptov0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
175177
golang.org/x/cryptov0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
176178
golang.org/x/cryptov0.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+
iferr!=nil {
66+
feedback.Warning("failed to check for updates: "+err.Error())
67+
}
6468
},
6569
})
6670

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp