From 492835a67ac68cd2805300d8d88b715febc2a797 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 13 Nov 2018 00:29:57 +0100 Subject: [PATCH] create alias instruction --- internal/instruction/alias.go | 37 ++++++++++++++++++++++++++++++++++ internal/instruction/reader.go | 4 ++++ 2 files changed, 41 insertions(+) create mode 100644 internal/instruction/alias.go diff --git a/internal/instruction/alias.go b/internal/instruction/alias.go new file mode 100644 index 0000000..0e3706c --- /dev/null +++ b/internal/instruction/alias.go @@ -0,0 +1,37 @@ +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 + } + + d.Name = split[0] + d.Value = split[1] + d.raw = _args + + return nil +} + +func (d alias) Exec(ctx ExecutionContext) ([]byte, error) { + ctx.Alias[d.Name] = d.Value + return nil, nil +} diff --git a/internal/instruction/reader.go b/internal/instruction/reader.go index 958ca1a..ad059d0 100644 --- a/internal/instruction/reader.go +++ b/internal/instruction/reader.go @@ -40,6 +40,10 @@ func Parse(raw string) (T, error) { i := &set{} err := i.Build(split[1]) return i, err + case "alias": + i := &alias{} + err := i.Build(split[1]) + return i, err default: return nil, ErrUnknownInstruction }