2021-09-05 20:33:07 +00:00
|
|
|
package recipe
|
|
|
|
|
|
|
|
import (
|
2023-09-24 09:18:26 +00:00
|
|
|
"fmt"
|
2023-09-24 08:55:22 +00:00
|
|
|
"sort"
|
|
|
|
|
2021-09-05 20:33:07 +00:00
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-12-25 16:12:22 +00:00
|
|
|
"coopcloud.tech/abra/pkg/autocomplete"
|
2021-12-28 00:24:23 +00:00
|
|
|
"coopcloud.tech/abra/pkg/formatter"
|
2021-12-27 15:40:59 +00:00
|
|
|
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
2023-09-24 09:18:26 +00:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2021-09-05 20:33:07 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-18 13:13:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2021-09-05 20:33:07 +00:00
|
|
|
)
|
|
|
|
|
2023-09-24 08:55:22 +00:00
|
|
|
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
|
|
|
|
}
|
2023-09-24 09:18:26 +00:00
|
|
|
return versions[i][1] < versions[j][1]
|
2023-09-24 08:55:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
var recipeVersionCommand = cli.Command{
|
2023-09-22 07:47:36 +00:00
|
|
|
Name: "versions",
|
|
|
|
Aliases: []string{"v"},
|
|
|
|
Usage: "List recipe versions",
|
|
|
|
ArgsUsage: "<recipe>",
|
|
|
|
Description: "Versions are read from the recipe catalogue.",
|
2022-01-18 13:13:20 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
internal.DebugFlag,
|
2023-07-26 06:16:07 +00:00
|
|
|
internal.OfflineFlag,
|
2023-09-07 16:50:25 +00:00
|
|
|
internal.NoInputFlag,
|
2023-09-24 08:55:22 +00:00
|
|
|
internal.MachineReadableFlag,
|
2022-01-18 13:13:20 +00:00
|
|
|
},
|
|
|
|
Before: internal.SubCommandBefore,
|
2021-12-25 16:12:22 +00:00
|
|
|
BashComplete: autocomplete.RecipeNameComplete,
|
2021-09-05 20:33:07 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2023-09-07 16:50:25 +00:00
|
|
|
recipe := internal.ValidateRecipe(c)
|
2023-08-01 19:19:20 +00:00
|
|
|
|
2023-09-07 16:50:25 +00:00
|
|
|
catl, err := recipePkg.ReadRecipeCatalogue(internal.Offline)
|
2021-09-05 20:33:07 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:50:25 +00:00
|
|
|
recipeMeta, ok := catl[recipe.Name]
|
2021-09-05 22:45:29 +00:00
|
|
|
if !ok {
|
2023-09-24 08:55:22 +00:00
|
|
|
logrus.Fatalf("%s is not published on the catalogue?", recipe.Name)
|
2021-09-05 22:45:29 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 08:55:22 +00:00
|
|
|
if len(recipeMeta.Versions) == 0 {
|
|
|
|
logrus.Fatalf("%s has no catalogue published versions?", recipe.Name)
|
|
|
|
}
|
2021-09-05 22:45:29 +00:00
|
|
|
|
2021-12-25 16:12:34 +00:00
|
|
|
for i := len(recipeMeta.Versions) - 1; i >= 0; i-- {
|
2023-09-24 09:18:26 +00:00
|
|
|
tableCols := []string{"version", "service", "image", "tag"}
|
2023-09-24 08:55:22 +00:00
|
|
|
table := formatter.CreateTable(tableCols)
|
|
|
|
for version, meta := range recipeMeta.Versions[i] {
|
|
|
|
var versions [][]string
|
2021-09-06 14:51:42 +00:00
|
|
|
for service, serviceMeta := range meta {
|
2023-09-24 08:55:22 +00:00
|
|
|
versions = append(versions, []string{version, service, serviceMeta.Image, serviceMeta.Tag})
|
2021-09-06 14:51:42 +00:00
|
|
|
}
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2023-09-24 08:55:22 +00:00
|
|
|
sort.Slice(versions, sortServiceByName(versions))
|
|
|
|
|
|
|
|
for _, version := range versions {
|
|
|
|
table.Append(version)
|
|
|
|
}
|
2021-12-25 16:12:41 +00:00
|
|
|
|
2023-09-24 08:55:22 +00:00
|
|
|
if internal.MachineReadable {
|
|
|
|
table.JSONRender()
|
|
|
|
} else {
|
|
|
|
table.SetAutoMergeCellsByColumnIndex([]int{0})
|
2023-09-24 09:18:26 +00:00
|
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
2023-09-24 08:55:22 +00:00
|
|
|
table.Render()
|
2023-09-24 09:18:26 +00:00
|
|
|
fmt.Println()
|
2023-09-24 08:55:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 16:12:41 +00:00
|
|
|
}
|
2021-09-05 22:45:29 +00:00
|
|
|
|
2021-09-05 20:33:07 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|