2018-11-08 12:41:07 +00:00
|
|
|
package instruction
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse from a raw line
|
|
|
|
func Parse(raw string) (T, error) {
|
|
|
|
|
|
|
|
// 1. format (trim + split first space)
|
|
|
|
cmd := strings.SplitN(raw, " ", 2)
|
|
|
|
cmd[0] = strings.Trim(cmd[0], " \t")
|
|
|
|
|
|
|
|
// 2. fail if invalid base syntax 'cmd args...'
|
|
|
|
// where command is 3 letters
|
|
|
|
if len(cmd) < 2 || len(cmd[0]) < 3 || cmd[0][1] == ' ' {
|
|
|
|
return nil, ErrInvalidSyntax
|
|
|
|
}
|
2018-11-10 11:37:31 +00:00
|
|
|
cmd[1] = strings.Trim(cmd[1], " \t")
|
2018-11-08 12:41:07 +00:00
|
|
|
|
|
|
|
// 3. Extract instruction type
|
|
|
|
switch cmd[0] {
|
|
|
|
case "ins":
|
|
|
|
i := &install{}
|
|
|
|
err := i.Build(cmd[1])
|
|
|
|
return i, err
|
|
|
|
case "del":
|
|
|
|
i := &delete{}
|
|
|
|
err := i.Build(cmd[1])
|
|
|
|
return i, err
|
|
|
|
case "ser":
|
|
|
|
i := &service{}
|
|
|
|
err := i.Build(cmd[1])
|
|
|
|
return i, err
|
|
|
|
case "cnf":
|
|
|
|
i := &config{}
|
|
|
|
err := i.Build(cmd[1])
|
|
|
|
return i, err
|
|
|
|
default:
|
|
|
|
return nil, ErrUnknownInstruction
|
|
|
|
}
|
|
|
|
|
|
|
|
// return nil, nil
|
|
|
|
}
|