Skip to content

Commit

Permalink
Handle command with quote for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kaneg committed Jun 25, 2018
1 parent 9009bc8 commit bd466e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,9 @@ type CommandOutput struct {
func (fs *WebFS) ExecuteCmd() string {
r := flaskgo.GetRequest()
command := r.FormValue("command")
fmt.Println("Run command:", command)
cmd := getStartCommands(command)
buffer, err := cmd.Output()
buffer, err := cmd.CombinedOutput()
output := CommandOutput{err == nil, string(buffer)}
return returnJson(&output)
}
Expand Down
23 changes: 22 additions & 1 deletion main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,35 @@ import (
"os/exec"
"golang.org/x/sys/windows/svc"
"time"
"fmt"
"regexp"
"strings"
)

type WindowsService struct {
running func()
}

func splitCommands(command string) []string {
r := regexp.MustCompile("'.+'|\".+\"|\\S+")
m := r.FindAllString(command, -1)
for i, item := range m {
if strings.HasPrefix(item, `"`) {
item = item[1 : len(item)-1]
}
if strings.HasSuffix(item, `"`) {
item = item[:len(item)-1]
}
m[i] = item
}
return m
}

func getStartCommands(command string) *exec.Cmd {
return exec.Command("cmd", "/c", command)
command = "/c " + command
args := splitCommands(command)
fmt.Println("args:", args)
return exec.Command("cmd", args...)
}

func (m *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
Expand Down

0 comments on commit bd466e3

Please sign in to comment.