-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix execution of external commands with quotes on Windows (#113)
- Loading branch information
Showing
7 changed files
with
115 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// +build !windows | ||
|
||
package externalcmd | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func (e *ExternalCmd) runInner() bool { | ||
cmd := exec.Command("/bin/sh", "-c", "exec "+e.cmdstr) | ||
|
||
cmd.Env = append(os.Environ(), | ||
"RTSP_PATH="+e.env.Path, | ||
"RTSP_PORT="+e.env.Port, | ||
) | ||
|
||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
err := cmd.Start() | ||
if err != nil { | ||
return true | ||
} | ||
|
||
cmdDone := make(chan struct{}) | ||
go func() { | ||
defer close(cmdDone) | ||
cmd.Wait() | ||
}() | ||
|
||
select { | ||
case <-e.terminate: | ||
cmd.Process.Signal(os.Interrupt) | ||
<-cmdDone | ||
return false | ||
|
||
case <-cmdDone: | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// +build windows | ||
|
||
package externalcmd | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/kballard/go-shellquote" | ||
) | ||
|
||
func (e *ExternalCmd) runInner() bool { | ||
// on Windows the shell is not used and command is started directly | ||
// variables are replaced manually in order to guarantee compatibility | ||
// with Linux commands | ||
tmp := strings.ReplaceAll(e.cmdstr, "$RTSP_PATH", e.env.Path) | ||
tmp = strings.ReplaceAll(tmp, "$RTSP_PORT", e.env.Port) | ||
|
||
parts, err := shellquote.Split(tmp) | ||
if err != nil { | ||
return true | ||
} | ||
|
||
cmd := exec.Command(parts[0], parts[1:]...) | ||
|
||
cmd.Env = append(os.Environ(), | ||
"RTSP_PATH="+e.env.Path, | ||
"RTSP_PORT="+e.env.Port, | ||
) | ||
|
||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
err := cmd.Start() | ||
if err != nil { | ||
return true | ||
} | ||
|
||
cmdDone := make(chan struct{}) | ||
go func() { | ||
defer close(cmdDone) | ||
cmd.Wait() | ||
}() | ||
|
||
select { | ||
case <-e.terminate: | ||
// on Windows it's not possible to send os.Interrupt to a process | ||
// Kill() is the only supported way | ||
cmd.Process.Kill() | ||
<-cmdDone | ||
return false | ||
|
||
case <-cmdDone: | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters