kubevela/e2e/cli.go

45 lines
956 B
Go
Raw Normal View History

package e2e
2020-07-31 18:26:36 +08:00
import (
"os"
"os/exec"
"path"
"strings"
"time"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega/gexec"
)
var (
rudrPath = ""
)
//GetCliBinary is to build rudr binary.
func GetCliBinary() (string, error) {
// TODO(zzxwill) Need to check before building from scratch every time
cwd, _ := os.Getwd()
rudrPath = path.Join(cwd, "..")
mainPath := path.Join(rudrPath, "../cmd/rudrx/main.go")
2020-07-31 18:26:36 +08:00
cmd := exec.Command("go", "build", "-o", path.Join(rudrPath, "rudr"), mainPath)
_, err := cmd.Output()
return rudrPath, err
}
func Exec(cli string) (string, error) {
c := strings.Fields(cli)
commandName := path.Join(rudrPath, c[0])
command := exec.Command(commandName, c[1:]...)
var output []byte
session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)
if err != nil {
return string(output), err
}
s := session.Wait(10 * time.Second)
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
}