From 2f25804a47c338575330d36cce65bde3be44d215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Kivim=C3=A4ki?= Date: Wed, 10 Jan 2024 12:14:39 +0200 Subject: [PATCH] chore: give user upgrade command when checking for updates --- internal/cli/check.go | 17 ++++++++++++++--- test/check-recipes.feature | 2 ++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/cli/check.go b/internal/cli/check.go index 67c51fb2..4fb2bc40 100644 --- a/internal/cli/check.go +++ b/internal/cli/check.go @@ -3,6 +3,7 @@ package cli import ( "context" "fmt" + "os" "strings" "github.com/futurice/jalapeno/internal/cli/option" @@ -105,7 +106,8 @@ func runCheck(cmd *cobra.Command, opts checkOptions) error { cmd.Println("Checking for new versions...") - updatesAvailable, errorsFound := false, false + errorsFound := false + upgradeCommands := make([]string, 0, len(sauces)) for _, sauce := range sauces { versions, err := recipeutil.CheckForUpdates(sauce, opts.OCIRepository) if err != nil { @@ -113,19 +115,28 @@ func runCheck(cmd *cobra.Command, opts checkOptions) error { cmd.Printf("%s: can not check for updates: %s\n", sauce.Recipe.Name, err) } else if len(versions) > 0 { - updatesAvailable = true cmd.Printf("%s: new versions found: %s\n", sauce.Recipe.Name, strings.Join(versions, ", ")) + upgradeCommands = append(upgradeCommands, + fmt.Sprintf("%s upgrade %s:%s", os.Args[0], sauce.CheckFrom, versions[len(versions)-1]), + ) } else { cmd.Printf("%s: no new versions found\n", sauce.Recipe.Name) } } + if len(upgradeCommands) > 0 { + cmd.Println("To upgrade recipes to the latest version run:") + for _, cmdMsg := range upgradeCommands { + cmd.Printf(" %s\n", cmdMsg) + } + } + var exitCode int switch { case errorsFound: exitCode = ExitCodeError - case updatesAvailable && opts.UseDetailedExitCode: + case len(upgradeCommands) > 0 && opts.UseDetailedExitCode: exitCode = ExitCodeUpdatesAvailable default: exitCode = ExitCodeOK diff --git a/test/check-recipes.feature b/test/check-recipes.feature index 1352e3e5..611d6e00 100644 --- a/test/check-recipes.feature +++ b/test/check-recipes.feature @@ -13,6 +13,7 @@ Feature: Check for new recipe versions And I push the recipe "foo" to the local OCI repository And I check new versions for recipe "foo" Then CLI produced an output "new versions found: v0.0.2" + Then CLI produced an output "To upgrade recipes to the latest version run:\n (.*) upgrade oci://localhost:\d+/foo:v0.0.2\n" Scenario: Find multiple newer version for a recipe Given a project directory @@ -30,6 +31,7 @@ Feature: Check for new recipe versions And I push the recipe "foo" to the local OCI repository Then I check new versions for recipe "foo" Then CLI produced an output "new versions found: v0.0.2, v0.0.3" + Then CLI produced an output "To upgrade recipes to the latest version run:\n (.*) upgrade oci://localhost:\d+/foo:v0.0.3\n" Scenario: Find newer version for multiple recipes Given a project directory