nix-amer/internal/instruction/run.go

35 lines
612 B
Go

package instruction
import (
"fmt"
"os"
"strings"
)
type run struct {
raw string
}
func (d *run) Raw() string { return strings.Join([]string{"run", d.raw}, " ") }
func (d *run) Build(_args string) error {
d.raw = _args
return nil
}
func (d run) Exec(ctx ExecutionContext) ([]byte, error) {
// 1. fail if file not found
if _, err := os.Stat(d.raw); os.IsNotExist(err) {
return nil, fmt.Errorf("cannot find script '%s'", d.raw)
}
// 2. execute script
if err := ctx.Executor.Command(d.raw).Run(); err != nil {
return nil, fmt.Errorf("cannot run '%s' | %s", d.raw, err)
}
return nil, nil
}