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)
|
2018-11-12 21:54:37 +00:00
|
|
|
split := strings.SplitN(raw, " ", 2)
|
|
|
|
if len(split) < 2 { // at least 1 command + arguments
|
2018-11-08 12:41:07 +00:00
|
|
|
return nil, ErrInvalidSyntax
|
|
|
|
}
|
2018-11-12 21:54:37 +00:00
|
|
|
|
|
|
|
split[0] = strings.Trim(split[0], " \t")
|
|
|
|
|
|
|
|
// 2. fail if invalid base syntax 'cmd args...'
|
|
|
|
split[1] = strings.Trim(split[1], " \t")
|
2018-11-08 12:41:07 +00:00
|
|
|
|
|
|
|
// 3. Extract instruction type
|
2018-11-12 21:54:37 +00:00
|
|
|
switch split[0] {
|
|
|
|
case "install":
|
2018-11-08 12:41:07 +00:00
|
|
|
i := &install{}
|
2018-11-12 21:54:37 +00:00
|
|
|
err := i.Build(split[1])
|
2018-11-08 12:41:07 +00:00
|
|
|
return i, err
|
2018-11-12 21:54:37 +00:00
|
|
|
case "delete":
|
2018-11-08 12:41:07 +00:00
|
|
|
i := &delete{}
|
2018-11-12 21:54:37 +00:00
|
|
|
err := i.Build(split[1])
|
2018-11-08 12:41:07 +00:00
|
|
|
return i, err
|
2018-11-12 21:54:37 +00:00
|
|
|
case "service":
|
2018-11-08 12:41:07 +00:00
|
|
|
i := &service{}
|
2018-11-12 21:54:37 +00:00
|
|
|
err := i.Build(split[1])
|
2018-11-08 12:41:07 +00:00
|
|
|
return i, err
|
2018-11-12 22:38:37 +00:00
|
|
|
case "run":
|
|
|
|
i := &run{}
|
|
|
|
err := i.Build(split[1])
|
|
|
|
return i, err
|
2018-11-12 21:54:37 +00:00
|
|
|
case "set":
|
|
|
|
i := &set{}
|
|
|
|
err := i.Build(split[1])
|
2018-11-08 12:41:07 +00:00
|
|
|
return i, err
|
2018-11-12 23:29:57 +00:00
|
|
|
case "alias":
|
|
|
|
i := &alias{}
|
|
|
|
err := i.Build(split[1])
|
|
|
|
return i, err
|
2018-11-08 12:41:07 +00:00
|
|
|
default:
|
|
|
|
return nil, ErrUnknownInstruction
|
|
|
|
}
|
|
|
|
|
|
|
|
// return nil, nil
|
|
|
|
}
|