Skip to content

Commit 77adbfa

Browse files
authored
Merge pull request #46 from coollabsio/update-check-every-cmd
Check for CLI updates on every command
2 parents 26c0925 + 9215fd5 commit 77adbfa

File tree

3 files changed

+304
-63
lines changed

3 files changed

+304
-63
lines changed

cmd/root.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"log"
77
"os"
88

9-
compareVersion "github.com/hashicorp/go-version"
109
"github.com/spf13/cobra"
1110
"github.com/spf13/viper"
1211

@@ -144,24 +143,6 @@ func initConfig() {
144143
// They are loaded on-demand by getAPIClient() based on --instance or default instance
145144
// This allows --instance flag to work correctly
146145

147-
// Check for updates
148-
latestVersionStr, err := version.CheckLatestVersionOfCli(Debug)
149-
if err != nil {
150-
if Debug {
151-
log.Println("Failed to check for updates:", err)
152-
}
153-
}
154-
155-
// Compare versions properly using semantic versioning
156-
if latestVersionStr != "" {
157-
latestVersion, err := compareVersion.NewVersion(latestVersionStr)
158-
if err == nil {
159-
currentVersion, err := compareVersion.NewVersion(version.GetVersion())
160-
if err == nil && latestVersion.GreaterThan(currentVersion) {
161-
if Debug {
162-
log.Printf("New version of Coolify CLI is available: %s\n", latestVersionStr)
163-
}
164-
}
165-
}
166-
}
146+
// Check for updates (errors are handled silently inside the function)
147+
_, _ = version.CheckLatestVersionOfCli(Debug)
167148
}

internal/version/checker.go

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,92 +5,90 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"log"
98
"net/http"
109
"sort"
1110
"time"
1211

1312
compareVersion "github.com/hashicorp/go-version"
14-
"github.com/spf13/viper"
1513
)
1614

1715
// Version variables injected by GoReleaser at build time via ldflags
1816
var (
1917
version = "v1.1"
2018
)
2119

20+
// GitHubAPIURL is the URL for fetching CLI version tags (exported for testing)
21+
var GitHubAPIURL = "https://api.github.com/repos/coollabsio/coolify-cli/git/refs/tags"
22+
2223
func GetVersion() string {
2324
return version
2425
}
2526

26-
// CheckInterval for version checking
27-
const CheckInterval = 10 * time.Minute
28-
2927
// Tag represents a git tag for version checking
3028
type Tag struct {
3129
Ref string `json:"ref"`
3230
}
3331

34-
// CheckLatestVersionOfCli checks for CLI updates
35-
func CheckLatestVersionOfCli(debug bool) (string, error) {
36-
lastCheck := viper.GetString("lastupdatechecktime")
37-
if lastCheck != "" {
38-
lastCheckTime, err := time.Parse(time.RFC3339, lastCheck)
39-
if err == nil && lastCheckTime.Add(CheckInterval).After(time.Now()) {
40-
if debug {
41-
log.Println("Skipping update check. Last check was less than 10 minutes ago.")
42-
}
43-
return GetVersion(), nil
44-
}
45-
}
46-
47-
// Update check time
48-
viper.Set("lastupdatechecktime", time.Now().Format(time.RFC3339))
49-
if err := viper.WriteConfig(); err != nil {
50-
log.Printf("Failed to write config: %v\n", err)
51-
}
32+
// CheckLatestVersionOfCli checks for CLI updates on every command.
33+
// Errors are handled silently - the function returns without printing anything
34+
// if the GitHub API call fails.
35+
func CheckLatestVersionOfCli(_ bool) (string, error) {
36+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
37+
defer cancel()
5238

53-
url := "https://api.github.com/repos/coollabsio/coolify-cli/git/refs/tags"
54-
ctx := context.Background()
55-
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
39+
req, err := http.NewRequestWithContext(ctx, "GET", GitHubAPIURL, nil)
5640
if err != nil {
57-
return "", err
41+
return "", nil // Silent fail
5842
}
5943

6044
client := &http.Client{}
6145
resp, err := client.Do(req)
6246
if err != nil {
63-
return "", err
47+
return "", nil // Silent fail
6448
}
6549
defer resp.Body.Close()
6650

67-
body, err := io.ReadAll(resp.Body)
68-
if err != nil {
69-
return "", err
51+
if resp.StatusCode != 200 {
52+
return "", nil // Silent fail
7053
}
7154

72-
if resp.StatusCode != 200 {
73-
return "", fmt.Errorf("%d - Failed to fetch data from %s. Error: %s", resp.StatusCode, url, string(body))
55+
body, err := io.ReadAll(resp.Body)
56+
if err != nil {
57+
return "", nil // Silent fail
7458
}
7559

7660
var tags []Tag
7761
if err := json.Unmarshal(body, &tags); err != nil {
78-
return "", err
62+
return "", nil // Silent fail
63+
}
64+
65+
if len(tags) == 0 {
66+
return "", nil // Silent fail
7967
}
8068

8169
versionsRaw := make([]string, 0, len(tags))
8270
for _, tag := range tags {
83-
versionStr := tag.Ref[10:]
84-
versionsRaw = append(versionsRaw, versionStr)
71+
if len(tag.Ref) > 10 {
72+
versionStr := tag.Ref[10:]
73+
versionsRaw = append(versionsRaw, versionStr)
74+
}
8575
}
8676

87-
versions := make([]*compareVersion.Version, len(versionsRaw))
88-
for i, raw := range versionsRaw {
77+
if len(versionsRaw) == 0 {
78+
return "", nil // Silent fail
79+
}
80+
81+
versions := make([]*compareVersion.Version, 0, len(versionsRaw))
82+
for _, raw := range versionsRaw {
8983
v, err := compareVersion.NewVersion(raw)
9084
if err != nil {
91-
return "", err
85+
continue // Skip invalid versions
9286
}
93-
versions[i] = v
87+
versions = append(versions, v)
88+
}
89+
90+
if len(versions) == 0 {
91+
return "", nil // Silent fail
9492
}
9593

9694
sort.Sort(compareVersion.Collection(versions))
@@ -99,11 +97,11 @@ func CheckLatestVersionOfCli(debug bool) (string, error) {
9997
// Compare versions properly using semantic versioning
10098
currentVersion, err := compareVersion.NewVersion(GetVersion())
10199
if err != nil {
102-
return latestVersion.String(), err
100+
return "", nil // Silent fail
103101
}
104102

105103
if latestVersion.GreaterThan(currentVersion) {
106-
fmt.Printf("There is a new version of Coolify CLI available.\nPlease update with 'coolify update'.\n\n")
104+
fmt.Printf("A new version (%s) is available. Update with: coolify update\n", latestVersion.String())
107105
}
108106
return latestVersion.String(), nil
109107
}

0 commit comments

Comments
 (0)