anoia.svc allow writing outputs

This commit is contained in:
Daniel Barlow 2024-08-08 11:37:50 +01:00
parent 0a629df48d
commit eb79928b37
3 changed files with 34 additions and 6 deletions

View File

@ -1,10 +1,18 @@
servicedir:=$(shell mktemp -d)
default: fs.lua init.lua nl.lua svc.lua net/constants.lua default: fs.lua init.lua nl.lua svc.lua net/constants.lua
test: check:
ln -s . anoia ln -s . anoia
fennel test.fnl fennel test.fnl
fennel test-svc.fnl fennel test-svc.fnl $(servicedir)
test -f $(servicedir)/fish
test "`cat $(servicedir)/fish`" = "food"
test -d $(servicedir)/nested/path
test "`cat $(servicedir)/nested/path/name`" = "value"
test "`cat $(servicedir)/nested/path/complex/attribute`" = "val"
test "`cat $(servicedir)/nested/path/complex/other`" = "42"
net/constants.lua: net/constants.c net/constants.lua: net/constants.c
$(CC) -imacros sys/socket.h -imacros linux/netlink.h -E -P - < net/constants.c | sed 's/ *$$//g' | cat -s > net/constants.lua $(CC) -imacros sys/socket.h -imacros linux/netlink.h -E -P - < net/constants.c | sed 's/ *$$//g' | cat -s > net/constants.lua

View File

@ -1,6 +1,6 @@
(local inotify (require :inotify)) (local inotify (require :inotify))
(local { : file-exists? } (require :anoia)) (local { : file-exists? : dirname } (require :anoia))
(local { : file-type : dir &as fs } (require :anoia.fs)) (local { : file-type : dir : mktree &as fs } (require :anoia.fs))
(fn read-line [name] (fn read-line [name]
(with-open [f (assert (io.open name :r) (.. "can't open file " name))] (with-open [f (assert (io.open name :r) (.. "can't open file " name))]
@ -18,6 +18,15 @@
inotify.IN_CLOSE_WRITE) inotify.IN_CLOSE_WRITE)
handle)) handle))
(fn write-value [pathname value]
(mktree (dirname pathname))
(match (type value)
"string"
(with-open [f (assert (io.open pathname :w) (.. "can't open " pathname))]
(f:write value))
"table" (each [k v (pairs value)]
(write-value (.. pathname "/" k) v))))
(fn read-value [pathname] (fn read-value [pathname]
(case (file-type pathname) (case (file-type pathname)
nil nil nil nil
@ -46,8 +55,10 @@
:wait #(watcher:read) :wait #(watcher:read)
:ready? (fn [self] :ready? (fn [self]
(and (has-file? "state") (not (has-file? ".lock")))) (and (has-file? "state") (not (has-file? ".lock"))))
:output (fn [_ filename] :output (fn [_ filename new-value]
(read-value (.. directory "/" filename))) (if new-value
(write-value (.. directory "/" filename) new-value)
(read-value (.. directory "/" filename))))
:close #(watcher:close) :close #(watcher:close)
: events : events
})) }))

View File

@ -15,3 +15,12 @@
:3 {:attribute "a33"} :3 {:attribute "a33"}
:5 {:attribute "a55"} :5 {:attribute "a55"}
:6 {:attribute "a66"}}))) :6 {:attribute "a66"}})))
(let [dir (. arg 1)
ex2 (svc.open dir)]
(ex2:output "fish" "food")
(ex2:output "nested/path/name" "value")
(ex2:output "nested/path/complex" {
:attribute "val"
:other "42"
}))