nix-amer/internal/instruction/reader.go

45 lines
835 B
Go

package instruction
import (
"strings"
)
// Parse from a raw line
func Parse(raw string) (T, error) {
// 1. format (trim + split first space)
split := strings.SplitN(raw, " ", 2)
if len(split) < 2 { // at least 1 command + arguments
return nil, ErrInvalidSyntax
}
split[0] = strings.Trim(split[0], " \t")
// 2. fail if invalid base syntax 'cmd args...'
split[1] = strings.Trim(split[1], " \t")
// 3. Extract instruction type
switch split[0] {
case "install":
i := &install{}
err := i.Build(split[1])
return i, err
case "delete":
i := &delete{}
err := i.Build(split[1])
return i, err
case "service":
i := &service{}
err := i.Build(split[1])
return i, err
case "set":
i := &set{}
err := i.Build(split[1])
return i, err
default:
return nil, ErrUnknownInstruction
}
// return nil, nil
}