feat: add support for piped input to CLI (#51)

This commit is contained in:
Ed Zynda
2025-05-23 14:54:09 +03:00
committed by GitHub
parent b9ebcea82c
commit 89e3a72ae1
3 changed files with 187 additions and 1 deletions

View File

@@ -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")