chore(noninteractive): accept a writer for output

Christian Rocha created

Change summary

internal/app/app.go | 7 ++++---
internal/cmd/run.go | 3 ++-
2 files changed, 6 insertions(+), 4 deletions(-)

Detailed changes

internal/app/app.go 🔗

@@ -5,6 +5,7 @@ import (
 	"database/sql"
 	"errors"
 	"fmt"
+	"io"
 	"log/slog"
 	"sync"
 	"time"
@@ -103,7 +104,7 @@ func (app *App) Config() *config.Config {
 
 // RunNonInteractive runs the application in non-interactive mode with the
 // given prompt, printing to stdout.
-func (app *App) RunNonInteractive(ctx context.Context, prompt string, quiet bool) error {
+func (app *App) RunNonInteractive(ctx context.Context, output io.Writer, prompt string, quiet bool) error {
 	slog.Info("Running in non-interactive mode")
 
 	ctx, cancel := context.WithCancel(ctx)
@@ -170,7 +171,7 @@ func (app *App) RunNonInteractive(ctx context.Context, prompt string, quiet bool
 
 		// Always print a newline at the end. If output is a TTY this will
 		// prevent the prompt from overwriting the last line of output.
-		_, _ = fmt.Print('\n')
+		_, _ = fmt.Fprintln(output)
 	}()
 
 	for {
@@ -204,7 +205,7 @@ func (app *App) RunNonInteractive(ctx context.Context, prompt string, quiet bool
 				}
 
 				part := content[readBytes:]
-				fmt.Print(part)
+				fmt.Fprint(output, part)
 				messageReadBytes[msg.ID] = len(content)
 			}
 

internal/cmd/run.go 🔗

@@ -3,6 +3,7 @@ package cmd
 import (
 	"fmt"
 	"log/slog"
+	"os"
 	"strings"
 
 	"github.com/spf13/cobra"
@@ -54,7 +55,7 @@ crush run -q "Generate a README for this project"
 		//     echo "Do something fancy" | crush run > output.txt
 		//
 		// TODO: We currently need to press ^c twice to cancel. Fix that.
-		return app.RunNonInteractive(cmd.Context(), prompt, quiet)
+		return app.RunNonInteractive(cmd.Context(), os.Stdout, prompt, quiet)
 	},
 }