From a6411eddf2380a988896070a78ff56845861cb98 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Fri, 9 Jun 2023 16:07:28 +0200 Subject: [PATCH 1/4] fix wrong type assertion --- internal/cli/core/upgrade.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/cli/core/upgrade.go b/internal/cli/core/upgrade.go index 3884bdd8129..8020c939c18 100644 --- a/internal/cli/core/upgrade.go +++ b/internal/cli/core/upgrade.go @@ -17,7 +17,6 @@ package core import ( "context" - "errors" "fmt" "os" @@ -110,7 +109,7 @@ func Upgrade(inst *rpc.Instance, args []string, skipPostInstall bool) { response, err := core.PlatformUpgrade(context.Background(), r, feedback.ProgressBar(), feedback.TaskProgress()) warningMissingIndex(response) if err != nil { - if errors.Is(err, &arduino.PlatformAlreadyAtTheLatestVersionError{}) { + if _, ok := err.(*arduino.PlatformAlreadyAtTheLatestVersionError); ok { feedback.Print(err.Error()) continue } From 074965db43bae337c441320ad70bf08ce8858809 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Fri, 9 Jun 2023 16:34:35 +0200 Subject: [PATCH 2/4] use feedback.Fatal otherwise it won't flush the stdout as json format --- internal/cli/core/upgrade.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/core/upgrade.go b/internal/cli/core/upgrade.go index 8020c939c18..bfd2694b964 100644 --- a/internal/cli/core/upgrade.go +++ b/internal/cli/core/upgrade.go @@ -110,7 +110,7 @@ func Upgrade(inst *rpc.Instance, args []string, skipPostInstall bool) { warningMissingIndex(response) if err != nil { if _, ok := err.(*arduino.PlatformAlreadyAtTheLatestVersionError); ok { - feedback.Print(err.Error()) + feedback.Fatal(err.Error(), feedback.Success) continue } From 27f9b052a57bba743c31ec555f8f04fc1dcc619f Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Wed, 14 Jun 2023 09:27:26 +0200 Subject: [PATCH 3/4] use feedback.PrintResult instead of using fatal --- internal/cli/core/upgrade.go | 17 ++++++++++++++++- internal/integrationtest/core/core_test.go | 8 ++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/cli/core/upgrade.go b/internal/cli/core/upgrade.go index bfd2694b964..d99edab089d 100644 --- a/internal/cli/core/upgrade.go +++ b/internal/cli/core/upgrade.go @@ -110,7 +110,7 @@ func Upgrade(inst *rpc.Instance, args []string, skipPostInstall bool) { warningMissingIndex(response) if err != nil { if _, ok := err.(*arduino.PlatformAlreadyAtTheLatestVersionError); ok { - feedback.Fatal(err.Error(), feedback.Success) + feedback.Warning(err.Error()) continue } @@ -121,4 +121,19 @@ func Upgrade(inst *rpc.Instance, args []string, skipPostInstall bool) { if hasBadArguments { feedback.Fatal(tr("Some upgrades failed, please check the output for details."), feedback.ErrBadArgument) } + + feedback.PrintResult(&platformUpgradeResult{}) +} + +// This is needed so we can print warning messages in case users use --format json +type platformUpgradeResult struct{} + +// Data implements feedback.Result. +func (r *platformUpgradeResult) Data() interface{} { + return r +} + +// String implements feedback.Result. +func (r *platformUpgradeResult) String() string { + return "" } diff --git a/internal/integrationtest/core/core_test.go b/internal/integrationtest/core/core_test.go index da13160ee1e..b2b68dd366e 100644 --- a/internal/integrationtest/core/core_test.go +++ b/internal/integrationtest/core/core_test.go @@ -1041,8 +1041,8 @@ func TestCoreUpgradeWarningWithPackageInstalledButNotIndexed(t *testing.T) { _, _, err = cli.Run("core", "install", "test:x86@1.0.0", "--additional-urls="+url) require.NoError(t, err) //upgrade without index fires a warning - _, jsonStderr, _ := cli.Run("core", "upgrade", "test:x86", "--format", "json") - requirejson.Query(t, jsonStderr, ".warnings[]", `"missing package index for test:x86, future updates cannot be guaranteed"`) + jsonStdout, _, _ := cli.Run("core", "upgrade", "test:x86", "--format", "json") + requirejson.Query(t, jsonStdout, ".warnings[]", `"missing package index for test:x86, future updates cannot be guaranteed"`) }) // removing installed.json @@ -1050,7 +1050,7 @@ func TestCoreUpgradeWarningWithPackageInstalledButNotIndexed(t *testing.T) { require.NoError(t, os.Remove(installedJson.String())) t.Run("missing both installed.json and additional-urls", func(t *testing.T) { - _, jsonStderr, _ := cli.Run("core", "upgrade", "test:x86", "--format", "json") - requirejson.Query(t, jsonStderr, ".warnings[]", `"missing package index for test:x86, future updates cannot be guaranteed"`) + jsonStdout, _, _ := cli.Run("core", "upgrade", "test:x86", "--format", "json") + requirejson.Query(t, jsonStdout, ".warnings[]", `"missing package index for test:x86, future updates cannot be guaranteed"`) }) } From 537807bee4d3dc4bf38408f9d35138e538709626 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Wed, 14 Jun 2023 10:05:50 +0200 Subject: [PATCH 4/4] use errors.As in case in the future the err coming from PlatformUpgrade might be wrapped errors.As search through all the wrapped error for our target type, in case it finds it then it popolate that struct but more important we can use it as a type assertion even if is nested through many errors --- internal/cli/core/upgrade.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/cli/core/upgrade.go b/internal/cli/core/upgrade.go index d99edab089d..54ce8e5bfc8 100644 --- a/internal/cli/core/upgrade.go +++ b/internal/cli/core/upgrade.go @@ -17,6 +17,7 @@ package core import ( "context" + "errors" "fmt" "os" @@ -109,7 +110,8 @@ func Upgrade(inst *rpc.Instance, args []string, skipPostInstall bool) { response, err := core.PlatformUpgrade(context.Background(), r, feedback.ProgressBar(), feedback.TaskProgress()) warningMissingIndex(response) if err != nil { - if _, ok := err.(*arduino.PlatformAlreadyAtTheLatestVersionError); ok { + var alreadyAtLatestVersionErr *arduino.PlatformAlreadyAtTheLatestVersionError + if errors.As(err, &alreadyAtLatestVersionErr) { feedback.Warning(err.Error()) continue }