Skip to content

Commit

Permalink
Update root command to handle stdin and args
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Sep 18, 2023
1 parent b8001d6 commit 3d22d38
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ $ echo 'QUESTION' | askai
$ askai < question.txt
```

It is possible to pass both input from stdin and arguments at the same time.
By utilizing this, for example, you can also summarize the contents of a specific file.

```console
$ cat README.md | askai 'Please summarize this content'
```

## LICENSE

[MIT](./LICENSE)
24 changes: 14 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,26 @@ var rootCmd = &cobra.Command{
uicfg.Model = openai.GPT3Dot5Turbo
}

var q string
q := ""
if len(args) > 0 {
q = strings.Join(args, " ")
} else {
info, err := os.Stdin.Stat()
if err != nil {
}

info, err := os.Stdin.Stat()
if err != nil {
return err
}
if info.Mode()&os.ModeCharDevice == 0 {
b := new(bytes.Buffer)
if _, err := b.ReadFrom(os.Stdin); err != nil {
return err
}
if info.Mode()&os.ModeCharDevice == 0 {
b := new(bytes.Buffer)
if _, err := b.ReadFrom(os.Stdin); err != nil {
return err
}
q = b.String()
if q != "" {
q += "\n\n"
}
q += b.String()
}

if q != "" {
uicfg.Question = &q
}
Expand Down

0 comments on commit 3d22d38

Please sign in to comment.