refactor(recipe): clean up runner implementation

- Adjust bufio import to blank (likely for side effects or pending use)
- Refine comments for clarity and remove unnecessary ones
- Split regex string to avoid backtick collisions in literals
- Add error handling to patch writing in createUnifiedPatch
- Minor formatting and defer close adjustments
This commit is contained in:
Greg Gauthier 2026-03-06 22:07:22 +00:00
parent 7b415f8e26
commit 40d40f7669

View File

@ -1,7 +1,7 @@
package recipe package recipe
import ( import (
"bufio" _ "bufio"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -31,7 +31,6 @@ func (r *Runner) Run() error {
titleLower := strings.ToLower(step.Title) titleLower := strings.ToLower(step.Title)
// Only two special cases the CLI ever needs to handle
switch { switch {
case strings.Contains(titleLower, "discover") || strings.Contains(titleLower, "find"): case strings.Contains(titleLower, "discover") || strings.Contains(titleLower, "find"):
files := r.discoverFiles() files := r.discoverFiles()
@ -44,7 +43,6 @@ func (r *Runner) Run() error {
continue continue
default: default:
// Everything else = pure LLM (works for any language)
prompt := fmt.Sprintf(`Recipe Overview: prompt := fmt.Sprintf(`Recipe Overview:
%s %s
@ -79,10 +77,10 @@ Execute this step now. Respond ONLY with the expected output format — no expla
return nil return nil
} }
// discoverFiles does a real filesystem scan — generic enough for any Go project // discoverFiles does a real filesystem scan (used by "Discover files" steps)
func (r *Runner) discoverFiles() []string { func (r *Runner) discoverFiles() []string {
var files []string var files []string
root := "internal" // matches the recipe default; we can make it parametric later root := "internal"
_ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { _ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") { if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") {
@ -101,7 +99,7 @@ func (r *Runner) discoverFiles() []string {
return files return files
} }
// handleApplyStep stays exactly as you had it (dry-run patch + confirmation) // handleApplyStep (dry-run patch + confirmation)
func (r *Runner) handleApplyStep(previousResults []string) { func (r *Runner) handleApplyStep(previousResults []string) {
if len(previousResults) == 0 { if len(previousResults) == 0 {
fmt.Println(" ⚠️ No previous results to apply — skipping.") fmt.Println(" ⚠️ No previous results to apply — skipping.")
@ -126,7 +124,11 @@ func (r *Runner) handleApplyStep(previousResults []string) {
fmt.Println(" Review it, then run with dry_run=false to apply.") fmt.Println(" Review it, then run with dry_run=false to apply.")
} }
var blockRe = regexp.MustCompile(`(?s)^//\s*(.+?\.go)\n```go\n(.*?)\n````) // regex split across lines to avoid backtick collision
var blockRe = regexp.MustCompile(`(?s)^//\s*(.+?\.go)\n` +
"```go\n" +
`(.*?)\n` +
"```")
func extractCodeBlocks(text string) map[string]string { func extractCodeBlocks(text string) map[string]string {
blocks := make(map[string]string) blocks := make(map[string]string)
@ -144,13 +146,24 @@ func createUnifiedPatch(blocks map[string]string, patchPath string) error {
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer func(f *os.File) {
err := f.Close()
if err != nil {
return
}
}(f)
for path, content := range blocks { for path, content := range blocks {
fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", path, path, strings.Count(content, "\n")+1) _, err := fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", path, path, strings.Count(content, "\n")+1)
if err != nil {
return err
}
for _, line := range strings.Split(content, "\n") { for _, line := range strings.Split(content, "\n") {
fmt.Fprintf(f, "+%s\n", line) _, err := fmt.Fprintf(f, "+%s\n", line)
if err != nil {
return err
}
} }
} }
return nil return nil
} }