Skip to content

Commit 39bcbd5

Browse files
committed
feat: Check for new version of the CLI when running commands
Signed-off-by: Arthur Amstutz <arthur.amstutz@corp.ovh.com>
1 parent 8b3ccdd commit 39bcbd5

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

internal/cmd/root.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package cmd
77
import (
88
"encoding/json"
99
"fmt"
10+
"log"
11+
"net/http"
1012
"os"
1113

1214
"github.com/spf13/cobra"
@@ -16,7 +18,8 @@ import (
1618
"github.com/ovh/ovhcloud-cli/internal/config"
1719
"github.com/ovh/ovhcloud-cli/internal/display"
1820
"github.com/ovh/ovhcloud-cli/internal/flags"
19-
"github.com/ovh/ovhcloud-cli/internal/http"
21+
httplib "github.com/ovh/ovhcloud-cli/internal/http"
22+
"github.com/ovh/ovhcloud-cli/internal/version"
2023
)
2124

2225
// rootCmd represents the base command when called without any subcommands
@@ -117,15 +120,39 @@ Examples:
117120
rootCmd.MarkFlagsMutuallyExclusive("json", "yaml", "interactive", "format")
118121

119122
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
120-
if http.Client == nil {
123+
// Check if a new version is available in a separate goroutine
124+
go func() {
125+
const latestURL = "https://github.com/ovh/ovhcloud-cli/releases/latest"
126+
req, err := http.NewRequest("GET", latestURL, nil)
127+
if err != nil {
128+
return
129+
}
130+
req.Header.Set("Accept", "application/json")
131+
resp, err := http.DefaultClient.Do(req)
132+
if err != nil {
133+
return
134+
}
135+
defer resp.Body.Close()
136+
var data struct {
137+
TagName string `json:"tag_name"`
138+
}
139+
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
140+
return
141+
}
142+
if data.TagName != "" && data.TagName != version.Version {
143+
log.Printf("A new version of ovhcloud-cli is available: %s (current: %s)\n", data.TagName, version.Version)
144+
}
145+
}()
146+
147+
if httplib.Client == nil {
121148
display.OutputError(&flags.OutputFormatConfig, "API client is not initialized, please run `ovhcloud login` to authenticate")
122149
os.Exit(1) // Force os.Exit even in WASM mode
123150
}
124151
}
125152
}
126153

127154
func init() {
128-
http.InitClient()
155+
httplib.InitClient()
129156

130157
// Load configuration files by order of increasing priority. All configuration
131158
// files are optional. Only load file from user home if home could be resolve

0 commit comments

Comments
 (0)