From d26332a2e046ee0891ca2261e8af3d972d07a805 Mon Sep 17 00:00:00 2001 From: Abhijit Wakchaure Date: Tue, 18 Oct 2022 17:59:52 +0530 Subject: [PATCH] fix 7-app-update-is-not-working --- VERSION | 2 +- app/app.go | 9 +++++++++ cmd/root.go | 4 ++-- config/config.go | 2 +- software/software.go | 30 ++++++++++++++++++++++-------- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/VERSION b/VERSION index b02d37b..201271f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v2.0.2 \ No newline at end of file +v2.0.3 \ No newline at end of file diff --git a/app/app.go b/app/app.go index 97edcbf..3687ed5 100644 --- a/app/app.go +++ b/app/app.go @@ -11,6 +11,7 @@ import ( "github.com/abhijitWakchaure/run-flogo-app/config" "github.com/abhijitWakchaure/run-flogo-app/files" "github.com/abhijitWakchaure/run-flogo-app/software" + "github.com/spf13/viper" ) // App holds the environment variables for the user @@ -46,6 +47,7 @@ func NewApp(appConfig *config.AppConfig, updateConfig *software.UpdateConfig) *A if a.AppsDir == "" { a.AppsDir = filepath.Join(config.GetUserHomeDir(), "Downloads") } + WriteAppConfig(a.AppConfig) software.PrintUpdateInfo(a.UpdateConfig) return a } @@ -59,6 +61,13 @@ func (a *App) PrintConfig() { config.Print(c) } +// WriteAppConfig will write the app config +func WriteAppConfig(appConfig *config.AppConfig) { + viper.Set("appsDir", appConfig.AppsDir) + viper.Set("appPattern", appConfig.AppPattern) + viper.WriteConfig() +} + // RunLatestApp will run the latest app func (a *App) RunLatestApp(logLevel string, args []string) { latestFlogoApp := files.FindLatestApp(a.AppsDir, a.AppPattern) diff --git a/cmd/root.go b/cmd/root.go index 868c234..f226e53 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,8 +30,7 @@ var rootCmd = &cobra.Command{ name, _ := cmd.Flags().GetString("name") go func() { updateConfig := software.CheckForUpdates() - // TODO: write update config - _ = updateConfig + software.WriteUpdateConfig(updateConfig) }() logLevel := config.LogLevelInfo if trace { @@ -115,6 +114,7 @@ func initConfig() { if err := viper.ReadInConfig(); err == nil { fmt.Printf("i> Using config file: %s\n", viper.ConfigFileUsed()) } + appsDir := viper.GetString("appsDir") appPattern := viper.GetString("appPattern") isUpdateAvailable := viper.GetBool("isUpdateAvailable") diff --git a/config/config.go b/config/config.go index 5cd768e..fb0bfaa 100644 --- a/config/config.go +++ b/config/config.go @@ -9,7 +9,7 @@ import ( ) // VERSION ... -var VERSION string +var VERSION = "undefined" // AppConfig ... type AppConfig struct { diff --git a/software/software.go b/software/software.go index a23d9df..cd0cc77 100644 --- a/software/software.go +++ b/software/software.go @@ -13,6 +13,7 @@ import ( "strings" "github.com/abhijitWakchaure/run-flogo-app/config" + "github.com/spf13/viper" ) // UpdateConfig ... @@ -104,6 +105,7 @@ func Uninstall(installPath string) { func CheckForUpdates() *UpdateConfig { resp, err := http.Get(config.GithubLastestReleaseURL) if err != nil { + fmt.Printf("\n\nE> run-flogo-app Error: ERR_CHKUPDATE_HTTPGET %s\n", err) return nil } defer resp.Body.Close() @@ -118,27 +120,39 @@ func CheckForUpdates() *UpdateConfig { fmt.Printf("\nE> run-flogo-app Error: ERR_CHKUPDATE_DECODE %s\n", err) return nil } + OSAndArch := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH) for _, d := range assets { durl := d.(map[string]interface{})["browser_download_url"].(string) - if strings.Contains(durl, runtime.GOOS) && !strings.Contains(durl, config.VERSION) { - return &UpdateConfig{ - IsUpdateAvailable: true, - UpdateURL: durl, - ReleaseNotes: strings.Replace(strings.TrimSpace(gitdata["body"].(string)), "\n", "\n\t", -1), - } - } else if strings.Contains(durl, runtime.GOOS) { + if !strings.Contains(durl, OSAndArch) { + continue + } + if strings.Contains(durl, config.VERSION) { // fmt.Println() // fmt.Println("Your app is up to date 👍") return nil } + return &UpdateConfig{ + IsUpdateAvailable: true, + UpdateURL: durl, + ReleaseNotes: strings.Replace(strings.TrimSpace(gitdata["body"].(string)), "\n", "\n\t", -1), + } + } return nil } +// WriteUpdateConfig will write the update info +func WriteUpdateConfig(updateConfig *UpdateConfig) { + viper.Set("isUpdateAvailable", updateConfig.IsUpdateAvailable) + viper.Set("updateURL", updateConfig.UpdateURL) + viper.Set("releaseNotes", updateConfig.ReleaseNotes) + viper.WriteConfig() +} + // PrintUpdateInfo will print the update info func PrintUpdateInfo(updateConfig *UpdateConfig) { if updateConfig.IsUpdateAvailable { - fmt.Println("#> New version of the app is available at:", updateConfig.UpdateURL) + fmt.Println("#> New version of the app is available!") fmt.Println("#> Release Notes:") fmt.Printf("\t%s\n\n", updateConfig.ReleaseNotes) }