MCPcopy
hub / github.com/create-go-app/cli / ExecCommand

Function ExecCommand

pkg/cgapp/exec.go:15–57  ·  view source on GitHub ↗

ExecCommand function to execute a given command.

(command string, options []string, silentMode bool)

Source from the content-addressed store, hash-verified

13
14// ExecCommand function to execute a given command.
15func ExecCommand(command string, options []string, silentMode bool) error {
16 // Checking for nil.
17 if command == "" || options == nil {
18 return fmt.Errorf("no command to execute")
19 }
20
21 // Create buffer for stderr.
22 stderr := &bytes.Buffer{}
23
24 // Collect command line.
25 cmd := exec.Command(command, options...) // #nosec G204
26
27 // Set buffer for stderr from cmd.
28 cmd.Stderr = stderr
29
30 // Create a new reader.
31 cmdReader, errStdoutPipe := cmd.StdoutPipe()
32 if errStdoutPipe != nil {
33 return ShowError(errStdoutPipe.Error())
34 }
35
36 // Start executing command.
37 if errStart := cmd.Start(); errStart != nil {
38 return ShowError(errStart.Error())
39 }
40
41 // Create a new scanner and run goroutine func with output, if not in silent mode.
42 if !silentMode {
43 scanner := bufio.NewScanner(cmdReader)
44 go func() {
45 for scanner.Scan() {
46 ShowMessage("", scanner.Text(), false, false)
47 }
48 }()
49 }
50
51 // Wait for executing command.
52 if errWait := cmd.Wait(); errWait != nil {
53 return ShowError(errWait.Error())
54 }
55
56 return nil
57}

Callers 3

runCreateCmdFunction · 0.92
runDeployCmdFunction · 0.92
TestExecCommandFunction · 0.85

Calls 2

ShowErrorFunction · 0.85
ShowMessageFunction · 0.85

Tested by 1

TestExecCommandFunction · 0.68