50 lines
945 B
Go
50 lines
945 B
Go
package instruction
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// Parse from a raw line
|
|
func Parse(raw string) (T, error) {
|
|
|
|
// 1. format (trim + split first space)
|
|
raw = strings.Trim(raw, " \t\n")
|
|
cmd := strings.SplitN(raw, " ", 2)
|
|
cmd[0] = strings.Trim(cmd[0], " \t")
|
|
cmd[1] = strings.Trim(cmd[1], " \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
|
|
}
|
|
|
|
// 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 "upd":
|
|
i := &update{}
|
|
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
|
|
}
|