From 48600584a0e1e04d58ab3e6b5eb034beef0b34dd Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 13 Nov 2018 14:21:36 +0100 Subject: [PATCH] add copy command (instruction) --- internal/instruction/copy.go | 47 ++++++++++++++++++++++++++++++++++ internal/instruction/reader.go | 4 +++ 2 files changed, 51 insertions(+) create mode 100644 internal/instruction/copy.go diff --git a/internal/instruction/copy.go b/internal/instruction/copy.go new file mode 100644 index 0000000..2cfc52b --- /dev/null +++ b/internal/instruction/copy.go @@ -0,0 +1,47 @@ +package instruction + +import ( + "fmt" + "os" + "strings" +) + +type copy struct { + raw string + Src string + Dst string +} + +func (d *copy) Raw() string { return strings.Join([]string{"copy", d.raw}, " ") } + +func (d *copy) Build(_args string) error { + + // 1. extract action (sub command) + split := strings.Split(_args, " ") + + // 2. check syntax + if len(split) != 2 { + return ErrInvalidSyntax + } + + d.Src = strings.Trim(split[0], " \t") + d.Dst = strings.Trim(split[1], " \t") + d.raw = _args + return nil +} + +func (d copy) Exec(ctx ExecutionContext) ([]byte, error) { + + // 1. fail if source file not found + if _, err := os.Stat(d.Src); os.IsNotExist(err) { + return nil, fmt.Errorf("cannot find script '%s'", d.Src) + } + + // 2. execute script + if err := ctx.Executor.Command("cp", d.Src, d.Dst).Run(); err != nil { + return nil, fmt.Errorf("cannot copy '%s' to '%s' | %s", d.Src, d.Dst, err) + } + + return nil, nil + +} diff --git a/internal/instruction/reader.go b/internal/instruction/reader.go index ad059d0..14b6475 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 "copy": + i := ©{} + err := i.Build(split[1]) + return i, err case "alias": i := &alias{} err := i.Build(split[1])