refactor: centralise recipe validation

This commit is contained in:
decentral1se 2021-09-06 00:45:13 +02:00
parent 691a2c7a50
commit a0625bf133
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
8 changed files with 14 additions and 12 deletions

View File

@ -11,8 +11,8 @@ import (
"github.com/urfave/cli/v2"
)
// ValidateRecipeArg ensures the recipe arg is valid.
func ValidateRecipeArg(c *cli.Context) string {
// ValidateRecipe ensures the recipe arg is valid.
func ValidateRecipe(c *cli.Context) string {
recipeName := c.Args().First()
if recipeName == "" {
@ -21,6 +21,7 @@ func ValidateRecipeArg(c *cli.Context) string {
if err := recipe.EnsureExists(recipeName); err != nil {
logrus.Fatal(err)
os.Exit(1)
}
return recipeName

View File

@ -1,7 +1,6 @@
package recipe
import (
"errors"
"fmt"
"os"
"path"
@ -20,10 +19,7 @@ var recipeCreateCommand = &cli.Command{
Aliases: []string{"c"},
ArgsUsage: "<recipe>",
Action: func(c *cli.Context) error {
recipe := c.Args().First()
if recipe == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no recipe provided"))
}
recipe := internal.ValidateRecipe(c)
directory := path.Join(config.APPS_DIR, recipe)
if _, err := os.Stat(directory); !os.IsNotExist(err) {

View File

@ -23,7 +23,7 @@ var recipeLintCommand = &cli.Command{
Aliases: []string{"l"},
ArgsUsage: "<recipe>",
Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipeArg(c)
recipe := internal.ValidateRecipe(c)
pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, recipe)
composeFiles, err := filepath.Glob(pattern)

View File

@ -19,16 +19,21 @@ var recipeListCommand = &cli.Command{
if err != nil {
logrus.Fatal(err.Error())
}
recipes := catl.Flatten()
sort.Sort(catalogue.ByRecipeName(recipes))
tableCol := []string{"Name", "Category", "Status"}
table := formatter.CreateTable(tableCol)
for _, recipe := range recipes {
status := fmt.Sprintf("%v", recipe.Features.Status)
tableRow := []string{recipe.Name, recipe.Category, status}
table.Append(tableRow)
}
table.Render()
return nil
},
}

View File

@ -28,7 +28,7 @@ the versioning metadata of up-and-running containers are.
`,
ArgsUsage: "<recipe>",
Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipeArg(c)
recipe := internal.ValidateRecipe(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {

View File

@ -35,7 +35,7 @@ This is step 1 of upgrading a recipe. Step 2 is running "abra recipe sync
`,
ArgsUsage: "<recipe>",
Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipeArg(c)
recipe := internal.ValidateRecipe(c)
appFiles, err := config.LoadAppFiles("")
if err != nil {

View File

@ -14,7 +14,7 @@ var recipeVersionCommand = &cli.Command{
Aliases: []string{"v"},
ArgsUsage: "<recipe>",
Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipeArg(c)
recipe := internal.ValidateRecipe(c)
catalogue, err := catalogue.ReadRecipeCatalogue()
if err != nil {

View File

@ -157,7 +157,7 @@ func LoadAppFiles(servers ...string) (AppFiles, error) {
func GetApp(apps AppFiles, name AppName) (App, error) {
appFile, exists := apps[name]
if !exists {
return App{}, fmt.Errorf("cannot find app file with name '%s'", name)
return App{}, fmt.Errorf("cannot find app with name '%s'", name)
}
app, err := readAppEnvFile(appFile, name)
if err != nil {