Skip to content

Commit 17b1286

Browse files
committed
chore: add glctl/update selfupdate cmd
1 parent c952c3b commit 17b1286

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed

.changelog/9.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
`glctl/update` - Now `glctl` can update itself. Run `glctl update` to update to the latest version.
3+
```

.vscode/changelog.code-snippets

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"Release Note Breaking change": {
3+
"scope": "plaintext",
4+
"prefix": "release-note-breaking-change",
5+
"body": [
6+
"```release-note:breaking-change",
7+
"`server/xx` - Short Description.",
8+
"`glctl/xx` - Short Description.",
9+
"`api/xx` - Short Description.",
10+
"`sdk/xx` - Short Description.",
11+
"```"
12+
]
13+
},
14+
// Release note feature
15+
"Release Note New Feature": {
16+
"scope": "plaintext",
17+
"prefix": "release-note-feature",
18+
"body": [
19+
"```release-note:feature",
20+
"`server/xx` - Short Description.",
21+
"`glctl/xx` - Short Description.",
22+
"`api/xx` - Short Description.",
23+
"`sdk/xx` - Short Description.",
24+
"```"
25+
]
26+
},
27+
// Release note bug
28+
"Release Note Bug": {
29+
"scope": "plaintext",
30+
"prefix": "release-note-bug",
31+
"body": [
32+
"```release-note:bug",
33+
"`server/xx` - Short Description.",
34+
"`glctl/xx` - Short Description.",
35+
"`api/xx` - Short Description.",
36+
"`sdk/xx` - Short Description.",
37+
"```"
38+
]
39+
},
40+
// Release note Note
41+
"Release Note Note": {
42+
"scope": "plaintext",
43+
"prefix": "release-note-note",
44+
"body": [
45+
"```release-note:note",
46+
"`server/xx` - Short Description.",
47+
"`glctl/xx` - Short Description.",
48+
"`api/xx` - Short Description.",
49+
"`sdk/xx` - Short Description.",
50+
"```"
51+
]
52+
},
53+
// Release note enchancement
54+
"Release Note Enhancement": {
55+
"scope": "plaintext",
56+
"prefix": "release-note-enhancement",
57+
"body": [
58+
"```release-note:enhancement",
59+
"`server/xx` - Short Description.",
60+
"`glctl/xx` - Short Description.",
61+
"`api/xx` - Short Description.",
62+
"`sdk/xx` - Short Description.",
63+
"```"
64+
]
65+
},
66+
}

