-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
run_windows.go
53 lines (42 loc) · 1.01 KB
/
run_windows.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
46
47
48
49
50
51
52
53
//go:build windows
package exec
import (
"bytes"
"fmt"
"os"
"os/exec"
"syscall"
"time"
"github.com/kballard/go-shellquote"
"github.com/influxdata/telegraf/internal"
)
func (c commandRunner) run(
command string,
environments []string,
timeout time.Duration,
) ([]byte, []byte, error) {
splitCmd, err := shellquote.Split(command)
if err != nil || len(splitCmd) == 0 {
return nil, nil, fmt.Errorf("exec: unable to parse command: %w", err)
}
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
if len(environments) > 0 {
cmd.Env = append(os.Environ(), environments...)
}
var (
out bytes.Buffer
stderr bytes.Buffer
)
cmd.Stdout = &out
cmd.Stderr = &stderr
runErr := internal.RunTimeout(cmd, timeout)
out = removeWindowsCarriageReturns(out)
if stderr.Len() > 0 && !c.debug {
stderr = removeWindowsCarriageReturns(stderr)
stderr = c.truncate(stderr)
}
return out.Bytes(), stderr.Bytes(), runErr
}