Skip to content

Commit

Permalink
add piping support
Browse files Browse the repository at this point in the history
  • Loading branch information
Daulet Zhanguzin committed May 5, 2024
1 parent da9f845 commit 9a2273a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

# cmd

Most common usage is to pass to your message as an argument:
Most common usage is to pass your message as an argument (wrap it in quotes if it contains special shell modifiers like `*` or `?`): `cmd hi` or `cmd "what's up?"`. Addtionally, you can pipe content to it:
```bash
$ cmd hi
Hello! How can I help you today?
cat README.md | cmd briefly describe functionality
Cmd is a versatile command-line tool that leverages AI to understand natural language input and generate shell commands or even entire scripts. It offers various flags to enhance its functionality. You can use `-run` to execute the generated command and display the output, while `-exec` executes the command without showing the generation process or output. For multi-turn interactions, there's `-chat`, and you can configure the model, connectors, and other settings with `-config` and related flags. Additionally, content can be piped into cmd for quick processing.
```
## run code
Expand Down
22 changes: 17 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,33 @@ func cmd(ctx context.Context) error {
return response, nil
}

var pipeContent string
// Check if there is input from the pipe (stdin)
if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) == 0 {
pipeBytes, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("failed to read from pipe: %w", err)
}
pipeContent = string(pipeBytes)
}

var err error
switch {
case *chat:
err = multiTurn(ctx, os.Stdout, os.Stdin, turnFn)
default:
usrMsg := strings.Join(flag.Args(), " ")
if pipeContent != "" {
usrMsg = fmt.Sprintf("%s\n%s", pipeContent, usrMsg)
}
_, err = turnFn(ctx, os.Stdout, []*co.ChatMessage{
{
Role: co.ChatMessageRoleUser,
Message: strings.Join(flag.Args(), " ")},
Message: usrMsg,
},
})
}
if err != nil {
return err
}
return nil
return err
}

func main() {
Expand Down

0 comments on commit 9a2273a

Please sign in to comment.