diff --git a/cli/recipe/version.go b/cli/recipe/version.go index baa1c4f7..c29859e0 100644 --- a/cli/recipe/version.go +++ b/cli/recipe/version.go @@ -1,6 +1,8 @@ package recipe import ( + "sort" + "coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/formatter" @@ -9,6 +11,16 @@ import ( "github.com/urfave/cli" ) +func sortServiceByName(versions [][]string) func(i, j int) bool { + return func(i, j int) bool { + // NOTE(d1): corresponds to the `tableCol` definition below + if versions[i][1] == "app" { + return true + } + return false + } +} + var recipeVersionCommand = cli.Command{ Name: "versions", Aliases: []string{"v"}, @@ -19,6 +31,7 @@ var recipeVersionCommand = cli.Command{ internal.DebugFlag, internal.OfflineFlag, internal.NoInputFlag, + internal.MachineReadableFlag, }, Before: internal.SubCommandBefore, BashComplete: autocomplete.RecipeNameComplete, @@ -32,28 +45,37 @@ var recipeVersionCommand = cli.Command{ recipeMeta, ok := catl[recipe.Name] if !ok { - logrus.Fatalf("%s recipe doesn't exist?", recipe.Name) + logrus.Fatalf("%s is not published on the catalogue?", recipe.Name) } - tableCol := []string{"Version", "Service", "Image", "Tag"} - table := formatter.CreateTable(tableCol) + if len(recipeMeta.Versions) == 0 { + logrus.Fatalf("%s has no catalogue published versions?", recipe.Name) + } for i := len(recipeMeta.Versions) - 1; i >= 0; i-- { - for tag, meta := range recipeMeta.Versions[i] { + tableCols := []string{"Version", "Service", "Image", "Tag"} + table := formatter.CreateTable(tableCols) + for version, meta := range recipeMeta.Versions[i] { + var versions [][]string for service, serviceMeta := range meta { - table.Append([]string{tag, service, serviceMeta.Image, serviceMeta.Tag}) + versions = append(versions, []string{version, service, serviceMeta.Image, serviceMeta.Tag}) + } + + sort.Slice(versions, sortServiceByName(versions)) + + for _, version := range versions { + table.Append(version) + } + + if internal.MachineReadable { + table.JSONRender() + } else { + table.SetAutoMergeCellsByColumnIndex([]int{0}) + table.Render() } } } - table.SetAutoMergeCells(true) - - if table.NumLines() > 0 { - table.Render() - } else { - logrus.Fatalf("%s has no published versions?", recipe.Name) - } - return nil }, } diff --git a/tests/integration/recipe_version.bats b/tests/integration/recipe_version.bats index 5d5bfca7..5efc9f5d 100644 --- a/tests/integration/recipe_version.bats +++ b/tests/integration/recipe_version.bats @@ -10,3 +10,28 @@ setup() { assert_success assert_output --partial '2.3.2+1.20.3-rootless' } + +@test "error if not present in catalogue" { + run $ABRA recipe versions "$TEST_RECIPE" + assert_failure + assert_output --partial "is not published on the catalogue" +} + +@test "versions listed in correct order" { + latestVersion=$(jq -r '.gitea.versions[-1] | keys[0]' < "$ABRA_DIR/catalogue/recipes.json") + refute [ -z "$latestVersion" ]; + + run bash -c '$ABRA recipe versions gitea --machine | jq -r ".[0].Version" | head -n 1' + assert_success + assert_output "$latestVersion" +} + +@test "app is first service listed" { + run bash -c '$ABRA recipe versions gitea --machine | jq -r ".[0].Service" | uniq' + assert_success + assert_output 'app' + + run bash -c '$ABRA recipe versions gitea --machine | jq -r ".[1].Service" | uniq' + assert_success + assert_output 'db' +}