cmd/glctl/cmd/update.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package glctl
5+
6+
import (
7+
"context"
8+
"log"
9+
"os"
10+
"runtime"
11+
12+
selfupdate "github.com/creativeprojects/go-selfupdate"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
// updateCmd represents the version command.
17+
var updateCmd = &cobra.Command{
18+
Use: "update",
19+
GroupID: "other",
20+
Short: "Check for updates and update the application",
21+
Long: `Check if a new version is available and update the application.`,
22+
Run: func(cmd *cobra.Command, args []string) {
23+
if version == "dev" {
24+
log.Printf("Cannot update a development version")
25+
return
26+
}
27+
log.Println("Checking for updates...")
28+
latest, found, err := selfupdate.DetectLatest(context.Background(), selfupdate.ParseSlug("azrod/golink"))
29+
if err != nil {
30+
log.Default().Printf("error occurred while detecting version: %s", err)
31+
return
32+
}
33+
if !found {
34+
log.Default().Printf("latest version for %s/%s could not be found from github repository", runtime.GOOS, runtime.GOARCH)
35+
return
36+
}
37+
38+
if latest.LessOrEqual(version) {
39+
log.Printf("Current version (%s) is the latest", version)
40+
return
41+
}
42+
43+
exe, err := os.Executable()
44+
if err != nil {
45+
log.Default().Printf("could not locate executable path")
46+
return
47+
}
48+
49+
log.Printf("Updating to version %s", latest.Version())
50+
if err := selfupdate.UpdateTo(context.Background(), latest.AssetURL, latest.AssetName, exe); err != nil {
51+
log.Default().Printf("error occurred while updating binary: %s", err)
52+
return
53+
}
54+
log.Printf("Successfully updated to version %s", latest.Version())
55+
},
56+
}
57+
58+
func init() {
59+
rootCmd.AddCommand(updateCmd)
60+
}

docs/glctl/commands/update.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
hide:
3+
- toc
4+
---
5+
6+
# Update
7+
8+
glctl supports updating itself. The update command will check for a new version and update itself if a new version is available.
9+
10+
## Usages
11+
12+
### Update glctl
13+
14+
``` sh
15+
$> glctl update
16+
Successfully updated to version x.x.x
17+
```

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/azrod/golink
33
go 1.21.4
44

55
require (
6+
github.com/creativeprojects/go-selfupdate v1.1.3
67
github.com/go-resty/resty/v2 v2.10.0
78
github.com/google/uuid v1.5.0
89
github.com/labstack/echo/v4 v4.11.4
@@ -18,14 +19,18 @@ require (
1819
)
1920

2021
require (
22+
code.gitea.io/sdk/gitea v0.17.1 // indirect
2123
github.com/KyleBanks/depth v1.2.1 // indirect
24+
github.com/Masterminds/semver/v3 v3.2.1 // indirect
2225
github.com/PuerkitoBio/purell v1.1.1 // indirect
2326
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
2427
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2528
github.com/creasty/defaults v1.6.0 // indirect
29+
github.com/davidmz/go-pageant v1.0.2 // indirect
2630
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
2731
github.com/fsnotify/fsnotify v1.7.0 // indirect
2832
github.com/ghodss/yaml v1.0.0 // indirect
33+
github.com/go-fed/httpsig v1.1.0 // indirect
2934
github.com/go-openapi/jsonpointer v0.19.5 // indirect
3035
github.com/go-openapi/jsonreference v0.19.6 // indirect
3136
github.com/go-openapi/spec v0.20.4 // indirect
@@ -34,7 +39,13 @@ require (
3439
github.com/go-playground/universal-translator v0.18.0 // indirect
3540
github.com/go-playground/validator/v10 v10.10.1 // indirect
3641
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
42+
github.com/golang/protobuf v1.5.3 // indirect
3743
github.com/google/go-cmp v0.6.0 // indirect
44+
github.com/google/go-github/v30 v30.1.0 // indirect
45+
github.com/google/go-querystring v1.1.0 // indirect
46+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
47+
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
48+
github.com/hashicorp/go-version v1.6.0 // indirect
3849
github.com/hashicorp/hcl v1.0.0 // indirect
3950
github.com/iamolegga/enviper v1.4.0 // indirect
4051
github.com/inconshreveable/mousetrap v1.1.0 // indirect
@@ -55,17 +66,22 @@ require (
5566
github.com/spf13/pflag v1.0.5 // indirect
5667
github.com/subosito/gotenv v1.6.0 // indirect
5768
github.com/swaggo/files/v2 v2.0.0 // indirect
69+
github.com/ulikunitz/xz v0.5.11 // indirect
5870
github.com/valyala/bytebufferpool v1.0.0 // indirect
5971
github.com/valyala/fasttemplate v1.2.2 // indirect
72+
github.com/xanzy/go-gitlab v0.95.2 // indirect
6073
go.uber.org/atomic v1.9.0 // indirect
6174
go.uber.org/multierr v1.9.0 // indirect
6275
golang.org/x/crypto v0.17.0 // indirect
6376
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
6477
golang.org/x/net v0.19.0 // indirect
78+
golang.org/x/oauth2 v0.15.0 // indirect
6579
golang.org/x/sys v0.15.0 // indirect
6680
golang.org/x/text v0.14.0 // indirect
6781
golang.org/x/time v0.5.0 // indirect
6882
golang.org/x/tools v0.13.0 // indirect
83+
google.golang.org/appengine v1.6.8 // indirect
84+
google.golang.org/protobuf v1.31.0 // indirect
6985
gopkg.in/ini.v1 v1.67.0 // indirect
7086
gopkg.in/yaml.v2 v2.4.0 // indirect
7187
gopkg.in/yaml.v3 v3.0.1 // indirect

0 commit comments

Comments
 (0)