@@ -7,7 +7,10 @@ package cmd
77import (
88 "encoding/json"
99 "fmt"
10+ "log"
11+ "net/http"
1012 "os"
13+ "runtime"
1114
1215 "github.com/spf13/cobra"
1316 "github.com/spf13/pflag"
@@ -16,7 +19,8 @@ import (
1619 "github.com/ovh/ovhcloud-cli/internal/config"
1720 "github.com/ovh/ovhcloud-cli/internal/display"
1821 "github.com/ovh/ovhcloud-cli/internal/flags"
19- "github.com/ovh/ovhcloud-cli/internal/http"
22+ httplib "github.com/ovh/ovhcloud-cli/internal/http"
23+ "github.com/ovh/ovhcloud-cli/internal/version"
2024)
2125
2226// rootCmd represents the base command when called without any subcommands
@@ -116,16 +120,58 @@ Examples:
116120 --format '(nbFieldA + nbFieldB) * 10' (to compute values from numeric fields)` )
117121 rootCmd .MarkFlagsMutuallyExclusive ("json" , "yaml" , "interactive" , "format" )
118122
123+ var newVersionMessage * string
119124 rootCmd .PersistentPreRun = func (cmd * cobra.Command , args []string ) {
120- if http .Client == nil {
125+ // Check if a new version is available in a separate goroutine
126+ // Don't do it when running in WASM binary
127+ if ! (runtime .GOARCH == "wasm" && runtime .GOOS == "js" ) {
128+ go func () {
129+ // Skip version check if version is undefined (development mode)
130+ if version .Version == "undefined" {
131+ return
132+ }
133+
134+ const latestURL = "https://github.com/ovh/ovhcloud-cli/releases/latest"
135+ req , err := http .NewRequest ("GET" , latestURL , nil )
136+ if err != nil {
137+ return
138+ }
139+ req .Header .Set ("Accept" , "application/json" )
140+ resp , err := http .DefaultClient .Do (req )
141+ if err != nil {
142+ return
143+ }
144+ defer resp .Body .Close ()
145+ var data struct {
146+ TagName string `json:"tag_name"`
147+ }
148+ if err := json .NewDecoder (resp .Body ).Decode (& data ); err != nil {
149+ return
150+ }
151+ if data .TagName != "" && data .TagName != version .Version {
152+ message := fmt .Sprintf ("A new version of ovhcloud-cli is available: %s (current: %s)" , data .TagName , version .Version )
153+ newVersionMessage = & message
154+ }
155+ }()
156+ }
157+
158+ // Check if the API client is initialized
159+ if httplib .Client == nil {
121160 display .OutputError (& flags .OutputFormatConfig , "API client is not initialized, please run `ovhcloud login` to authenticate" )
122161 os .Exit (1 ) // Force os.Exit even in WASM mode
123162 }
124163 }
164+
165+ // Set PostRun to display the new version message if available
166+ rootCmd .PersistentPostRun = func (cmd * cobra.Command , args []string ) {
167+ if newVersionMessage != nil {
168+ log .Println (* newVersionMessage )
169+ }
170+ }
125171}
126172
127173func init () {
128- http .InitClient ()
174+ httplib .InitClient ()
129175
130176 // Load configuration files by order of increasing priority. All configuration
131177 // files are optional. Only load file from user home if home could be resolve
0 commit comments