Allow `abra app new <git-url>` to use a recipe from outside the catalogue. On clone, a `.abra-source` sidecar records the canonical host/path name (the on-disk directory escapes "/" and "." lossily), and IsClean ignores it. When templating the app's .env, a `RECIPE=<canonical name>` line is injected so a later `abra app deploy`, possibly on another machine, re-fetches the recipe from the same git source. `recipe ls` now shows a source column listing these external recipes alongside catalogue ones. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
162 lines
3.4 KiB
Go
162 lines
3.4 KiB
Go
package recipe
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
"coopcloud.tech/abra/pkg/config"
|
|
"coopcloud.tech/abra/pkg/formatter"
|
|
"coopcloud.tech/abra/pkg/i18n"
|
|
"coopcloud.tech/abra/pkg/log"
|
|
"coopcloud.tech/abra/pkg/recipe"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// translators: `abra recipe list` aliases. use a comma separated list of
|
|
// aliases with no spaces in between
|
|
var recipeListAliases = i18n.G("ls")
|
|
|
|
var RecipeListCommand = &cobra.Command{
|
|
// translators: `recipe list` command
|
|
Use: i18n.G("list"),
|
|
// translators: Short description for `recipe list` command
|
|
Short: i18n.G("List recipes"),
|
|
Aliases: strings.Split(recipeListAliases, ","),
|
|
Args: cobra.NoArgs,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
catl, err := recipe.ReadRecipeCatalogue(internal.Offline)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
recipes := catl.Flatten()
|
|
sort.Sort(recipe.ByRecipeName(recipes))
|
|
|
|
table, err := formatter.CreateTable()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
headers := []string{
|
|
i18n.G("name"),
|
|
i18n.G("source"),
|
|
i18n.G("category"),
|
|
i18n.G("status"),
|
|
i18n.G("healthcheck"),
|
|
i18n.G("backups"),
|
|
i18n.G("email"),
|
|
i18n.G("tests"),
|
|
i18n.G("SSO"),
|
|
}
|
|
|
|
table.Headers(headers...)
|
|
|
|
var rows [][]string
|
|
for _, recipe := range recipes {
|
|
row := []string{
|
|
recipe.Name,
|
|
i18n.G("catalogue"),
|
|
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.Row(row...)
|
|
rows = append(rows, row)
|
|
}
|
|
} else {
|
|
table.Row(row...)
|
|
rows = append(rows, row)
|
|
}
|
|
}
|
|
|
|
externals := externalRecipes(catl)
|
|
sort.Strings(externals)
|
|
for _, name := range externals {
|
|
row := []string{
|
|
name,
|
|
i18n.G("external"),
|
|
"-", "-", "-", "-", "-", "-", "-",
|
|
}
|
|
if pattern != "" {
|
|
if !strings.Contains(name, pattern) {
|
|
continue
|
|
}
|
|
}
|
|
table.Row(row...)
|
|
rows = append(rows, row)
|
|
}
|
|
|
|
if len(rows) > 0 {
|
|
if internal.MachineReadable {
|
|
out, err := formatter.ToJSON(headers, rows)
|
|
if err != nil {
|
|
log.Fatal(i18n.G("unable to render to JSON: %s", err))
|
|
}
|
|
fmt.Println(out)
|
|
return
|
|
}
|
|
|
|
if err := formatter.PrintTable(table); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
// externalRecipes returns canonical names of locally-cloned recipes that
|
|
// were sourced from an arbitrary git URL (i.e. they carry a .abra-source
|
|
// sidecar) and are not already listed in the catalogue.
|
|
func externalRecipes(catl recipe.RecipeCatalogue) []string {
|
|
dirs, err := recipe.GetRecipesLocal()
|
|
if err != nil {
|
|
log.Debug(i18n.G("can't read local recipes: %s", err))
|
|
return nil
|
|
}
|
|
|
|
var names []string
|
|
for _, dir := range dirs {
|
|
canonical := recipe.ReadRecipeSource(path.Join(config.RECIPES_DIR, dir))
|
|
if canonical == "" {
|
|
continue
|
|
}
|
|
if _, inCatalogue := catl[canonical]; inCatalogue {
|
|
continue
|
|
}
|
|
names = append(names, canonical)
|
|
}
|
|
return names
|
|
}
|
|
|
|
var (
|
|
pattern string
|
|
)
|
|
|
|
func init() {
|
|
RecipeListCommand.Flags().BoolVarP(
|
|
&internal.MachineReadable,
|
|
i18n.G("machine"),
|
|
i18n.G("m"),
|
|
false,
|
|
i18n.G("print machine-readable output"),
|
|
)
|
|
|
|
RecipeListCommand.Flags().StringVarP(
|
|
&pattern,
|
|
i18n.G("pattern"),
|
|
i18n.G("p"),
|
|
"",
|
|
i18n.G("filter by recipe"),
|
|
)
|
|
}
|