Skip to content

Commit 69a2cde

Browse files
committed
Support hooking in a reader for stdin
This will be used in arkade to pipe the input to kubectl apply. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent 8697e4e commit 69a2cde

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

pkg/v1/exec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ type ExecTask struct {
1616
Env []string
1717
Cwd string
1818

19+
// Stdin connect a reader to stdin for the command
20+
// being executed.
21+
Stdin io.Reader
22+
1923
// StreamStdio prints stdout and stderr directly to os.Stdout/err as
2024
// the command runs.
2125
StreamStdio bool
@@ -86,6 +90,9 @@ func (et ExecTask) Execute() (ExecResult, error) {
8690
}
8791
}
8892
}
93+
if et.Stdin != nil {
94+
cmd.Stdin = et.Stdin
95+
}
8996

9097
stdoutBuff := bytes.Buffer{}
9198
stderrBuff := bytes.Buffer{}

pkg/v1/exec_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package execute
22

33
import (
4+
"bytes"
45
"os"
56
"strings"
67
"testing"
@@ -25,6 +26,49 @@ func TestExec_WithShell(t *testing.T) {
2526
}
2627
}
2728

29+
func TestExec_CatTransformString(t *testing.T) {
30+
input := "1 line 1"
31+
32+
reader := bytes.NewReader([]byte(input))
33+
want := " 1\t1 line 1"
34+
35+
task := ExecTask{Command: "cat", Shell: false, Args: []string{"-b"}, Stdin: reader}
36+
res, err := task.Execute()
37+
if err != nil {
38+
t.Errorf(err.Error())
39+
t.Fail()
40+
}
41+
42+
if res.Stdout != want {
43+
t.Errorf("want %q, got %q", want, res.Stdout)
44+
t.Fail()
45+
}
46+
}
47+
48+
func TestExec_CatWC(t *testing.T) {
49+
input := `this
50+
has
51+
four
52+
lines
53+
`
54+
55+
reader := bytes.NewReader([]byte(input))
56+
want := "4"
57+
58+
task := ExecTask{Command: "wc", Shell: false, Args: []string{"-l"}, Stdin: reader}
59+
res, err := task.Execute()
60+
if err != nil {
61+
t.Errorf(err.Error())
62+
t.Fail()
63+
}
64+
65+
got := strings.TrimSpace(res.Stdout)
66+
if got != want {
67+
t.Errorf("want %q, got %q", want, got)
68+
t.Fail()
69+
}
70+
}
71+
2872
func TestExec_WithEnvVars(t *testing.T) {
2973
task := ExecTask{Command: "env", Shell: false, Env: []string{"GOTEST=1", "GOTEST2=2"}}
3074
res, err := task.Execute()
@@ -42,7 +86,6 @@ func TestExec_WithEnvVars(t *testing.T) {
4286
t.Errorf("want env to show GOTEST2=2 since we passed that variable")
4387
t.Fail()
4488
}
45-
4689
}
4790

4891
func TestExec_WithEnvVarsInheritedFromParent(t *testing.T) {

0 commit comments

Comments
 (0)