Skip to content

Commit d5c89f9

Browse files
authored
Merge pull request #183 from rios0rios0/feat/add-update-check
feat(selfupdate): added automatic version check on CLI startup
2 parents be3b2de + f0810b8 commit d5c89f9

4 files changed

Lines changed: 25 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Exceptions are acceptable depending on the circumstances (critical bug fixes tha
1616

1717
## [Unreleased]
1818

19+
### Added
20+
21+
- added automatic version check on CLI startup using `CheckForUpdates()`
22+
1923
### Changed
2024

2125
- changed the Go module dependencies to their latest versions

cmd/terra/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55

66
"github.com/joho/godotenv"
7+
"github.com/rios0rios0/cliforge/pkg/selfupdate"
78
"github.com/rios0rios0/terra/internal/domain/commands"
89
"github.com/rios0rios0/terra/internal/domain/entities"
910
logger "github.com/sirupsen/logrus"
@@ -16,6 +17,20 @@ import (
1617

1718
var version = "dev"
1819

20+
// runUpdateCheck queries the cliforge selfupdate command for a newer version,
21+
// skipping local dev builds and the self-update / version subcommands to avoid
22+
// redundant GitHub API calls and noisy warnings.
23+
func runUpdateCheck(command *cobra.Command) {
24+
if commands.TerraVersion == "dev" {
25+
return
26+
}
27+
switch command.Name() {
28+
case "self-update", "version":
29+
return
30+
}
31+
selfupdate.NewCommand("rios0rios0", "terra", "terra", commands.TerraVersion).CheckForUpdates()
32+
}
33+
1934
// buildRootCommand creates and configures the root cobra command.
2035
func buildRootCommand(rootController entities.Controller, enableFlagParsing bool) *cobra.Command {
2136
bind := rootController.GetBind()
@@ -24,6 +39,9 @@ func buildRootCommand(rootController entities.Controller, enableFlagParsing bool
2439
Use: bind.Use,
2540
Short: bind.Short,
2641
Long: bind.Long,
42+
PersistentPreRun: func(command *cobra.Command, _ []string) {
43+
runUpdateCheck(command)
44+
},
2745
Run: func(command *cobra.Command, arguments []string) {
2846
rootController.Execute(command, arguments)
2947
},

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/go-playground/validator/v10 v10.30.2
88
github.com/joho/godotenv v1.5.1
99
github.com/kelseyhightower/envconfig v1.4.0
10-
github.com/rios0rios0/cliforge v0.2.0
10+
github.com/rios0rios0/cliforge v0.2.1-0.20260404230949-b9683f7eded2
1111
github.com/rios0rios0/testkit v0.1.1
1212
github.com/sirupsen/logrus v1.9.4
1313
github.com/spf13/cobra v1.10.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjS
3232
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
3333
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3434
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
35-
github.com/rios0rios0/cliforge v0.2.0 h1:wy+bvUzsx1+ZXjOa9McLqppTQFPaSnx3ISf6EhW7Z2U=
36-
github.com/rios0rios0/cliforge v0.2.0/go.mod h1:xqHRf5C15as2KOOTXdyNZQnfExtXOpSkRljkhZ4hVsQ=
35+
github.com/rios0rios0/cliforge v0.2.1-0.20260404230949-b9683f7eded2 h1:cRuv/NTZ7B7UzTzO3JQy8VQlYxo45xC0RPhnVN6Ar7k=
36+
github.com/rios0rios0/cliforge v0.2.1-0.20260404230949-b9683f7eded2/go.mod h1:Ma0C18zImHW305Ezt+XCsmUmtSj3BM1mcBk7G5lttv0=
3737
github.com/rios0rios0/testkit v0.1.1 h1:EIMz9S2fo+19ESdQ7FL8bUap0Xf7Z2zlbgV6gnuHtd4=
3838
github.com/rios0rios0/testkit v0.1.1/go.mod h1:4yNptfPTrFn1TzsvZIvQjIia4PMl8hH8u+dqnLopmQA=
3939
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=

0 commit comments

Comments
 (0)