From 42f9e6d45879bc9a5f16e2688aed345aedca305e Mon Sep 17 00:00:00 2001 From: Ammar Hussein Date: Fri, 19 Dec 2025 06:50:35 -0800 Subject: [PATCH] return error instead of handling it inside getLatestVersion --- cli/recipe/sync.go | 18 ++++++++++++------ cli/recipe/sync_test.go | 8 ++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cli/recipe/sync.go b/cli/recipe/sync.go index 5e303493a..226b154e9 100644 --- a/cli/recipe/sync.go +++ b/cli/recipe/sync.go @@ -1,6 +1,7 @@ package recipe import ( + "errors" "fmt" "strconv" "strings" @@ -19,6 +20,9 @@ import ( "github.com/spf13/cobra" ) +// Errors +var emptyVersionsInCatalogue = errors.New("Catalogue versions list is empty (unexpectedly!)") + // translators: `abra recipe reset` aliases. use a comma separated list of // aliases with no spaces in between var recipeSyncAliases = i18n.G("s") @@ -127,7 +131,10 @@ likely to change. } latestRelease := tags[len(tags)-1] - latestRecipeVersion := getLatestVersion(recipe, catl) + latestRecipeVersion, err := getLatestVersion(recipe, catl) + if err != nil && err != emptyVersionsInCatalogue { + log.Fatal(err) + } changesTable.Headers(i18n.G("SERVICE"), latestRelease, i18n.G("PROPOSED CHANGES")) allRecipeVersions := catl[recipe.Name].Versions @@ -294,14 +301,13 @@ func init() { ) } -func getLatestVersion(recipe recipePkg.Recipe, catl recipePkg.RecipeCatalogue) string { +func getLatestVersion(recipe recipePkg.Recipe, catl recipePkg.RecipeCatalogue) (string, error) { versions, err := recipePkg.GetRecipeCatalogueVersions(recipe.Name, catl) if err != nil { - log.Fatal(err) + return "", err } - latestRecipeVersion := "0.0.0" if len(versions) > 0 { - latestRecipeVersion = versions[len(versions)-1] + return versions[len(versions)-1], nil } - return latestRecipeVersion + return "", emptyVersionsInCatalogue } diff --git a/cli/recipe/sync_test.go b/cli/recipe/sync_test.go index ffe9965b0..2e5df3578 100644 --- a/cli/recipe/sync_test.go +++ b/cli/recipe/sync_test.go @@ -7,11 +7,11 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGetLatestVersionReturnsDefault(t *testing.T) { +func TestGetLatestVersionReturnsErrorWhenVersionsIsEmpty(t *testing.T) { recipe := recipePkg.Recipe{} catalogue := recipePkg.RecipeCatalogue{} - version := getLatestVersion(recipe, catalogue) - assert.Equal(t, version, "0.0.0") + _, err := getLatestVersion(recipe, catalogue) + assert.Equal(t, err, emptyVersionsInCatalogue) } func TestGetLatestVersionReturnsLastVersion(t *testing.T) { @@ -28,6 +28,6 @@ func TestGetLatestVersionReturnsLastVersion(t *testing.T) { catalogue["test"] = recipePkg.RecipeMeta{ Versions: versions, } - version := getLatestVersion(recipe, catalogue) + version, _ := getLatestVersion(recipe, catalogue) assert.Equal(t, version, "0.0.3") }