fix: recipe versions lists correctly (also -m)
This commit is contained in:
		| @ -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,26 +45,35 @@ 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}) | ||||
| 				} | ||||
|  | ||||
| 		table.SetAutoMergeCells(true) | ||||
| 				sort.Slice(versions, sortServiceByName(versions)) | ||||
|  | ||||
| 		if table.NumLines() > 0 { | ||||
| 			table.Render() | ||||
| 				for _, version := range versions { | ||||
| 					table.Append(version) | ||||
| 				} | ||||
|  | ||||
| 				if internal.MachineReadable { | ||||
| 					table.JSONRender() | ||||
| 				} else { | ||||
| 			logrus.Fatalf("%s has no published versions?", recipe.Name) | ||||
| 					table.SetAutoMergeCellsByColumnIndex([]int{0}) | ||||
| 					table.Render() | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		return nil | ||||
|  | ||||
| @ -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' | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user