mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-21 17:54:23 +01:00
feat: add support for piped input to CLI (#51)
This commit is contained in:
30
cmd/root.go
30
cmd/root.go
@@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -91,6 +92,16 @@ to assist developers in writing, debugging, and understanding code directly from
|
||||
|
||||
// Check if we're in non-interactive mode
|
||||
prompt, _ := cmd.Flags().GetString("prompt")
|
||||
|
||||
// Check for piped input if no prompt was provided via flag
|
||||
if prompt == "" {
|
||||
pipedInput, hasPipedInput := checkStdinPipe()
|
||||
if hasPipedInput {
|
||||
prompt = pipedInput
|
||||
}
|
||||
}
|
||||
|
||||
// If we have a prompt (either from flag or piped input), run in non-interactive mode
|
||||
if prompt != "" {
|
||||
outputFormatStr, _ := cmd.Flags().GetString("output-format")
|
||||
outputFormat := format.OutputFormat(outputFormatStr)
|
||||
@@ -311,6 +322,25 @@ func Execute() {
|
||||
}
|
||||
}
|
||||
|
||||
// checkStdinPipe checks if there's data being piped into stdin
|
||||
func checkStdinPipe() (string, bool) {
|
||||
// Check if stdin is not a terminal (i.e., it's being piped)
|
||||
stat, _ := os.Stdin.Stat()
|
||||
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||
// Read all data from stdin
|
||||
data, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// If we got data, return it
|
||||
if len(data) > 0 {
|
||||
return string(data), true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().BoolP("help", "h", false, "Help")
|
||||
rootCmd.Flags().BoolP("version", "v", false, "Version")
|
||||
|
||||
Reference in New Issue
Block a user