nix-amer/internal/instruction/reader.go

57 lines
1.0 KiB
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)
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 "run":
i := &run{}
err := i.Build(split[1])
return i, err
case "set":
i := &set{}
err := i.Build(split[1])
return i, err
2018-11-13 13:21:36 +00:00
case "copy":
i := &copy{}
err := i.Build(split[1])
return i, err
2018-11-12 23:29:57 +00:00
case "alias":
i := &alias{}
err := i.Build(split[1])
return i, err
default:
return nil, ErrUnknownInstruction
}
// return nil, nil
}