Little refactor on command creation

This commit is contained in:
Andrey Nering
2017-03-12 17:18:59 -03:00
parent 678ea86350
commit 8c5e7e89cd
5 changed files with 62 additions and 28 deletions

22
execext/exec.go Normal file
View File

@@ -0,0 +1,22 @@
package execext
import (
"os/exec"
)
var (
// ShPath is path to "sh" command
ShPath string
// ShExists is true if "sh" command is available on the system
ShExists bool
)
func init() {
var err error
ShPath, err = exec.LookPath("sh")
ShExists = err == nil
}
func newShCommand(c string) *exec.Cmd {
return exec.Command(ShPath, "-c", c)
}

13
execext/exec_other.go Normal file
View File

@@ -0,0 +1,13 @@
// +build !windows
package execext
import (
"os/exec"
)
// NewCommand returns a new command that runs on "sh" is available or on "cmd"
// otherwise on Windows
func NewCommand(c string) *exec.Cmd {
return newShCommand(c)
}

20
execext/exec_win.go Normal file
View File

@@ -0,0 +1,20 @@
// +build windows
package execext
import (
"os/exec"
)
// NewCommand returns a new command that runs on "sh" is available or on "cmd"
// otherwise on Windows
func NewCommand(c string) *exec.Cmd {
if ShExists {
return newShCommand(c)
}
return newCmdCommand(c)
}
func newCmdCommand(c string) {
return exec.Command("cmd", "/C", c)
}