feat: select prompt for recipes on app new

This commit is contained in:
decentral1se 2021-10-22 08:21:46 +02:00
parent 2bb4a9c063
commit 9f9248b987
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 38 additions and 1 deletions

View File

@ -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)

View File

@ -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()