From 40d40f7669098c5414e86f038522017bf92f5660 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Fri, 6 Mar 2026 22:07:22 +0000 Subject: [PATCH] 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 --- internal/recipe/runner.go | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/internal/recipe/runner.go b/internal/recipe/runner.go index 828d389..4443cda 100644 --- a/internal/recipe/runner.go +++ b/internal/recipe/runner.go @@ -1,7 +1,7 @@ package recipe import ( - "bufio" + _ "bufio" "fmt" "os" "path/filepath" @@ -31,7 +31,6 @@ func (r *Runner) Run() error { titleLower := strings.ToLower(step.Title) - // Only two special cases the CLI ever needs to handle switch { case strings.Contains(titleLower, "discover") || strings.Contains(titleLower, "find"): files := r.discoverFiles() @@ -44,7 +43,6 @@ func (r *Runner) Run() error { continue default: - // Everything else = pure LLM (works for any language) prompt := fmt.Sprintf(`Recipe Overview: %s @@ -79,10 +77,10 @@ Execute this step now. Respond ONLY with the expected output format — no expla 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 { 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 { if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") { @@ -101,7 +99,7 @@ func (r *Runner) discoverFiles() []string { return files } -// handleApplyStep stays exactly as you had it (dry-run patch + confirmation) +// handleApplyStep (dry-run patch + confirmation) func (r *Runner) handleApplyStep(previousResults []string) { if len(previousResults) == 0 { 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.") } -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 { blocks := make(map[string]string) @@ -144,13 +146,24 @@ func createUnifiedPatch(blocks map[string]string, patchPath string) error { if err != nil { return err } - defer f.Close() + defer func(f *os.File) { + err := f.Close() + if err != nil { + return + } + }(f) 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") { - fmt.Fprintf(f, "+%s\n", line) + _, err := fmt.Fprintf(f, "+%s\n", line) + if err != nil { + return err + } } } return nil -} \ No newline at end of file +}