refactor: centralise recipe validation
This commit is contained in:
		| @ -11,8 +11,8 @@ import ( | |||||||
| 	"github.com/urfave/cli/v2" | 	"github.com/urfave/cli/v2" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // ValidateRecipeArg ensures the recipe arg is valid. | // ValidateRecipe ensures the recipe arg is valid. | ||||||
| func ValidateRecipeArg(c *cli.Context) string { | func ValidateRecipe(c *cli.Context) string { | ||||||
| 	recipeName := c.Args().First() | 	recipeName := c.Args().First() | ||||||
|  |  | ||||||
| 	if recipeName == "" { | 	if recipeName == "" { | ||||||
| @ -21,6 +21,7 @@ func ValidateRecipeArg(c *cli.Context) string { | |||||||
|  |  | ||||||
| 	if err := recipe.EnsureExists(recipeName); err != nil { | 	if err := recipe.EnsureExists(recipeName); err != nil { | ||||||
| 		logrus.Fatal(err) | 		logrus.Fatal(err) | ||||||
|  | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return recipeName | 	return recipeName | ||||||
|  | |||||||
| @ -1,7 +1,6 @@ | |||||||
| package recipe | package recipe | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path" | 	"path" | ||||||
| @ -20,10 +19,7 @@ var recipeCreateCommand = &cli.Command{ | |||||||
| 	Aliases:   []string{"c"}, | 	Aliases:   []string{"c"}, | ||||||
| 	ArgsUsage: "<recipe>", | 	ArgsUsage: "<recipe>", | ||||||
| 	Action: func(c *cli.Context) error { | 	Action: func(c *cli.Context) error { | ||||||
| 		recipe := c.Args().First() | 		recipe := internal.ValidateRecipe(c) | ||||||
| 		if recipe == "" { |  | ||||||
| 			internal.ShowSubcommandHelpAndError(c, errors.New("no recipe provided")) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		directory := path.Join(config.APPS_DIR, recipe) | 		directory := path.Join(config.APPS_DIR, recipe) | ||||||
| 		if _, err := os.Stat(directory); !os.IsNotExist(err) { | 		if _, err := os.Stat(directory); !os.IsNotExist(err) { | ||||||
|  | |||||||
| @ -23,7 +23,7 @@ var recipeLintCommand = &cli.Command{ | |||||||
| 	Aliases:   []string{"l"}, | 	Aliases:   []string{"l"}, | ||||||
| 	ArgsUsage: "<recipe>", | 	ArgsUsage: "<recipe>", | ||||||
| 	Action: func(c *cli.Context) error { | 	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) | 		pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, recipe) | ||||||
| 		composeFiles, err := filepath.Glob(pattern) | 		composeFiles, err := filepath.Glob(pattern) | ||||||
|  | |||||||
| @ -19,16 +19,21 @@ var recipeListCommand = &cli.Command{ | |||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			logrus.Fatal(err.Error()) | 			logrus.Fatal(err.Error()) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		recipes := catl.Flatten() | 		recipes := catl.Flatten() | ||||||
| 		sort.Sort(catalogue.ByRecipeName(recipes)) | 		sort.Sort(catalogue.ByRecipeName(recipes)) | ||||||
|  |  | ||||||
| 		tableCol := []string{"Name", "Category", "Status"} | 		tableCol := []string{"Name", "Category", "Status"} | ||||||
| 		table := formatter.CreateTable(tableCol) | 		table := formatter.CreateTable(tableCol) | ||||||
|  |  | ||||||
| 		for _, recipe := range recipes { | 		for _, recipe := range recipes { | ||||||
| 			status := fmt.Sprintf("%v", recipe.Features.Status) | 			status := fmt.Sprintf("%v", recipe.Features.Status) | ||||||
| 			tableRow := []string{recipe.Name, recipe.Category, status} | 			tableRow := []string{recipe.Name, recipe.Category, status} | ||||||
| 			table.Append(tableRow) | 			table.Append(tableRow) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		table.Render() | 		table.Render() | ||||||
|  |  | ||||||
| 		return nil | 		return nil | ||||||
| 	}, | 	}, | ||||||
| } | } | ||||||
|  | |||||||
| @ -28,7 +28,7 @@ the versioning metadata of up-and-running containers are. | |||||||
| `, | `, | ||||||
| 	ArgsUsage: "<recipe>", | 	ArgsUsage: "<recipe>", | ||||||
| 	Action: func(c *cli.Context) error { | 	Action: func(c *cli.Context) error { | ||||||
| 		recipe := internal.ValidateRecipeArg(c) | 		recipe := internal.ValidateRecipe(c) | ||||||
|  |  | ||||||
| 		appFiles, err := config.LoadAppFiles("") | 		appFiles, err := config.LoadAppFiles("") | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
|  | |||||||
| @ -35,7 +35,7 @@ This is step 1 of upgrading a recipe. Step 2 is running "abra recipe sync | |||||||
| `, | `, | ||||||
| 	ArgsUsage: "<recipe>", | 	ArgsUsage: "<recipe>", | ||||||
| 	Action: func(c *cli.Context) error { | 	Action: func(c *cli.Context) error { | ||||||
| 		recipe := internal.ValidateRecipeArg(c) | 		recipe := internal.ValidateRecipe(c) | ||||||
|  |  | ||||||
| 		appFiles, err := config.LoadAppFiles("") | 		appFiles, err := config.LoadAppFiles("") | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
|  | |||||||
| @ -14,7 +14,7 @@ var recipeVersionCommand = &cli.Command{ | |||||||
| 	Aliases:   []string{"v"}, | 	Aliases:   []string{"v"}, | ||||||
| 	ArgsUsage: "<recipe>", | 	ArgsUsage: "<recipe>", | ||||||
| 	Action: func(c *cli.Context) error { | 	Action: func(c *cli.Context) error { | ||||||
| 		recipe := internal.ValidateRecipeArg(c) | 		recipe := internal.ValidateRecipe(c) | ||||||
|  |  | ||||||
| 		catalogue, err := catalogue.ReadRecipeCatalogue() | 		catalogue, err := catalogue.ReadRecipeCatalogue() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
|  | |||||||
| @ -157,7 +157,7 @@ func LoadAppFiles(servers ...string) (AppFiles, error) { | |||||||
| func GetApp(apps AppFiles, name AppName) (App, error) { | func GetApp(apps AppFiles, name AppName) (App, error) { | ||||||
| 	appFile, exists := apps[name] | 	appFile, exists := apps[name] | ||||||
| 	if !exists { | 	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) | 	app, err := readAppEnvFile(appFile, name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user