Fix #54: cmd parsing improved. It is possible set multiple command on ticker.commands with single or double quote.

This commit is contained in:
Matrix86 2018-02-16 18:31:56 +01:00
commit b83823283d

View file

@ -52,14 +52,67 @@ type Session struct {
Events *EventPool `json:"-"`
}
func ParseCommands(buffer string) []string {
func ParseCommands(line string) []string {
args := []string{}
buf := ""
singleQuoted := false
doubleQuoted := false
finish := false
for _, c := range line {
switch c {
case ';':
if singleQuoted == false && doubleQuoted == false {
finish = true
} else {
buf += string(c)
}
case '"':
if doubleQuoted {
// finish of quote
doubleQuoted = false
} else if singleQuoted {
// quote initiated with ', so we ignore it
buf += string(c)
} else {
// quote init here
doubleQuoted = true
}
case '\'':
if singleQuoted {
singleQuoted = false
} else if doubleQuoted {
buf += string(c)
} else {
singleQuoted = true
}
default:
buf += string(c)
}
if finish {
args = append(args, buf)
finish = false
buf = ""
}
}
if len(buf) > 0 {
args = append(args, buf)
}
cmds := make([]string, 0)
for _, cmd := range strings.Split(buffer, ";") {
for _, cmd := range args {
cmd = core.Trim(cmd)
if cmd != "" || (len(cmd) > 0 && cmd[0] != '#') {
cmds = append(cmds, cmd)
}
}
return cmds
}