Skip to content

Commit 62e62d3

Browse files
committed
Add ability to execute commands
Using RunCommand you can execute any binary on a system with whatever arguments. Commands are constrained to running for up to 1sec. Commands will be looked up in path, unless an absolute path is given. This does not execute a shell and so cannot directly execute shell scripts. For that, you can call it with `sh -c`.
1 parent d15b5eb commit 62e62d3

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

command.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"os/exec"
6+
"time"
7+
)
8+
9+
// RunCommand runs a command, not a shell, governed by the
10+
// passed in context.
11+
//
12+
// Commands are given 1s to complete.
13+
func RunCommand(ctx context.Context, name string, args ...string) (string, error) {
14+
cctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
15+
defer cancel()
16+
17+
cmd := exec.CommandContext(cctx, name, args...)
18+
output, err := cmd.CombinedOutput()
19+
return string(output), err
20+
}

examples/scripts/notification.expr

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let mixer = "MIXER_ID";
2+
let fader = "A";
3+
let oldState = status.Old.Mixers[mixer].FaderStatus[fader].MuteState;
4+
let newState = status.New.Mixers[mixer].FaderStatus[fader].MuteState;
5+
oldState != newState ? RunCommand("notify-send", "-a", "gopherxlr", "-e", "state changed", "toggle") : nil

expr.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,22 @@ func (Env) PlayPause(ctx context.Context, name string) error {
2222
return dbus.ToggleMediaPlayback(ctx, name)
2323
}
2424

25-
func LoadPrograms(dir string) ([]*vm.Program, error) {
26-
res := []*vm.Program{}
25+
func (Env) RunCommand(ctx context.Context, name string, args ...string) error {
26+
_, err := RunCommand(ctx, name, args...)
27+
if err != nil {
28+
return err
29+
}
30+
return nil
31+
}
32+
33+
type Program struct {
34+
file string
35+
prog *vm.Program
36+
}
37+
38+
func LoadPrograms(dir string) ([]Program, error) {
39+
res := []Program{}
40+
2741
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
2842
if err != nil {
2943
return err
@@ -37,7 +51,7 @@ func LoadPrograms(dir string) ([]*vm.Program, error) {
3751
if err != nil {
3852
return err
3953
}
40-
res = append(res, prog)
54+
res = append(res, Program{file: path, prog: prog})
4155
}
4256
return nil
4357
})

main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ Main:
7171
Context: ctx,
7272
}
7373
for _, prog := range programs {
74-
_, err := expr.Run(prog, env)
74+
output, err := expr.Run(prog.prog, env)
7575
if err != nil {
76-
fmt.Println("error: ", err)
76+
fmt.Printf("error executing %s: %s\n", prog.file, err)
77+
}
78+
if output != nil {
79+
fmt.Printf("error from %s: %s\n", prog.file, output)
7780
}
7881
}
7982
}

0 commit comments

Comments
 (0)