-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
61 lines (53 loc) · 1.17 KB
/
expr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"io/fs"
"os"
"path/filepath"
"code.dny.dev/gopherxlr/dbus"
"code.dny.dev/gopherxlr/websocket"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
)
type Env struct {
StatusChange websocket.StatusChange `expr:"status"`
Context context.Context `expr:"ctx"`
}
func (Env) PlayPause(ctx context.Context, name string) error {
return dbus.ToggleMediaPlayback(ctx, name)
}
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
}
if !d.IsDir() {
src, err := os.ReadFile(path)
if err != nil {
return err
}
prog, err := expr.Compile(string(src), expr.Env(Env{}), expr.WithContext("ctx"))
if err != nil {
return err
}
res = append(res, Program{file: path, prog: prog})
}
return nil
})
if err != nil {
return nil, err
}
return res, nil
}