From 6f90fc3025a4390d141d96a2d4f7f428d127585d Mon Sep 17 00:00:00 2001 From: p4u1 Date: Mon, 8 Jul 2024 11:29:20 +0200 Subject: [PATCH] refactor(recipe): don't use README.md path directly --- cli/catalogue/catalogue.go | 3 ++- cli/recipe/new.go | 6 +----- pkg/lint/recipe.go | 3 ++- pkg/recipe/recipe.go | 21 +++++++++++---------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/cli/catalogue/catalogue.go b/cli/catalogue/catalogue.go index 52ee6923..ae319c93 100644 --- a/cli/catalogue/catalogue.go +++ b/cli/catalogue/catalogue.go @@ -57,6 +57,7 @@ keys configured on your account. BashComplete: autocomplete.RecipeNameComplete, Action: func(c *cli.Context) error { recipeName := c.Args().First() + r := recipe.Get2(recipeName) if recipeName != "" { internal.ValidateRecipe(c) @@ -103,7 +104,7 @@ keys configured on your account. log.Warn(err) } - features, category, err := recipe.GetRecipeFeaturesAndCategory(recipeMeta.Name) + features, category, err := recipe.GetRecipeFeaturesAndCategory(r) if err != nil { log.Warn(err) } diff --git a/cli/recipe/new.go b/cli/recipe/new.go index 57de11fa..39f63a6e 100644 --- a/cli/recipe/new.go +++ b/cli/recipe/new.go @@ -80,11 +80,7 @@ recipe and domain in the sample environment config). meta := newRecipeMeta(recipeName) - toParse := []string{ - path.Join(config.RECIPES_DIR, recipeName, "README.md"), - r.SampleEnvPath, - } - for _, path := range toParse { + for _, path := range []string{r.ReadmePath, r.SampleEnvPath} { tpl, err := template.ParseFiles(path) if err != nil { log.Fatal(err) diff --git a/pkg/lint/recipe.go b/pkg/lint/recipe.go index fce04f20..85e7783c 100644 --- a/pkg/lint/recipe.go +++ b/pkg/lint/recipe.go @@ -358,7 +358,8 @@ func LintHasPublishedVersion(recipe recipe.Recipe) (bool, error) { } func LintMetadataFilledIn(r recipe.Recipe) (bool, error) { - features, category, err := recipe.GetRecipeFeaturesAndCategory(r.Name) + r2 := recipe.Get2(r.Name) + features, category, err := recipe.GetRecipeFeaturesAndCategory(r2) if err != nil { return false, err } diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index 7f23b7c1..3a3d83d5 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -215,9 +215,11 @@ func Get(recipeName string, offline bool) (Recipe, error) { func Get2(name string) Recipe2 { dir := path.Join(config.RECIPES_DIR, name) return Recipe2{ - Name: name, - Dir: dir, - SSHURL: fmt.Sprintf(config.SSH_URL_TEMPLATE, name), + Name: name, + Dir: dir, + SSHURL: fmt.Sprintf(config.SSH_URL_TEMPLATE, name), + + ReadmePath: path.Join(dir, "README.md"), SampleEnvPath: path.Join(dir, ".env.sample"), } } @@ -227,6 +229,7 @@ type Recipe2 struct { Dir string SSHURL string + ReadmePath string SampleEnvPath string } @@ -261,22 +264,20 @@ func GetVersionLabelLocal(recipe Recipe) (string, error) { return label, nil } -func GetRecipeFeaturesAndCategory(recipeName string) (Features, string, error) { +func GetRecipeFeaturesAndCategory(r Recipe2) (Features, string, error) { feat := Features{} var category string - readmePath := path.Join(config.RECIPES_DIR, recipeName, "README.md") + log.Debugf("attempting to open %s for recipe metadata parsing", r.ReadmePath) - log.Debugf("attempting to open %s for recipe metadata parsing", readmePath) - - readmeFS, err := ioutil.ReadFile(readmePath) + readmeFS, err := ioutil.ReadFile(r.ReadmePath) if err != nil { return feat, category, err } readmeMetadata, err := GetStringInBetween( // Find text between delimiters - recipeName, + r.Name, string(readmeFS), "", "", ) @@ -327,7 +328,7 @@ func GetRecipeFeaturesAndCategory(recipeName string) (Features, string, error) { if strings.Contains(val, "**Image**") { imageMetadata, err := GetImageMetadata(strings.TrimSpace( strings.TrimPrefix(val, "* **Image**:"), - ), recipeName) + ), r.Name) if err != nil { continue }