diff --git a/internal/core/path.go b/internal/core/path.go index 26246371..ad04c696 100644 --- a/internal/core/path.go +++ b/internal/core/path.go @@ -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() } diff --git a/internal/externalcmd/cmd.go b/internal/externalcmd/cmd.go index 24968178..18cfb72a 100644 --- a/internal/externalcmd/cmd.go +++ b/internal/externalcmd/cmd.go @@ -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 } diff --git a/internal/externalcmd/cmd_unix.go b/internal/externalcmd/cmd_unix.go index 1b0f45f8..59003d5a 100644 --- a/internal/externalcmd/cmd_unix.go +++ b/internal/externalcmd/cmd_unix.go @@ -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 } diff --git a/internal/externalcmd/cmd_win.go b/internal/externalcmd/cmd_win.go index 1e7c0533..1fc0e07b 100644 --- a/internal/externalcmd/cmd_win.go +++ b/internal/externalcmd/cmd_win.go @@ -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 } diff --git a/internal/hooks/on_connect.go b/internal/hooks/on_connect.go index 73353910..4ecc1b4b 100644 --- a/internal/hooks/on_connect.go +++ b/internal/hooks/on_connect.go @@ -61,7 +61,6 @@ func OnConnect(params OnConnectParams) func() { Cmdstr: params.RunOnDisconnect, Restart: false, Env: env, - OnExit: nil, } cmd.Start() } diff --git a/internal/hooks/on_demand.go b/internal/hooks/on_demand.go index 2857c225..fb413a1a 100644 --- a/internal/hooks/on_demand.go +++ b/internal/hooks/on_demand.go @@ -53,7 +53,6 @@ func OnDemand(params OnDemandParams) func(string) { Cmdstr: params.Conf.RunOnUnDemand, Restart: false, Env: env, - OnExit: nil, } cmd.Start() } diff --git a/internal/hooks/on_read.go b/internal/hooks/on_read.go index cda2dd9f..8ab82d6b 100644 --- a/internal/hooks/on_read.go +++ b/internal/hooks/on_read.go @@ -57,7 +57,6 @@ func OnRead(params OnReadParams) func() { Cmdstr: params.Conf.RunOnUnread, Restart: false, Env: env, - OnExit: nil, } cmd.Start() } diff --git a/internal/hooks/on_ready.go b/internal/hooks/on_ready.go index b8b8bf94..16a06b09 100644 --- a/internal/hooks/on_ready.go +++ b/internal/hooks/on_ready.go @@ -58,7 +58,6 @@ func OnReady(params OnReadyParams) func() { Cmdstr: params.Conf.RunOnNotReady, Restart: false, Env: env, - OnExit: nil, } cmd.Start() }