From eb79928b37cb796d16e09ca91309d228e98e347b Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 8 Aug 2024 11:37:50 +0100 Subject: [PATCH] anoia.svc allow writing outputs --- pkgs/anoia/Makefile | 12 ++++++++++-- pkgs/anoia/svc.fnl | 19 +++++++++++++++---- pkgs/anoia/test-svc.fnl | 9 +++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pkgs/anoia/Makefile b/pkgs/anoia/Makefile index f68e596..75f9021 100644 --- 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 index c473dc7..9156860 100644 --- 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 index 61729d4..c5cab05 100644 --- 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" + }))