From a30e0cf829b24ee505a72655ce93960586888f20 Mon Sep 17 00:00:00 2001 From: Shalom Yiblet Date: Tue, 6 Feb 2024 11:13:13 -0800 Subject: [PATCH] feat: allow `ask` to pass in stdin via `-a -` --- ask_cmd.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/ask_cmd.go b/ask_cmd.go index d934c9b..783d18a 100644 --- a/ask_cmd.go +++ b/ask_cmd.go @@ -32,7 +32,7 @@ type askCmd struct { Temperature float32 `default:"0.7"` Bash bool `arg:"--bash" help:"output only valid bash"` Model string `arg:"--model,-m" help:"set openai model"` - Attach []string `arg:"--attach,-a,separate" help:"attach additional files at the end of the message"` + Attach []string `arg:"--attach,-a,separate" help:"attach additional files at the end of the message. pass '-' to pass in stdin"` } func (args *askCmd) buildContent(ctx context.Context) (string, error) { @@ -51,12 +51,20 @@ func (args *askCmd) buildContent(ctx context.Context) (string, error) { for _, a := range args.Attach { sb.WriteRune('\n') - file, err := os.Open(a) - if err != nil { - return "", err + + var file *os.File + if a == "-" { + file = os.Stdin + } else { + var err error + file, err = os.Open(a) + if err != nil { + return "", err + } } defer file.Close() - _, err = io.Copy(&sb, file) + + _, err := io.Copy(&sb, file) if err != nil { return "", err }