mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-01-14 08:28:02 -05:00
Co-authored-by: chuhanjin <419436363@qq.com> Co-authored-by: maskpp <maskpp266@gmail.com> Co-authored-by: Lawliet-Chan <1576710154@qq.com> Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Co-authored-by: colinlyguo <colinlyguo@gmail.com> Co-authored-by: Péter Garamvölgyi <peter@scroll.io> Co-authored-by: colinlyguo <102356659+colinlyguo@users.noreply.github.com>
99 lines
1.7 KiB
Go
99 lines
1.7 KiB
Go
package docker
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
var verbose bool
|
|
|
|
func init() {
|
|
v := os.Getenv("LOG_DOCKER")
|
|
if v == "true" || v == "TRUE" {
|
|
verbose = true
|
|
}
|
|
}
|
|
|
|
type checkFunc func(buf string)
|
|
|
|
// Cmd struct
|
|
type Cmd struct {
|
|
*testing.T
|
|
|
|
checkFuncs sync.Map //map[string]checkFunc
|
|
|
|
//stdout bytes.Buffer
|
|
errMsg chan error
|
|
}
|
|
|
|
// NewCmd create Cmd instance.
|
|
func NewCmd(t *testing.T) *Cmd {
|
|
cmd := &Cmd{
|
|
T: t,
|
|
//stdout: bytes.Buffer{},
|
|
errMsg: make(chan error, 2),
|
|
}
|
|
// Handle panic.
|
|
cmd.RegistFunc("panic", func(buf string) {
|
|
if strings.Contains(buf, "panic") {
|
|
cmd.errMsg <- errors.New(buf)
|
|
}
|
|
})
|
|
return cmd
|
|
}
|
|
|
|
// RegistFunc register check func
|
|
func (t *Cmd) RegistFunc(key string, check checkFunc) {
|
|
t.checkFuncs.Store(key, check)
|
|
}
|
|
|
|
// UnRegistFunc unregister check func
|
|
func (t *Cmd) UnRegistFunc(key string) {
|
|
if _, ok := t.checkFuncs.Load(key); ok {
|
|
t.checkFuncs.Delete(key)
|
|
}
|
|
}
|
|
|
|
// RunCmd parallel running when parallel is true.
|
|
func (t *Cmd) RunCmd(args []string, parallel bool) {
|
|
t.Log("RunCmd cmd", args)
|
|
if parallel {
|
|
go t.runCmd(args)
|
|
} else {
|
|
t.runCmd(args)
|
|
}
|
|
}
|
|
|
|
// ErrMsg return error output channel
|
|
func (t *Cmd) ErrMsg() <-chan error {
|
|
return t.errMsg
|
|
}
|
|
|
|
func (t *Cmd) Write(data []byte) (int, error) {
|
|
out := string(data)
|
|
if verbose {
|
|
t.Logf(out)
|
|
} else if strings.Contains(out, "error") || strings.Contains(out, "warning") {
|
|
t.Logf(out)
|
|
}
|
|
go func(content string) {
|
|
t.checkFuncs.Range(func(key, value any) bool {
|
|
check := value.(checkFunc)
|
|
check(content)
|
|
return true
|
|
})
|
|
}(out)
|
|
return len(data), nil
|
|
}
|
|
|
|
func (t *Cmd) runCmd(args []string) {
|
|
cmd := exec.Command(args[0], args[1:]...) //nolint:gosec
|
|
cmd.Stdout = t
|
|
cmd.Stderr = t
|
|
_ = cmd.Run()
|
|
}
|