refactor(recipe): introduce new recipe struct and move some methods

This commit is contained in:
2024-07-07 12:35:09 +02:00
parent 9ef64778f5
commit 950f85e2b4
16 changed files with 197 additions and 167 deletions

View File

@ -47,22 +47,23 @@ var appBackupListCommand = cli.Command{
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
if err := recipe.EnsureExists(app.Recipe); err != nil {
r := recipe.Get2(app.Recipe)
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -109,23 +110,24 @@ var appBackupDownloadCommand = cli.Command{
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -179,23 +181,24 @@ var appBackupCreateCommand = cli.Command{
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -237,23 +240,24 @@ var appBackupSnapshotsCommand = cli.Command{
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}

View File

@ -7,7 +7,6 @@ import (
"coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
recipePkg "coopcloud.tech/abra/pkg/recipe"
"github.com/urfave/cli"
)
@ -37,23 +36,24 @@ ${FOO:<default>} syntax). "check" does not confirm or deny this for you.`,
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipePkg.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipePkg.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipePkg.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}

View File

@ -17,7 +17,6 @@ import (
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
recipePkg "coopcloud.tech/abra/pkg/recipe"
"github.com/urfave/cli"
)
@ -60,23 +59,24 @@ Example:
},
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Name)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipePkg.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipePkg.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipePkg.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -228,23 +228,24 @@ var appCmdListCommand = cli.Command{
Before: internal.SubCommandBefore,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Name)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipePkg.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipePkg.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipePkg.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}

View File

@ -52,28 +52,29 @@ recipes.
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
stackName := app.StackName()
r2 := recipe.Get2(app.Recipe)
specificVersion := c.Args().Get(1)
if specificVersion != "" && internal.Chaos {
log.Fatal("cannot use <version> and --chaos together")
}
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r2.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r2.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r2.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r2.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -107,7 +108,7 @@ recipes.
if specificVersion != "" {
version = specificVersion
log.Debugf("choosing %s as version to deploy", version)
if err := recipe.EnsureVersion(app.Recipe, version); err != nil {
if err := r2.EnsureVersion(version); err != nil {
log.Fatal(err)
}
}
@ -157,7 +158,7 @@ recipes.
if len(versions) > 0 && !internal.Chaos {
version = versions[len(versions)-1]
log.Debugf("choosing %s as version to deploy", version)
if err := recipe.EnsureVersion(app.Recipe, version); err != nil {
if err := r2.EnsureVersion(version); err != nil {
log.Fatal(err)
}
} else {
@ -173,7 +174,7 @@ recipes.
if internal.Chaos {
log.Warnf("chaos mode engaged")
var err error
version, err = recipe.ChaosVersion(app.Recipe)
version, err = r2.ChaosVersion()
if err != nil {
log.Fatal(err)
}

View File

@ -38,8 +38,9 @@ var appLogsCommand = cli.Command{
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
stackName := app.StackName()
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}

View File

@ -67,13 +67,14 @@ var appNewCommand = cli.Command{
},
Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipe(c)
r := recipePkg.Get2(recipe.Name)
if !internal.Chaos {
if err := recipePkg.EnsureIsClean(recipe.Name); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipePkg.EnsureUpToDate(recipe.Name); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
@ -93,16 +94,16 @@ var appNewCommand = cli.Command{
version = tag
}
if err := recipePkg.EnsureVersion(recipe.Name, version); err != nil {
if err := r.EnsureVersion(version); err != nil {
log.Fatal(err)
}
} else {
if err := recipePkg.EnsureLatest(recipe.Name); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
} else {
if err := recipePkg.EnsureVersion(recipe.Name, c.Args().Get(1)); err != nil {
if err := r.EnsureVersion(c.Args().Get(1)); err != nil {
log.Fatal(err)
}
}

View File

@ -35,6 +35,7 @@ var appPsCommand = cli.Command{
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
cl, err := client.New(app.Server)
if err != nil {
@ -53,7 +54,7 @@ var appPsCommand = cli.Command{
statuses, err := appPkg.GetAppStatuses([]appPkg.App{app}, true)
if statusMeta, ok := statuses[app.StackName()]; ok {
if _, exists := statusMeta["chaos"]; !exists {
if err := recipe.EnsureVersion(app.Recipe, deployedVersion); err != nil {
if err := r.EnsureVersion(deployedVersion); err != nil {
log.Fatal(err)
}
}

View File

@ -32,23 +32,24 @@ var appRestoreCommand = cli.Command{
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}

View File

@ -52,28 +52,29 @@ recipes.
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
stackName := app.StackName()
r2 := recipe.Get2(app.Recipe)
specificVersion := c.Args().Get(1)
if specificVersion != "" && internal.Chaos {
log.Fatal("cannot use <version> and --chaos together")
}
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r2.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r2.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r2.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r2.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -185,7 +186,7 @@ recipes.
}
if !internal.Chaos {
if err := recipe.EnsureVersion(app.Recipe, chosenDowngrade); err != nil {
if err := r2.EnsureVersion(chosenDowngrade); err != nil {
log.Fatal(err)
}
}
@ -193,7 +194,7 @@ recipes.
if internal.Chaos {
log.Warn("chaos mode engaged")
var err error
chosenDowngrade, err = recipe.ChaosVersion(app.Recipe)
chosenDowngrade, err = r2.ChaosVersion()
if err != nil {
log.Fatal(err)
}

View File

@ -56,23 +56,24 @@ var appSecretGenerateCommand = cli.Command{
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -263,23 +264,24 @@ Example:
`,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -371,23 +373,24 @@ var appSecretLsCommand = cli.Command{
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
r := recipe.Get2(app.Recipe)
if err := recipe.EnsureExists(app.Recipe); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}

View File

@ -58,6 +58,7 @@ recipes.
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
stackName := app.StackName()
r := recipe.Get2(app.Name)
specificVersion := c.Args().Get(1)
if specificVersion != "" && internal.Chaos {
@ -65,17 +66,17 @@ recipes.
}
if !internal.Chaos {
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipe.EnsureLatest(app.Recipe); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}
@ -219,7 +220,7 @@ recipes.
}
if !internal.Chaos {
if err := recipePkg.EnsureVersion(app.Recipe, chosenUpgrade); err != nil {
if err := r.EnsureVersion(chosenUpgrade); err != nil {
log.Fatal(err)
}
}
@ -227,7 +228,7 @@ recipes.
if internal.Chaos {
log.Warn("chaos mode engaged")
var err error
chosenUpgrade, err = recipePkg.ChaosVersion(app.Recipe)
chosenUpgrade, err = r.ChaosVersion()
if err != nil {
log.Fatal(err)
}

View File

@ -24,9 +24,10 @@ var recipeFetchCommand = cli.Command{
BashComplete: autocomplete.RecipeNameComplete,
Action: func(c *cli.Context) error {
recipeName := c.Args().First()
r := recipe.Get2(recipeName)
if recipeName != "" {
internal.ValidateRecipe(c)
if err := recipe.Ensure(recipeName); err != nil {
if err := r.Ensure(); err != nil {
log.Fatal(err)
}
return nil
@ -39,7 +40,8 @@ var recipeFetchCommand = cli.Command{
catlBar := formatter.CreateProgressbar(len(catalogue), "fetching latest recipes...")
for recipeName := range catalogue {
if err := recipe.Ensure(recipeName); err != nil {
r := recipe.Get2(recipeName)
if err := r.Ensure(); err != nil {
log.Error(err)
}
catlBar.Add(1)

View File

@ -28,23 +28,24 @@ var recipeLintCommand = cli.Command{
BashComplete: autocomplete.RecipeNameComplete,
Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipe(c)
r := recipePkg.Get2(recipe.Name)
if err := recipePkg.EnsureExists(recipe.Name); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if !internal.Chaos {
if err := recipePkg.EnsureIsClean(recipe.Name); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if !internal.Offline {
if err := recipePkg.EnsureUpToDate(recipe.Name); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
}
if err := recipePkg.EnsureLatest(recipe.Name); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}
}

View File

@ -72,20 +72,21 @@ You may invoke this command in "wizard" mode and be prompted for input:
BashComplete: autocomplete.RecipeNameComplete,
Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipe(c)
r := recipePkg.Get2(recipe.Name)
if err := recipePkg.EnsureIsClean(recipe.Name); err != nil {
if err := r.EnsureIsClean(); err != nil {
log.Fatal(err)
}
if err := recipePkg.EnsureExists(recipe.Name); err != nil {
if err := r.EnsureExists(); err != nil {
log.Fatal(err)
}
if err := recipePkg.EnsureUpToDate(recipe.Name); err != nil {
if err := r.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
if err := recipePkg.EnsureLatest(recipe.Name); err != nil {
if err := r.EnsureLatest(); err != nil {
log.Fatal(err)
}

View File

@ -318,20 +318,20 @@ func getAvailableUpgrades(cl *dockerclient.Client, stackName string, recipeName
// processRecipeRepoVersion clones, pulls, checks out the version and lints the
// recipe repository.
func processRecipeRepoVersion(recipeName, version string) error {
if err := recipe.EnsureExists(recipeName); err != nil {
func processRecipeRepoVersion(r recipe.Recipe2, version string) error {
if err := r.EnsureExists(); err != nil {
return err
}
if err := recipe.EnsureUpToDate(recipeName); err != nil {
if err := r.EnsureUpToDate(); err != nil {
return err
}
if err := recipe.EnsureVersion(recipeName, version); err != nil {
if err := r.EnsureVersion(version); err != nil {
return err
}
if r, err := recipe.Get(recipeName, internal.Offline); err != nil {
if r, err := recipe.Get(r.Name, internal.Offline); err != nil {
return err
} else if err := lint.LintForErrors(r); err != nil {
return err
@ -445,7 +445,9 @@ func upgrade(cl *dockerclient.Client, stackName, recipeName,
Env: env,
}
if err = processRecipeRepoVersion(recipeName, upgradeVersion); err != nil {
r := recipe.Get2(recipeName)
if err = processRecipeRepoVersion(r, upgradeVersion); err != nil {
return err
}