Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for latest Cadence 1.0 release #1496

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package command
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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
Expand All @@ -275,12 +287,54 @@ 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 {
// 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 {
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",
Expand Down
Loading