@@ -7,6 +7,10 @@ import (
77 "path/filepath"
88 "reflow/cmd/deploy"
99 "reflow/cmd/destroy"
10+ "reflow/cmd/version"
11+ "reflow/internal/update"
12+ "sync"
13+ "time"
1014
1115 "github.com/spf13/cobra"
1216 "github.com/spf13/viper"
@@ -15,8 +19,10 @@ import (
1519)
1620
1721var (
18- debug bool
19- cfgFileBase string
22+ debug bool
23+ cfgFileBase string
24+ updateCheckStarted bool
25+ updateCheckMutex sync.Mutex
2026)
2127
2228// rootCmd represents the base command when called without any subcommands
@@ -62,6 +68,54 @@ implementing a blue-green deployment strategy to minimize downtime.`,
6268 util .Log .Debug ("Debug mode enabled by flag, overriding config file setting if it was false." )
6369 }
6470 }
71+
72+ // --- Perform Update Check (in background) ---
73+ if cmd .Name () != "version" {
74+ updateCheckMutex .Lock ()
75+ shouldStartCheck := ! updateCheckStarted
76+ if shouldStartCheck {
77+ updateCheckStarted = true
78+ }
79+ updateCheckMutex .Unlock ()
80+
81+ if shouldStartCheck {
82+ go func () {
83+ repo := version .GetRepository ()
84+ currentVersion := version .GetVersion ()
85+
86+ if repo == "" || currentVersion == "dev" {
87+ util .Log .Debug ("Skipping update check (repo not set or dev version)." )
88+ return
89+ }
90+
91+ cacheDir := filepath .Join (cfgFileBase , ".reflow-state" )
92+ cachePath := filepath .Join (cacheDir , update .CacheFileName )
93+
94+ util .Log .Debugf ("Initiating background update check for repo: %s" , repo )
95+ result , checkErr := update .CheckForUpdate (currentVersion , repo , cachePath , 24 * time .Hour )
96+
97+ if checkErr != nil {
98+ util .Log .Debugf ("Update check failed: %v" , checkErr )
99+ return
100+ }
101+
102+ if result != nil && result .IsNewer {
103+ fmt .Fprintf (os .Stderr , "\n ---\n " )
104+ fmt .Fprintf (os .Stderr , "[Reflow Update Available]\n " )
105+ fmt .Fprintf (os .Stderr , " Your version: %s\n " , currentVersion )
106+ fmt .Fprintf (os .Stderr , " Latest version:%s\n " , result .LatestVersion )
107+ fmt .Fprintf (os .Stderr , " Release notes: %s\n " , result .ReleaseURL )
108+ fmt .Fprintf (os .Stderr , " Consider updating by visiting the release page or rebuilding from source.\n " )
109+ fmt .Fprintf (os .Stderr , "---\n \n " )
110+ } else if result != nil {
111+ util .Log .Debugf ("Update check completed. Version '%s' is up-to-date." , currentVersion )
112+ }
113+ }()
114+ } else {
115+ util .Log .Debug ("Update check already initiated for this execution." )
116+ }
117+ }
118+
65119 return nil
66120 },
67121}
@@ -81,6 +135,7 @@ func init() {
81135 deploy .AddDeployCommand (rootCmd )
82136 deploy .AddApproveCommand (rootCmd )
83137 destroy .AddDestroyCommand (rootCmd )
138+ version .AddVersionCommand (rootCmd )
84139}
85140
86141// GetReflowBasePath allows other commands (like init) to access the calculated base path
0 commit comments