forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupergloo_cli.go
45 lines (36 loc) · 817 Bytes
/
supergloo_cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package utils
import (
"bytes"
"io"
"os"
"strings"
"github.com/solo-io/supergloo/cli/pkg/cmd"
)
func Supergloo(args string) error {
app := cmd.SuperglooCli("test")
app.SetArgs(strings.Split(args, " "))
return app.Execute()
}
func SuperglooOut(args string) (string, error) {
stdOut := os.Stdout
r, w, err := os.Pipe()
if err != nil {
return "", err
}
os.Stdout = w
app := cmd.SuperglooCli("test")
app.SetArgs(strings.Split(args, " "))
err = app.Execute()
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
// back to normal state
w.Close()
os.Stdout = stdOut // restoring the real stdout
out := <-outC
return strings.TrimSuffix(out, "\n"), nil
}