46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
|
|
package recipe
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ListRecipes returns a list of recipe names in the given directory.
|
||
|
|
// It looks for .md files that contain recipe frontmatter.
|
||
|
|
func ListRecipes(dir string) ([]string, error) {
|
||
|
|
if dir == "" {
|
||
|
|
dir = "."
|
||
|
|
}
|
||
|
|
|
||
|
|
var recipes []string
|
||
|
|
|
||
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
if info.IsDir() {
|
||
|
|
// Skip hidden directories and common ignore dirs
|
||
|
|
name := info.Name()
|
||
|
|
if strings.HasPrefix(name, ".") && name != "." || name == "node_modules" || name == "vendor" {
|
||
|
|
return filepath.SkipDir
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
if strings.HasSuffix(strings.ToLower(info.Name()), ".md") {
|
||
|
|
// Simple check for recipe files (contains "Objective:" or "Instructions:")
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err == nil && (strings.Contains(string(data), "**Objective:**") || strings.Contains(string(data), "Objective:")) {
|
||
|
|
name := strings.TrimSuffix(info.Name(), ".md")
|
||
|
|
recipes = append(recipes, name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
|
||
|
|
return recipes, err
|
||
|
|
}
|