domain/architecture/system/deployment/launchd/ops.bend
near-v-framework@0.6.0.0 · 76 lines · 0 definitions
1import Base
2import near-v-framework@0.6.0.0/general/markup/type.bend as M
3import near-v-framework@0.6.0.0/general/markup/ops.bend as MO
4import near-v-framework@0.6.0.0/domain/architecture/system/deployment/nix_darwin/type.bend as ND
5import near-v-framework@0.6.0.0/domain/architecture/system/deployment/launchd/type.bend as L
6
7# A launch agent's property list, written as XML through markup.
8
9def key(+k: String) -> M.Node:
10 M.Element{"key", [], [M.Text{k}]}
11
12def str(+s: String) -> M.Node:
13 M.Element{"string", [], [M.Text{s}]}
14
15def strs(xs: List<&2, String>) -> List<&2, M.Node>:
16 match xs:
17 case []:
18 []
19 case x <> rest:
20 str(x) <> strs(rest)
21
22def vars(vs: List<&2, L.Variable>) -> List<&2, M.Node>:
23 match vs:
24 case []:
25 []
26 case L.Variable{k, v} <> rest:
27 key(k) <> str(v) <> vars(rest)
28
29def path(+home: String, +label: String) -> String:
30 home ++ "/Library/LaunchAgents/" ++ label ++ ".plist"
31
32def plist(+home: String, a: L.Agent) -> String:
33 match a:
34 case L.Agent{_, label, command, args, env, +log, _}:
35 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ++
36 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" ++
37 MO.print([M.Element{"plist", [M.Attribute{"version", "1.0"}], [M.Element{"dict", [], [
38 key("Label"), str(label),
39 key("ProgramArguments"), M.Element{"array", [], str(command) <> strs(args)},
40 key("EnvironmentVariables"), M.Element{"dict", [], vars(env)},
41 key("RunAtLoad"), M.Raw{"<true/>"},
42 key("KeepAlive"), M.Raw{"<true/>"},
43 key("StandardOutPath"), str(home ++ "/Library/Logs/" ++ log ++ ".log"),
44 key("StandardErrorPath"), str(home ++ "/Library/Logs/" ++ log ++ ".log")]}]}]) ++ "\n"
45
46def label_of(a: L.Agent) -> String:
47 match a:
48 case L.Agent{_, label, _, _, _, _, _}:
49 label
50
51def programs(ld: L.Launchd) -> List<&2, L.Program>:
52 match ld:
53 case L.Launchd{xs, _, _}:
54 xs
55
56def agents(ld: L.Launchd) -> List<&2, L.Agent>:
57 match ld:
58 case L.Launchd{_, xs, _}:
59 xs
60
61def serves(ld: L.Launchd) -> List<&2, ND.Serve>:
62 match ld:
63 case L.Launchd{_, _, xs}:
64 xs
65
66def port_of(a: L.Agent) -> U32:
67 match a:
68 case L.Agent{_, _, _, _, _, _, p}:
69 p
70
71def ports(xs: List<&2, L.Agent>) -> List<&2, U32>:
72 match xs:
73 case []:
74 []
75 case a <> rest:
76 port_of(a) <> ports(rest)