package recipe import ( "fmt" "path" "sort" "strconv" "strings" "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/formatter" gitPkg "coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/recipe" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) var pattern string var patternFlag = &cli.StringFlag{ Name: "pattern", Value: "", Aliases: []string{"p"}, Usage: "Simple string to filter recipes", Destination: &pattern, } var recipeListCommand = &cli.Command{ Name: "list", Usage: "List available recipes", Aliases: []string{"ls"}, Flags: []cli.Flag{ patternFlag, }, Action: func(c *cli.Context) error { 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() if err != nil { logrus.Fatal(err.Error()) } recipes := catl.Flatten() sort.Sort(recipe.ByRecipeName(recipes)) tableCol := []string{"name", "category", "status", "healthcheck", "backups", "email", "tests", "SSO"} table := formatter.CreateTable(tableCol) len := 0 for _, recipe := range recipes { tableRow := []string{ recipe.Name, recipe.Category, strconv.Itoa(recipe.Features.Status), recipe.Features.Healthcheck, recipe.Features.Backups, recipe.Features.Email, recipe.Features.Tests, recipe.Features.SSO, } if pattern != "" { if strings.Contains(recipe.Name, pattern) { table.Append(tableRow) len++ } } else { table.Append(tableRow) len++ } } table.SetCaption(true, fmt.Sprintf("total recipes: %v", len)) if table.NumLines() > 0 { table.Render() } return nil }, }