refactor: construct recipe struct proper
continuous-integration/drone/push Build is failing Details

This commit is contained in:
decentral1se 2021-09-06 01:41:16 +02:00
parent 6a1ecd0f85
commit 356c8f8c4e
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
6 changed files with 26 additions and 33 deletions

View File

@ -12,19 +12,20 @@ import (
) )
// ValidateRecipe ensures the recipe arg is valid. // ValidateRecipe ensures the recipe arg is valid.
func ValidateRecipe(c *cli.Context) string { func ValidateRecipe(c *cli.Context) recipe.Recipe {
recipeName := c.Args().First() recipeName := c.Args().First()
if recipeName == "" { if recipeName == "" {
ShowSubcommandHelpAndError(c, errors.New("no recipe provided")) ShowSubcommandHelpAndError(c, errors.New("no recipe provided"))
} }
if err := recipe.EnsureExists(recipeName); err != nil { recipe, err := recipe.Get(recipeName)
if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
os.Exit(1) os.Exit(1)
} }
return recipeName return recipe
} }
// ValidateApp ensures the app name arg is valid. // ValidateApp ensures the app name arg is valid.

View File

@ -19,9 +19,9 @@ 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 {
recipeName := internal.ValidateRecipe(c) recipe := internal.ValidateRecipe(c)
directory := path.Join(config.APPS_DIR, recipeName) directory := path.Join(config.APPS_DIR, recipe.Name)
if _, err := os.Stat(directory); !os.IsNotExist(err) { if _, err := os.Stat(directory); !os.IsNotExist(err) {
logrus.Fatalf("'%s' recipe directory already exists?", directory) logrus.Fatalf("'%s' recipe directory already exists?", directory)
return nil return nil
@ -34,16 +34,16 @@ var recipeCreateCommand = &cli.Command{
return nil return nil
} }
gitRepo := path.Join(config.APPS_DIR, recipeName, ".git") gitRepo := path.Join(config.APPS_DIR, recipe.Name, ".git")
if err := os.RemoveAll(gitRepo); err != nil { if err := os.RemoveAll(gitRepo); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
return nil return nil
} }
toParse := []string{ toParse := []string{
path.Join(config.APPS_DIR, recipeName, "README.md"), path.Join(config.APPS_DIR, recipe.Name, "README.md"),
path.Join(config.APPS_DIR, recipeName, ".env.sample"), path.Join(config.APPS_DIR, recipe.Name, ".env.sample"),
path.Join(config.APPS_DIR, recipeName, ".drone.yml"), path.Join(config.APPS_DIR, recipe.Name, ".drone.yml"),
} }
for _, path := range toParse { for _, path := range toParse {
file, err := os.OpenFile(path, os.O_RDWR, 0755) file, err := os.OpenFile(path, os.O_RDWR, 0755)
@ -64,7 +64,7 @@ var recipeCreateCommand = &cli.Command{
if err := tpl.Execute(file, struct { if err := tpl.Execute(file, struct {
Name string Name string
Description string Description string
}{recipeName, "TODO"}); err != nil { }{recipe.Name, "TODO"}); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
return nil return nil
} }
@ -72,7 +72,7 @@ var recipeCreateCommand = &cli.Command{
logrus.Infof( logrus.Infof(
"New recipe '%s' created in %s, happy hacking!\n", "New recipe '%s' created in %s, happy hacking!\n",
recipeName, path.Join(config.APPS_DIR, recipeName), recipe.Name, path.Join(config.APPS_DIR, recipe.Name),
) )
return nil return nil

View File

@ -5,7 +5,6 @@ import (
"coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/recipe"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -27,12 +26,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 {
recipeName := internal.ValidateRecipe(c) recipe := internal.ValidateRecipe(c)
recipe, err := recipe.Get(recipeName)
if err != nil {
logrus.Fatal(err)
}
hasAppService := false hasAppService := false
for _, service := range recipe.Config.Services { for _, service := range recipe.Config.Services {
@ -42,7 +36,7 @@ the versioning metadata of up-and-running containers are.
} }
if !hasAppService { if !hasAppService {
logrus.Fatal(fmt.Sprintf("No 'app' service defined in '%s', cannot proceed", recipeName)) logrus.Fatal(fmt.Sprintf("No 'app' service defined in '%s', cannot proceed", recipe.Name))
} }
for _, service := range recipe.Config.Services { for _, service := range recipe.Config.Services {

View File

@ -8,7 +8,6 @@ import (
"coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/catalogue" "coopcloud.tech/abra/pkg/catalogue"
"coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/recipe"
"coopcloud.tech/tagcmp" "coopcloud.tech/tagcmp"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
@ -34,15 +33,10 @@ 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 {
recipeName := internal.ValidateRecipe(c) recipe := internal.ValidateRecipe(c)
recipe, err := recipe.Get(recipeName)
if err != nil {
logrus.Fatal(err)
}
for _, service := range recipe.Config.Services { for _, service := range recipe.Config.Services {
catlVersions, err := catalogue.VersionsOfService(recipeName, service.Name) catlVersions, err := catalogue.VersionsOfService(recipe.Name, service.Name)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }

View File

@ -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 {
recipeName := internal.ValidateRecipe(c) recipe := internal.ValidateRecipe(c)
catalogue, err := catalogue.ReadRecipeCatalogue() catalogue, err := catalogue.ReadRecipeCatalogue()
if err != nil { if err != nil {
@ -22,17 +22,17 @@ var recipeVersionCommand = &cli.Command{
return nil return nil
} }
recipe, ok := catalogue[recipeName] rec, ok := catalogue[recipe.Name]
if !ok { if !ok {
logrus.Fatalf("'%s' recipe doesn't exist?", recipeName) logrus.Fatalf("'%s' recipe doesn't exist?", recipe.Name)
} }
tableCol := []string{"Version", "Service", "Image", "Digest"} tableCol := []string{"Version", "Service", "Image", "Digest"}
table := formatter.CreateTable(tableCol) table := formatter.CreateTable(tableCol)
for version := range recipe.Versions { for version := range rec.Versions {
for service := range recipe.Versions[version] { for service := range rec.Versions[version] {
meta := recipe.Versions[version][service] meta := rec.Versions[version][service]
table.Append([]string{version, service, meta.Image, meta.Digest}) table.Append([]string{version, service, meta.Image, meta.Digest})
} }
} }

View File

@ -42,6 +42,10 @@ func (r Recipe) UpdateTag(image, tag string) error {
// Get retrieves a recipe. // Get retrieves a recipe.
func Get(recipeName string) (Recipe, error) { func Get(recipeName string) (Recipe, error) {
if err := EnsureExists(recipeName); err != nil {
return Recipe{}, err
}
pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, recipeName) pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, recipeName)
composeFiles, err := filepath.Glob(pattern) composeFiles, err := filepath.Glob(pattern)
if err != nil { if err != nil {