abra/cli/recipe/list.go

85 lines
1.8 KiB
Go
Raw Normal View History

2021-09-05 20:33:07 +00:00
package recipe
import (
"fmt"
2022-01-01 21:01:16 +00:00
"path"
2021-09-05 20:33:07 +00:00
"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
2022-01-01 21:01:16 +00:00
"coopcloud.tech/abra/pkg/config"
2021-12-28 00:24:23 +00:00
"coopcloud.tech/abra/pkg/formatter"
2022-01-01 21:01:16 +00:00
gitPkg "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/recipe"
2021-09-05 20:33:07 +00:00
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
2021-12-27 10:00:04 +00:00
var pattern string
var patternFlag = &cli.StringFlag{
Name: "pattern",
Value: "",
Aliases: []string{"p"},
Usage: "Simple string to filter recipes",
Destination: &pattern,
}
2021-09-05 20:33:07 +00:00
var recipeListCommand = &cli.Command{
Name: "list",
Usage: "List available recipes",
Aliases: []string{"ls"},
2021-12-27 10:00:04 +00:00
Flags: []cli.Flag{
patternFlag,
},
2021-09-05 20:33:07 +00:00
Action: func(c *cli.Context) error {
2022-01-01 21:01:16 +00:00
catalogueDir := path.Join(config.ABRA_DIR, "catalogue")
url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, "recipes")
if err := gitPkg.Clone(catalogueDir, url); err != nil {
return err
}
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
},
}