From 9f9248b98743d371a9b829bcdd3e76098eade177 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Fri, 22 Oct 2021 08:21:46 +0200 Subject: [PATCH] feat: select prompt for recipes on app new --- cli/app/new.go | 2 +- cli/internal/validate.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/cli/app/new.go b/cli/app/new.go index 2166860a..8ee1b49e 100644 --- a/cli/app/new.go +++ b/cli/app/new.go @@ -165,7 +165,7 @@ func createSecrets(sanitisedAppName string) (secrets, error) { // action is the main command-line action for this package func action(c *cli.Context) error { - recipe := internal.ValidateRecipe(c) + recipe := internal.ValidateRecipeWithPrompt(c) if err := config.EnsureAbraDirExists(); err != nil { logrus.Fatal(err) diff --git a/cli/internal/validate.go b/cli/internal/validate.go index 3819d940..a2c1d2f9 100644 --- a/cli/internal/validate.go +++ b/cli/internal/validate.go @@ -4,8 +4,10 @@ import ( "errors" "coopcloud.tech/abra/pkg/app" + "coopcloud.tech/abra/pkg/catalogue" "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/recipe" + "github.com/AlecAivazis/survey/v2" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -28,6 +30,41 @@ func ValidateRecipe(c *cli.Context) recipe.Recipe { return recipe } +// ValidateRecipeWithPrompt ensures a recipe argument is present before +// validating, asking for input if required. +func ValidateRecipeWithPrompt(c *cli.Context) recipe.Recipe { + recipeName := c.Args().First() + + if recipeName == "" && !NoInput { + catl, err := catalogue.ReadRecipeCatalogue() + if err != nil { + logrus.Fatal(err) + } + var recipes []string + for name := range catl { + recipes = append(recipes, name) + } + prompt := &survey.Select{ + Message: "Select recipe", + Options: recipes, + } + if err := survey.AskOne(prompt, &recipeName); err != nil { + logrus.Fatal(err) + } + } else { + ShowSubcommandHelpAndError(c, errors.New("no recipe provided")) + } + + recipe, err := recipe.Get(recipeName) + if err != nil { + logrus.Fatal(err) + } + + logrus.Debugf("validated '%s' as recipe argument", recipeName) + + return recipe +} + // ValidateApp ensures the app name arg is valid. func ValidateApp(c *cli.Context) config.App { appName := c.Args().First()