1package cmd
 2
 3import (
 4	"fmt"
 5	"log/slog"
 6	"strings"
 7
 8	"github.com/charmbracelet/crush/internal/server"
 9	"github.com/spf13/cobra"
10)
11
12var runCmd = &cobra.Command{
13	Use:   "run [prompt...]",
14	Short: "Run a single non-interactive prompt",
15	Long: `Run a single prompt in non-interactive mode and exit.
16The prompt can be provided as arguments or piped from stdin.`,
17	Example: `
18# Run a simple prompt
19crush run Explain the use of context in Go
20
21# Pipe input from stdin
22echo "What is this code doing?" | crush run
23
24# Run with quiet mode (no spinner)
25crush run -q "Generate a README for this project"
26  `,
27	RunE: func(cmd *cobra.Command, args []string) error {
28		quiet, _ := cmd.Flags().GetBool("quiet")
29		hostURL, err := server.ParseHostURL(clientHost)
30		if err != nil {
31			return fmt.Errorf("invalid host URL: %v", err)
32		}
33
34		c, ins, err := setupApp(cmd, hostURL)
35		if err != nil {
36			return err
37		}
38		defer func() { c.DeleteInstance(cmd.Context(), ins.ID) }()
39
40		cfg, err := c.GetConfig(cmd.Context(), ins.ID)
41		if err != nil {
42			return fmt.Errorf("failed to get config: %v", err)
43		}
44
45		if !cfg.IsConfigured() {
46			return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
47		}
48
49		prompt := strings.Join(args, " ")
50
51		prompt, err = MaybePrependStdin(prompt)
52		if err != nil {
53			slog.Error("Failed to read from stdin", "error", err)
54			return err
55		}
56
57		if prompt == "" {
58			return fmt.Errorf("no prompt provided")
59		}
60
61		// Run non-interactive flow using the App method
62		// return c.RunNonInteractive(cmd.Context(), prompt, quiet)
63		// TODO: implement non-interactive run
64		_ = quiet
65		return nil
66	},
67}
68
69func init() {
70	runCmd.Flags().BoolP("quiet", "q", false, "Hide spinner")
71}