From d63de49e3be131fc3d92d8659166f70b8f0468df Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 26 Apr 2024 11:14:40 -0700 Subject: [PATCH] Use Homebrew latest version instead of version.txt (#1545) --- CONTRIBUTING.md | 2 +- internal/command/command.go | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ce3a5da7c..57f9d1285 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -159,7 +159,7 @@ You can also release manually although this is not recommended: To make the new version the default version that is installed -- Change `version.txt` and commit it +- **DEPRECATED** Change `version.txt` and commit it. (This file is no longer user for versions of the Flow CLI later than v1.18.0, although it should still be maintained to support older versions of the CLI for a while) ## Adding a scaffold You can add your own scaffold by creating a GitHub repository containing the scaffold content and then making a PR diff --git a/internal/command/command.go b/internal/command/command.go index cebdd608f..7c62c2755 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,7 +261,7 @@ func createLogger(logFlag string, formatFlag string) output.Logger { // checkVersion fetches latest version and compares it to local. func checkVersion(logger output.Logger) { - resp, err := http.Get("https://raw.githubusercontent.com/onflow/flow-cli/master/version.txt") + resp, err := http.Get("https://formulae.brew.sh/api/formula/flow-cli.json") if err != nil || resp.StatusCode >= 400 { return } @@ -273,7 +274,21 @@ func checkVersion(logger output.Logger) { }(resp.Body) body, _ := io.ReadAll(resp.Body) - latestVersion := strings.TrimSpace(string(body)) + var data map[string]interface{} + err = json.Unmarshal(body, &data) + if err != nil { + return + } + + versions, ok := data["versions"].(map[string]interface{}) + if !ok { + return + } + + latestVersion, ok := versions["stable"].(string) + if !ok { + return + } currentVersion := build.Semver() if isDevelopment() { @@ -282,7 +297,7 @@ func checkVersion(logger output.Logger) { if currentVersion != latestVersion { logger.Info(fmt.Sprintf( - "\n%s Version warning: a new version of Flow CLI is available (%s).\n"+ + "\n%s Version warning: a new version of Flow CLI is available (v%s).\n"+ " Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install\n", output.WarningEmoji(), strings.ReplaceAll(latestVersion, "\n", ""),