MCPcopy Create free account
hub / github.com/baidu/EasyFaaS / CommandOutput

Function CommandOutput

pkg/funclet/command/command.go:30–64  ·  view source on GitHub ↗
(command string, args ...string)

Source from the content-addressed store, hash-verified

28)
29
30func CommandOutput(command string, args ...string) (data []byte, err error) {
31 cmd := exec.Command(command, args...)
32 logs.V(9).Infof("start command %v", cmd.Args)
33 var stdout, stderr bytes.Buffer
34 cmd.Stdout = &stdout
35 cmd.Stderr = &stderr
36 if err := cmd.Start(); err != nil {
37 if exErr, ok := err.(*exec.Error); ok {
38 if exErr.Err == exec.ErrNotFound || exErr.Err == os.ErrNotExist {
39 return nil, fmt.Errorf("command %s not installed on system", command)
40 }
41 }
42 return nil, err
43 }
44
45 if err := cmd.Wait(); err != nil {
46 if sysErr, ok := err.(*os.SyscallError); ok {
47 // ignore error "no child process"
48 if e := sysErr.Err.(syscall.Errno); e == syscall.ECHILD {
49 return stdout.Bytes(), nil
50 }
51 } else {
52 logs.Errorf("command [%s] cmd.Wait err %s", cmd.Args, err)
53 }
54 logs.V(9).Infof("command %s stdout %s stderr %s", cmd.Args, stdout.Bytes(), stderr.Bytes())
55 if exitErr, ok := err.(*exec.ExitError); ok {
56 if ws, ok := exitErr.Sys().(syscall.WaitStatus); ok {
57 logs.Errorf("exit code %+v %+v", ws, exitErr)
58 }
59 return stderr.Bytes(), fmt.Errorf("command %s execution error", command)
60 }
61 return stderr.Bytes(), err
62 }
63 return stdout.Bytes(), nil
64}

Callers

nothing calls this directly

Calls 7

VFunction · 0.92
ErrorfFunction · 0.92
InfofMethod · 0.80
ErrorfMethod · 0.80
BytesMethod · 0.80
StartMethod · 0.65
WaitMethod · 0.65

Tested by

no test coverage detected