2
0

run.go 1.3 KB

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