nix-amer/internal/instruction/run.go

43 lines
746 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. get file / alias
path := d.raw
if !strings.Contains(path, "/") {
if p, exists := ctx.Alias[path]; exists {
path = p
}
}
// 1. fail if file not found
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, fmt.Errorf("cannot find script '%s'", path)
}
// 2. execute script
if err := ctx.Executor.Command(path).Run(); err != nil {
return nil, fmt.Errorf("cannot run '%s' | %s", path, err)
}
return nil, nil
}