2015-03-26 23:52:03 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/flynn/go-shlex"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SplitCommandAndArgs takes a command string and parses it
|
|
|
|
// shell-style into the command and its separate arguments.
|
|
|
|
func SplitCommandAndArgs(command string) (cmd string, args []string, err error) {
|
|
|
|
parts, err := shlex.Split(command)
|
|
|
|
if err != nil {
|
2015-06-08 08:49:17 +08:00
|
|
|
err = errors.New("error parsing command: " + err.Error())
|
2015-03-26 23:52:03 +08:00
|
|
|
return
|
|
|
|
} else if len(parts) == 0 {
|
2015-06-08 08:49:17 +08:00
|
|
|
err = errors.New("no command contained in '" + command + "'")
|
2015-03-26 23:52:03 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = parts[0]
|
|
|
|
if len(parts) > 1 {
|
|
|
|
args = parts[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|