commit eb79928b37cb796d16e09ca91309d228e98e347b
parent 0a629df48d1ea6873937a2b10432497c1fbaa24a
Author: Daniel Barlow <dan@telent.net>
Date: Thu, 8 Aug 2024 11:37:50 +0100
anoia.svc allow writing outputs
Diffstat:
3 files changed, 34 insertions(+), 6 deletions(-)
diff --git a/pkgs/anoia/Makefile b/pkgs/anoia/Makefile
@@ -1,10 +1,18 @@
+servicedir:=$(shell mktemp -d)
default: fs.lua init.lua nl.lua svc.lua net/constants.lua
-test:
+check:
ln -s . anoia
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
$(CC) -imacros sys/socket.h -imacros linux/netlink.h -E -P - < net/constants.c | sed 's/ *$$//g' | cat -s > net/constants.lua
diff --git a/pkgs/anoia/svc.fnl b/pkgs/anoia/svc.fnl
@@ -1,6 +1,6 @@
(local inotify (require :inotify))
-(local { : file-exists? } (require :anoia))
-(local { : file-type : dir &as fs } (require :anoia.fs))
+(local { : file-exists? : dirname } (require :anoia))
+(local { : file-type : dir : mktree &as fs } (require :anoia.fs))
(fn read-line [name]
(with-open [f (assert (io.open name :r) (.. "can't open file " name))]
@@ -18,6 +18,15 @@
inotify.IN_CLOSE_WRITE)
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]
(case (file-type pathname)
nil nil
@@ -46,8 +55,10 @@
:wait #(watcher:read)
:ready? (fn [self]
(and (has-file? "state") (not (has-file? ".lock"))))
- :output (fn [_ filename]
- (read-value (.. directory "/" filename)))
+ :output (fn [_ filename new-value]
+ (if new-value
+ (write-value (.. directory "/" filename) new-value)
+ (read-value (.. directory "/" filename))))
:close #(watcher:close)
: events
}))
diff --git a/pkgs/anoia/test-svc.fnl b/pkgs/anoia/test-svc.fnl
@@ -15,3 +15,12 @@
:3 {:attribute "a33"}
:5 {:attribute "a55"}
: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"
+ }))