Skip to content

Commit

Permalink
Add stdin as input-parameter to exec-bee (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrauser authored and muesli committed Apr 26, 2019
1 parent e31b5f0 commit 70d4a80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
19 changes: 19 additions & 0 deletions bees/execbee/execbee.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
* Authors:
* Dominik Schmidt <domme@tomahawk-player.org>
* Christian Muehlhaeuser <muesli@gmail.com>
* Matthias Krauser <matthias@krauser.eu>
*/

// Package execbee is a Bee that can launch external processes.
package execbee

import (
"bufio"
"io"
"os/exec"
"strings"

Expand All @@ -45,13 +47,30 @@ func (mod *ExecBee) Action(action bees.Action) []bees.Placeholder {
switch action.Name {
case "execute":
var command string
var stdin string

action.Options.Bind("command", &command)
action.Options.Bind("stdin", &stdin)
mod.Logln("Executing locally: ", command)

go func() {
c := strings.Split(command, " ")
cmd := exec.Command(c[0], c[1:]...)

if stdin != "" {
stdinPipe, err := cmd.StdinPipe()

if err != nil {
mod.LogFatal("Error creating stdinPipe for Cmd", err)
return
}

go func() {
io.WriteString(stdinPipe, stdin)
stdinPipe.Close()
}()
}

// read and print stdout
outReader, err := cmd.StdoutPipe()
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions bees/execbee/execbeefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Authors:
* Dominik Schmidt <domme@tomahawk-player.org>
* Christian Muehlhaeuser <muesli@gmail.com>
* Matthias Krauser <matthias@krauser.eu>
*/

package execbee
Expand Down Expand Up @@ -104,6 +105,12 @@ func (factory *ExecBeeFactory) Actions() []bees.ActionDescriptor {
Type: "string",
Mandatory: true,
},
{
Name: "stdin",
Description: "stdin-Data for the command",
Type: "string",
Mandatory: false,
},
},
},
}
Expand Down

0 comments on commit 70d4a80

Please sign in to comment.