nix-amer/internal/instruction/alias.go

42 lines
714 B
Go
Raw Normal View History

2018-11-12 23:29:57 +00:00
package instruction
import (
"strings"
)
type alias struct {
raw string
// Name of the alias
Name string
// Value to resolve to
Value string
}
func (d *alias) Raw() string { return strings.Join([]string{"alias", d.raw}, " ") }
func (d *alias) Build(_args string) error {
// 1. extract action fields ()
split := strings.SplitN(_args, " ", 2)
// 2. check syntax
if len(split) != 2 {
return ErrInvalidSyntax
}
if strings.Contains(split[0], "/") {
return ErrInvalidAlias
}
d.Name = strings.Trim(split[0], " \t")
d.Value = strings.Trim(split[1], " \t")
2018-11-12 23:29:57 +00:00
d.raw = _args
return nil
}
func (d alias) Exec(ctx ExecutionContext) ([]byte, error) {
ctx.Alias[d.Name] = d.Value
return nil, nil
}