refactor: centralise recipe validation
This commit is contained in:
		| @ -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 | ||||
|  | ||||
| @ -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) { | ||||
|  | ||||
| @ -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) | ||||
|  | ||||
| @ -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 | ||||
| 	}, | ||||
| } | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
		Reference in New Issue
	
	Block a user