nix-amer/internal/instruction/run.go

61 lines
1.1 KiB
Go
Raw Permalink Normal View History

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
}
func (d run) DryRun(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)
}
return nil, nil
}