fix: recipe versions lists correctly (also -m)

This commit is contained in:
decentral1se 2023-09-24 10:55:22 +02:00
parent 78b8cf9725
commit 533edbf172
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
2 changed files with 60 additions and 13 deletions

View File

@ -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
},
}

View File

@ -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'
}