run.go

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