From f59ae21eedbd5b9e7d7a7b359dead60f85481f53 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 5 Apr 2024 08:15:15 -0700 Subject: [PATCH 1/6] Check for latest Cadence 1.0 release --- internal/command/command.go | 61 ++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index a983f1ae6..63f8b7089 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -21,6 +21,7 @@ package command import ( "crypto/sha256" "encoding/base64" + "encoding/json" "errors" "fmt" "io" @@ -260,6 +261,17 @@ func createLogger(logFlag string, formatFlag string) output.Logger { // checkVersion fetches latest version and compares it to local. func checkVersion(logger output.Logger) { + currentVersion := build.Semver() + if isDevelopment() { + return // avoid warning in local development + } + + // If using cadence-v1.0.0 pre-release, check for cadence-v1.0.0 releases instead + if strings.Contains(currentVersion, "cadence-v1.0.0") { + checkVersionCadence1(logger) + return + } + resp, err := http.Get("https://raw.githubusercontent.com/onflow/flow-cli/master/version.txt") if err != nil || resp.StatusCode >= 400 { return @@ -275,12 +287,53 @@ func checkVersion(logger output.Logger) { body, _ := io.ReadAll(resp.Body) latestVersion := strings.TrimSpace(string(body)) - currentVersion := build.Semver() - if isDevelopment() { - return // avoid warning in local development + if currentVersion != latestVersion { + logger.Info(fmt.Sprintf( + "\n%s Version warning: a new version of Flow CLI is available (%s).\n"+ + " Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install\n", + output.WarningEmoji(), + strings.ReplaceAll(latestVersion, "\n", ""), + )) } +} - if currentVersion != latestVersion { +// checkVersion fetches latest version and compares it to local. +func checkVersionCadence1(logger output.Logger) { + resp, err := http.Get("https://api.github.com/repos/onflow/flow-cli/releases?per_page=100") + if err != nil || resp.StatusCode >= 400 { + return + } + + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + logger.Error("error closing request") + } + }(resp.Body) + + body, _ := io.ReadAll(resp.Body) + //parse json + var releases []map[string]interface{} + err = json.Unmarshal(body, &releases) + if err != nil { + return + } + + var latestVersion string + for _, release := range releases { + if release["tag_name"] == nil { + continue + } + + tagName := release["tag_name"].(string) + if strings.Contains(tagName, "cadence-v1.0.0") { + latestVersion = tagName + break + } + } + + currentVersion := build.Semver() + if currentVersion != latestVersion && latestVersion != "" { logger.Info(fmt.Sprintf( "\n%s Version warning: a new version of Flow CLI is available (%s).\n"+ " Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install\n", From c83c5bf5c031ddbcab6bde26075ed48601e32be8 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 5 Apr 2024 08:19:43 -0700 Subject: [PATCH 2/6] add msg --- internal/command/command.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/command/command.go b/internal/command/command.go index 63f8b7089..250a28a85 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -297,7 +297,8 @@ func checkVersion(logger output.Logger) { } } -// checkVersion fetches latest version and compares it to local. +// checkVersionCadence1 fetches latest version of cadence-v1.0.0 and compares it to local. +// This is a special case for cadence-v1.0.0 pre-release & should be removed when cadence-v1.0.0 branch is merged. func checkVersionCadence1(logger output.Logger) { resp, err := http.Get("https://api.github.com/repos/onflow/flow-cli/releases?per_page=100") if err != nil || resp.StatusCode >= 400 { From 304759530e10bbd19247b7f1103c4602c22f02ac Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 5 Apr 2024 08:54:05 -0700 Subject: [PATCH 3/6] update link --- internal/command/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/command/command.go b/internal/command/command.go index 250a28a85..26779443e 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -337,7 +337,7 @@ func checkVersionCadence1(logger output.Logger) { if currentVersion != latestVersion && latestVersion != "" { logger.Info(fmt.Sprintf( "\n%s Version warning: a new version of Flow CLI is available (%s).\n"+ - " Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install\n", + " Read the installation guide for upgrade instructions: https://cadence-lang.org/docs/cadence-migration-guide#install-cadence-10-cli\n", output.WarningEmoji(), strings.ReplaceAll(latestVersion, "\n", ""), )) From 62bf1deda8eb0cb454fcc8b9e59f6c58a886ffea Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 5 Apr 2024 09:08:27 -0700 Subject: [PATCH 4/6] switch to `tags` instead of `releases` for better perf --- internal/command/command.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index 26779443e..4e368c242 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -262,12 +262,9 @@ func createLogger(logFlag string, formatFlag string) output.Logger { // checkVersion fetches latest version and compares it to local. func checkVersion(logger output.Logger) { currentVersion := build.Semver() - if isDevelopment() { - return // avoid warning in local development - } // If using cadence-v1.0.0 pre-release, check for cadence-v1.0.0 releases instead - if strings.Contains(currentVersion, "cadence-v1.0.0") { + if strings.Contains(currentVersion, "cadence-v1.0.0") || true { checkVersionCadence1(logger) return } @@ -300,7 +297,7 @@ func checkVersion(logger output.Logger) { // checkVersionCadence1 fetches latest version of cadence-v1.0.0 and compares it to local. // This is a special case for cadence-v1.0.0 pre-release & should be removed when cadence-v1.0.0 branch is merged. func checkVersionCadence1(logger output.Logger) { - resp, err := http.Get("https://api.github.com/repos/onflow/flow-cli/releases?per_page=100") + resp, err := http.Get("https://api.github.com/repos/onflow/flow-cli/tags?per_page=100") if err != nil || resp.StatusCode >= 400 { return } @@ -313,20 +310,19 @@ func checkVersionCadence1(logger output.Logger) { }(resp.Body) body, _ := io.ReadAll(resp.Body) - //parse json - var releases []map[string]interface{} - err = json.Unmarshal(body, &releases) + var tags []map[string]interface{} + err = json.Unmarshal(body, &tags) if err != nil { return } var latestVersion string - for _, release := range releases { - if release["tag_name"] == nil { + for _, tag := range tags { + if tag["name"] == nil { continue } - tagName := release["tag_name"].(string) + tagName := tag["name"].(string) if strings.Contains(tagName, "cadence-v1.0.0") { latestVersion = tagName break From 523e46db4680e8ec7c5484ab5cf3025a1f339ce4 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 5 Apr 2024 09:10:10 -0700 Subject: [PATCH 5/6] add ok check --- internal/command/command.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index 4e368c242..520bcd8f8 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -318,11 +318,11 @@ func checkVersionCadence1(logger output.Logger) { var latestVersion string for _, tag := range tags { - if tag["name"] == nil { + tagName, ok := tag["name"].(string) + if !ok { continue } - tagName := tag["name"].(string) if strings.Contains(tagName, "cadence-v1.0.0") { latestVersion = tagName break From ed4de9c249b11ef99278bdc961589c4aad217b37 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 5 Apr 2024 09:11:56 -0700 Subject: [PATCH 6/6] restore development check --- internal/command/command.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/command/command.go b/internal/command/command.go index 520bcd8f8..266ab7e37 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -262,9 +262,12 @@ func createLogger(logFlag string, formatFlag string) output.Logger { // checkVersion fetches latest version and compares it to local. func checkVersion(logger output.Logger) { currentVersion := build.Semver() + if isDevelopment() { + return // avoid warning in local development + } // If using cadence-v1.0.0 pre-release, check for cadence-v1.0.0 releases instead - if strings.Contains(currentVersion, "cadence-v1.0.0") || true { + if strings.Contains(currentVersion, "cadence-v1.0.0") { checkVersionCadence1(logger) return }