abra/cli/recipe/list.go

79 lines
1.6 KiB
Go
Raw Normal View History

2021-09-05 20:33:07 +00:00
package recipe
import (
"fmt"
"sort"
2021-12-25 16:17:41 +00:00
"strconv"
2021-12-27 10:00:04 +00:00
"strings"
2021-09-05 20:33:07 +00:00
"coopcloud.tech/abra/cli/internal"
2021-12-28 00:24:23 +00:00
"coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/recipe"
2021-09-05 20:33:07 +00:00
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
2021-09-05 20:33:07 +00:00
)
2021-12-27 10:00:04 +00:00
var pattern string
var patternFlag = &cli.StringFlag{
Name: "pattern, p",
2021-12-27 10:00:04 +00:00
Value: "",
Usage: "Simple string to filter recipes",
Destination: &pattern,
}
var recipeListCommand = cli.Command{
2021-09-05 20:33:07 +00:00
Name: "list",
Usage: "List available recipes",
Aliases: []string{"ls"},
2021-12-27 10:00:04 +00:00
Flags: []cli.Flag{
internal.DebugFlag,
internal.NoInputFlag,
2021-12-27 10:00:04 +00:00
patternFlag,
},
Before: internal.SubCommandBefore,
2021-09-05 20:33:07 +00:00
Action: func(c *cli.Context) error {
catl, err := recipe.ReadRecipeCatalogue()
2021-09-05 20:33:07 +00:00
if err != nil {
logrus.Fatal(err.Error())
}
2021-09-05 22:45:13 +00:00
2021-09-05 20:33:07 +00:00
recipes := catl.Flatten()
sort.Sort(recipe.ByRecipeName(recipes))
2021-09-05 22:45:13 +00:00
2021-12-27 10:00:04 +00:00
tableCol := []string{"name", "category", "status", "healthcheck", "backups", "email", "tests", "SSO"}
2021-09-05 20:33:07 +00:00
table := formatter.CreateTable(tableCol)
2021-09-05 22:45:13 +00:00
2022-01-01 20:46:38 +00:00
len := 0
2021-09-05 20:33:07 +00:00
for _, recipe := range recipes {
2021-12-25 16:17:41 +00:00
tableRow := []string{
recipe.Name,
recipe.Category,
strconv.Itoa(recipe.Features.Status),
recipe.Features.Healthcheck,
recipe.Features.Backups,
recipe.Features.Email,
2021-12-27 10:00:04 +00:00
recipe.Features.Tests,
2021-12-25 16:17:41 +00:00
recipe.Features.SSO,
}
2021-12-27 10:00:04 +00:00
if pattern != "" {
if strings.Contains(recipe.Name, pattern) {
table.Append(tableRow)
2022-01-01 20:46:38 +00:00
len++
2021-12-27 10:00:04 +00:00
}
} else {
table.Append(tableRow)
2022-01-01 20:46:38 +00:00
len++
2021-12-27 10:00:04 +00:00
}
2021-09-05 20:33:07 +00:00
}
2021-09-05 22:45:13 +00:00
2022-01-01 20:46:38 +00:00
table.SetCaption(true, fmt.Sprintf("total recipes: %v", len))
2021-12-27 10:00:04 +00:00
if table.NumLines() > 0 {
table.Render()
}
2021-09-05 22:45:13 +00:00
2021-09-05 20:33:07 +00:00
return nil
},
}