@@ -16,8 +16,10 @@ import (
1616 "strings"
1717 "time"
1818
19+ "github.com/Masterminds/semver/v3"
1920 "github.com/spf13/cobra"
2021
22+ "github.com/smartcontractkit/cre-cli/cmd/version"
2123 "github.com/smartcontractkit/cre-cli/internal/runtime"
2224)
2325
@@ -294,12 +296,47 @@ func replaceSelf(newBin string) error {
294296 return os .Rename (newBin , self )
295297}
296298
297- func Run () error {
298- fmt .Println ("Updating cre CLI..." )
299+ // Run accepts the currentVersion string
300+ func Run (currentVersion string ) error {
301+ fmt .Println ("Checking for updates..." )
299302 tag , err := getLatestTag ()
300303 if err != nil {
301304 return fmt .Errorf ("error fetching latest version: %w" , err )
302305 }
306+
307+ // --- New Update Check Logic ---
308+ // Clean the current version string (e.g., "version v1.2.3" -> "v1.2.3")
309+ cleanedCurrent := strings .Replace (currentVersion , "version" , "" , 1 )
310+ cleanedCurrent = strings .TrimSpace (cleanedCurrent )
311+
312+ // Clean the latest tag (e.g., "v1.2.4")
313+ cleanedLatest := strings .TrimSpace (tag )
314+
315+ currentSemVer , errCurrent := semver .NewVersion (cleanedCurrent )
316+ latestSemVer , errLatest := semver .NewVersion (cleanedLatest )
317+
318+ if errCurrent != nil || errLatest != nil {
319+ // If we can't parse either version, fall back to just updating.
320+ // Print a warning to stderr.
321+ fmt .Fprintf (os .Stderr , "Warning: could not compare versions (current: '%s', latest: '%s'). Proceeding with update.\n " , cleanedCurrent , cleanedLatest )
322+ if errCurrent != nil {
323+ fmt .Fprintf (os .Stderr , "Current version parse error: %v\n " , errCurrent )
324+ }
325+ if errLatest != nil {
326+ fmt .Fprintf (os .Stderr , "Latest version parse error: %v\n " , errLatest )
327+ }
328+ } else {
329+ // Compare versions
330+ if latestSemVer .LessThan (currentSemVer ) || latestSemVer .Equal (currentSemVer ) {
331+ fmt .Printf ("You are already using the latest version %s\n " , currentSemVer .String ())
332+ return nil // Skip the update
333+ }
334+ }
335+ // --- End of New Logic ---
336+
337+ // If we're here, an update is needed.
338+ fmt .Println ("Updating cre CLI..." )
339+
303340 asset , _ , err := getAssetName ()
304341 if err != nil {
305342 return fmt .Errorf ("error determining asset name: %w" , err )
@@ -337,12 +374,13 @@ func Run() error {
337374 return nil
338375}
339376
340- func New (_ * runtime.Context ) * cobra.Command {
377+ // New is modified to use the version package
378+ func New (_ * runtime.Context ) * cobra.Command { // <-- No longer uses rt
341379 var versionCmd = & cobra.Command {
342380 Use : "update" ,
343381 Short : "Update the cre CLI to the latest version" ,
344382 RunE : func (cmd * cobra.Command , args []string ) error {
345- return Run ()
383+ return Run (version . Version )
346384 },
347385 }
348386
0 commit comments