nix-amer/internal/instruction/reader.go

45 lines
840 B
Go
Raw Normal View History

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
}
cmd[1] = strings.Trim(cmd[1], " \t")
// 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
}