summaryrefslogtreecommitdiffhomepage
path: root/cmd/root.go
diff options
context:
space:
mode:
authorEd Zynda <[email protected]>2025-05-23 14:54:09 +0300
committerGitHub <[email protected]>2025-05-23 06:54:09 -0500
commit89e3a72ae10b96cc1d8a01a8882c6d9e81f20b6a (patch)
tree1057cf90f0c55556de740fdb6798464e88732839 /cmd/root.go
parentb9ebcea82c262dc834633c2c8f44a94fe8773a15 (diff)
downloadopencode-89e3a72ae10b96cc1d8a01a8882c6d9e81f20b6a.tar.gz
opencode-89e3a72ae10b96cc1d8a01a8882c6d9e81f20b6a.zip
feat: add support for piped input to CLI (#51)
Diffstat (limited to 'cmd/root.go')
-rw-r--r--cmd/root.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/cmd/root.go b/cmd/root.go
index 39d58eab6..ab102afe6 100644
--- a/cmd/root.go
+++ b/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")