use cmd.Start instead of NewCmd (#5706)

This commit is contained in:
Alessandro Ros
2026-04-29 13:29:33 +02:00
committed by GitHub
parent c4fdd7c23a
commit 679925484f
8 changed files with 16 additions and 22 deletions
-2
View File
@@ -924,7 +924,6 @@ func (pa *path) startRecording() {
Cmdstr: pa.conf.RunOnRecordSegmentCreate,
Restart: false,
Env: env,
OnExit: nil,
}
cmd.Start()
}
@@ -941,7 +940,6 @@ func (pa *path) startRecording() {
Cmdstr: pa.conf.RunOnRecordSegmentComplete,
Restart: false,
Env: env,
OnExit: nil,
}
cmd.Start()
}
+10 -10
View File
@@ -34,15 +34,6 @@ type Cmd struct {
// Start starts the command.
func (c *Cmd) Start() {
// replace variables in both Linux and Windows, in order to allow using the
// same commands on both of them.
c.Cmdstr = os.Expand(c.Cmdstr, func(variable string) string {
if value, ok := c.Env[variable]; ok {
return value
}
return os.Getenv(variable)
})
if c.OnExit == nil {
c.OnExit = func(_ error) {}
}
@@ -62,13 +53,22 @@ func (c *Cmd) Close() {
func (c *Cmd) run() {
defer c.Pool.wg.Done()
// replace variables in both Linux and Windows, in order to allow using the
// same commands on both of them.
cmdstr := os.Expand(c.Cmdstr, func(variable string) string {
if value, ok := c.Env[variable]; ok {
return value
}
return os.Getenv(variable)
})
env := append([]string(nil), os.Environ()...)
for key, val := range c.Env {
env = append(env, key+"="+val)
}
for {
err := c.runOSSpecific(env)
err := c.runOSSpecific(cmdstr, env)
if errors.Is(err, errTerminated) {
return
}
+2 -2
View File
@@ -12,8 +12,8 @@ import (
"github.com/kballard/go-shellquote"
)
func (c *Cmd) runOSSpecific(env []string) error {
cmdParts, err := shellquote.Split(c.Cmdstr)
func (c *Cmd) runOSSpecific(cmdstr string, env []string) error {
cmdParts, err := shellquote.Split(cmdstr)
if err != nil {
return err
}
+4 -4
View File
@@ -61,7 +61,7 @@ func addProcessToGroup(h windows.Handle, p *os.Process) error {
return nil
}
func (c *Cmd) runOSSpecific(env []string) error {
func (c *Cmd) runOSSpecific(cmdstr string, env []string) error {
var cmd *exec.Cmd
// from Golang documentation:
@@ -71,15 +71,15 @@ func (c *Cmd) runOSSpecific(env []string) error {
// msiexec.exe and cmd.exe (and thus, all batch files), which have a different unquoting algorithm.
// In these or other similar cases, you can do the quoting yourself and provide the full command
// line in SysProcAttr.CmdLine, leaving Args empty.
if strings.HasPrefix(c.Cmdstr, "cmd ") || strings.HasPrefix(c.Cmdstr, "cmd.exe ") {
args := strings.TrimPrefix(strings.TrimPrefix(c.Cmdstr, "cmd "), "cmd.exe ")
if strings.HasPrefix(cmdstr, "cmd ") || strings.HasPrefix(cmdstr, "cmd.exe ") {
args := strings.TrimPrefix(strings.TrimPrefix(cmdstr, "cmd "), "cmd.exe ")
cmd = exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{
CmdLine: args,
}
} else {
cmdParts, err := shellquote.Split(c.Cmdstr)
cmdParts, err := shellquote.Split(cmdstr)
if err != nil {
return err
}
-1
View File
@@ -61,7 +61,6 @@ func OnConnect(params OnConnectParams) func() {
Cmdstr: params.RunOnDisconnect,
Restart: false,
Env: env,
OnExit: nil,
}
cmd.Start()
}
-1
View File
@@ -53,7 +53,6 @@ func OnDemand(params OnDemandParams) func(string) {
Cmdstr: params.Conf.RunOnUnDemand,
Restart: false,
Env: env,
OnExit: nil,
}
cmd.Start()
}
-1
View File
@@ -57,7 +57,6 @@ func OnRead(params OnReadParams) func() {
Cmdstr: params.Conf.RunOnUnread,
Restart: false,
Env: env,
OnExit: nil,
}
cmd.Start()
}
-1
View File
@@ -58,7 +58,6 @@ func OnReady(params OnReadyParams) func() {
Cmdstr: params.Conf.RunOnNotReady,
Restart: false,
Env: env,
OnExit: nil,
}
cmd.Start()
}