refactor(recipe): don't use README.md path directly

This commit is contained in:
p4u1 2024-07-08 11:29:20 +02:00
parent c861c09cce
commit 6f90fc3025
4 changed files with 16 additions and 17 deletions

View File

@ -57,6 +57,7 @@ keys configured on your account.
BashComplete: autocomplete.RecipeNameComplete, BashComplete: autocomplete.RecipeNameComplete,
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
recipeName := c.Args().First() recipeName := c.Args().First()
r := recipe.Get2(recipeName)
if recipeName != "" { if recipeName != "" {
internal.ValidateRecipe(c) internal.ValidateRecipe(c)
@ -103,7 +104,7 @@ keys configured on your account.
log.Warn(err) log.Warn(err)
} }
features, category, err := recipe.GetRecipeFeaturesAndCategory(recipeMeta.Name) features, category, err := recipe.GetRecipeFeaturesAndCategory(r)
if err != nil { if err != nil {
log.Warn(err) log.Warn(err)
} }

View File

@ -80,11 +80,7 @@ recipe and domain in the sample environment config).
meta := newRecipeMeta(recipeName) meta := newRecipeMeta(recipeName)
toParse := []string{ for _, path := range []string{r.ReadmePath, r.SampleEnvPath} {
path.Join(config.RECIPES_DIR, recipeName, "README.md"),
r.SampleEnvPath,
}
for _, path := range toParse {
tpl, err := template.ParseFiles(path) tpl, err := template.ParseFiles(path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -358,7 +358,8 @@ func LintHasPublishedVersion(recipe recipe.Recipe) (bool, error) {
} }
func LintMetadataFilledIn(r 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 { if err != nil {
return false, err return false, err
} }

View File

@ -215,9 +215,11 @@ func Get(recipeName string, offline bool) (Recipe, error) {
func Get2(name string) Recipe2 { func Get2(name string) Recipe2 {
dir := path.Join(config.RECIPES_DIR, name) dir := path.Join(config.RECIPES_DIR, name)
return Recipe2{ return Recipe2{
Name: name, Name: name,
Dir: dir, Dir: dir,
SSHURL: fmt.Sprintf(config.SSH_URL_TEMPLATE, name), SSHURL: fmt.Sprintf(config.SSH_URL_TEMPLATE, name),
ReadmePath: path.Join(dir, "README.md"),
SampleEnvPath: path.Join(dir, ".env.sample"), SampleEnvPath: path.Join(dir, ".env.sample"),
} }
} }
@ -227,6 +229,7 @@ type Recipe2 struct {
Dir string Dir string
SSHURL string SSHURL string
ReadmePath string
SampleEnvPath string SampleEnvPath string
} }
@ -261,22 +264,20 @@ func GetVersionLabelLocal(recipe Recipe) (string, error) {
return label, nil return label, nil
} }
func GetRecipeFeaturesAndCategory(recipeName string) (Features, string, error) { func GetRecipeFeaturesAndCategory(r Recipe2) (Features, string, error) {
feat := Features{} feat := Features{}
var category string 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(r.ReadmePath)
readmeFS, err := ioutil.ReadFile(readmePath)
if err != nil { if err != nil {
return feat, category, err return feat, category, err
} }
readmeMetadata, err := GetStringInBetween( // Find text between delimiters readmeMetadata, err := GetStringInBetween( // Find text between delimiters
recipeName, r.Name,
string(readmeFS), string(readmeFS),
"<!-- metadata -->", "<!-- endmetadata -->", "<!-- metadata -->", "<!-- endmetadata -->",
) )
@ -327,7 +328,7 @@ func GetRecipeFeaturesAndCategory(recipeName string) (Features, string, error) {
if strings.Contains(val, "**Image**") { if strings.Contains(val, "**Image**") {
imageMetadata, err := GetImageMetadata(strings.TrimSpace( imageMetadata, err := GetImageMetadata(strings.TrimSpace(
strings.TrimPrefix(val, "* **Image**:"), strings.TrimPrefix(val, "* **Image**:"),
), recipeName) ), r.Name)
if err != nil { if err != nil {
continue continue
} }