Skip to content

Commit

Permalink
Add ability to execute commands
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
daenney committed Mar 12, 2024
1 parent d15b5eb commit 62e62d3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
20 changes: 20 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"context"
"os/exec"
"time"
)

// RunCommand runs a command, not a shell, governed by the
// passed in context.
//
// Commands are given 1s to complete.
func RunCommand(ctx context.Context, name string, args ...string) (string, error) {
cctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

cmd := exec.CommandContext(cctx, name, args...)
output, err := cmd.CombinedOutput()
return string(output), err
}
5 changes: 5 additions & 0 deletions examples/scripts/notification.expr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let mixer = "MIXER_ID";
let fader = "A";
let oldState = status.Old.Mixers[mixer].FaderStatus[fader].MuteState;
let newState = status.New.Mixers[mixer].FaderStatus[fader].MuteState;
oldState != newState ? RunCommand("notify-send", "-a", "gopherxlr", "-e", "state changed", "toggle") : nil
20 changes: 17 additions & 3 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,22 @@ func (Env) PlayPause(ctx context.Context, name string) error {
return dbus.ToggleMediaPlayback(ctx, name)
}

func LoadPrograms(dir string) ([]*vm.Program, error) {
res := []*vm.Program{}
func (Env) RunCommand(ctx context.Context, name string, args ...string) error {
_, err := RunCommand(ctx, name, args...)
if err != nil {
return err
}
return nil
}

type Program struct {
file string
prog *vm.Program
}

func LoadPrograms(dir string) ([]Program, error) {
res := []Program{}

err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand All @@ -37,7 +51,7 @@ func LoadPrograms(dir string) ([]*vm.Program, error) {
if err != nil {
return err
}
res = append(res, prog)
res = append(res, Program{file: path, prog: prog})
}
return nil
})
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ Main:
Context: ctx,
}
for _, prog := range programs {
_, err := expr.Run(prog, env)
output, err := expr.Run(prog.prog, env)
if err != nil {
fmt.Println("error: ", err)
fmt.Printf("error executing %s: %s\n", prog.file, err)
}
if output != nil {
fmt.Printf("error from %s: %s\n", prog.file, output)
}
}
}
Expand Down

0 comments on commit 62e62d3

Please sign in to comment.