| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package cmd
- import (
- "context"
- "fmt"
- "log/slog"
- "os"
- "os/signal"
- "strings"
- "github.com/spf13/cobra"
- )
- var runCmd = &cobra.Command{
- Use: "run [prompt...]",
- Short: "Run a single non-interactive prompt",
- Long: `Run a single prompt in non-interactive mode and exit.
- The prompt can be provided as arguments or piped from stdin.`,
- Example: `
- # Run a simple prompt
- crush run Explain the use of context in Go
- # Pipe input from stdin
- curl https://charm.land | crush run "Summarize this website"
- # Read from a file
- crush run "What is this code doing?" <<< prrr.go
- # Run in quiet mode (hide the spinner)
- crush run --quiet "Generate a README for this project"
- `,
- RunE: func(cmd *cobra.Command, args []string) error {
- quiet, _ := cmd.Flags().GetBool("quiet")
- // Cancel on SIGINT or SIGTERM.
- ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
- defer cancel()
- app, err := setupApp(cmd)
- if err != nil {
- return err
- }
- defer app.Shutdown()
- if !app.Config().IsConfigured() {
- return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
- }
- prompt := strings.Join(args, " ")
- prompt, err = MaybePrependStdin(prompt)
- if err != nil {
- slog.Error("Failed to read from stdin", "error", err)
- return err
- }
- if prompt == "" {
- return fmt.Errorf("no prompt provided")
- }
- return app.RunNonInteractive(ctx, os.Stdout, prompt, quiet)
- },
- }
- func init() {
- runCmd.Flags().BoolP("quiet", "q", false, "Hide spinner")
- }
|