commit 870da62a1e970a9beb4cbab4a234c229bc300669
parent 0312f7a99905e9f46093d55105c37e56cb7624e5
Author: Daniel Barlow <dan@telent.net>
Date: Sat, 9 Sep 2023 00:16:21 +0100
anoia.svc outputs may be directories (read as table)
Diffstat:
12 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/pkgs/anoia/example-output/addresses/1/attribute b/pkgs/anoia/example-output/addresses/1/attribute
@@ -0,0 +1 @@
+a11
diff --git a/pkgs/anoia/example-output/addresses/3/attribute b/pkgs/anoia/example-output/addresses/3/attribute
@@ -0,0 +1 @@
+a33
diff --git a/pkgs/anoia/example-output/addresses/5/attribute b/pkgs/anoia/example-output/addresses/5/attribute
@@ -0,0 +1 @@
+a55
diff --git a/pkgs/anoia/example-output/addresses/6/attribute b/pkgs/anoia/example-output/addresses/6/attribute
@@ -0,0 +1 @@
+a66
diff --git a/pkgs/anoia/example-output/colours/black b/pkgs/anoia/example-output/colours/black
@@ -0,0 +1 @@
+000000
diff --git a/pkgs/anoia/example-output/colours/blue b/pkgs/anoia/example-output/colours/blue
@@ -0,0 +1 @@
+0000ff
diff --git a/pkgs/anoia/example-output/colours/green b/pkgs/anoia/example-output/colours/green
@@ -0,0 +1 @@
+00ff00
diff --git a/pkgs/anoia/example-output/colours/red b/pkgs/anoia/example-output/colours/red
@@ -0,0 +1 @@
+ff0000
diff --git a/pkgs/anoia/example-output/name b/pkgs/anoia/example-output/name
@@ -0,0 +1 @@
+eth1
diff --git a/pkgs/anoia/fs.fnl b/pkgs/anoia/fs.fnl
@@ -29,4 +29,8 @@
(error (.. "can't remove " pathname " of kind \"" unknown.mode "\""))))
-{ : mktree : rmtree }
+{
+ : mktree
+ : rmtree
+ : directory?
+ }
diff --git a/pkgs/anoia/svc.fnl b/pkgs/anoia/svc.fnl
@@ -1,5 +1,7 @@
(local inotify (require :inotify))
(local { : file-exists? } (require :anoia))
+(local { : directory? } (require :anoia.fs))
+(local lfs (require :lfs))
(fn read-line [name]
(with-open [f (assert (io.open name :r) (.. "can't open file " name))]
@@ -17,14 +19,30 @@
inotify.IN_CLOSE_WRITE)
handle))
+(fn read-value [pathname]
+ (case (lfs.symlinkattributes pathname)
+ nil nil
+ {:mode "directory"}
+ (collect [f (lfs.dir pathname)]
+ (when (not (or (= f ".") (= f "..")))
+ (values f (read-value ( .. pathname "/" f)))))
+ {:mode "file"}
+ (read-line pathname)
+ {:mode "link"}
+ (read-line pathname)
+ unknown
+ (error (.. "can't read " pathname " of kind \"" unknown.mode "\""))))
+
+
(fn open [directory]
(let [watcher (watch-fsevents directory)
has-file? (fn [filename] (file-exists? (.. directory "/" filename)))]
{
- :wait (fn [] (watcher:read))
+ :wait #(watcher:read)
:ready? (fn [self]
(and (has-file? "state") (not (has-file? ".lock"))))
- :output (fn [_ filename] (read-line (.. directory "/" filename)))
+ :output (fn [_ filename]
+ (read-value (.. directory "/" filename)))
:close #(watcher:close)
}))
diff --git a/pkgs/anoia/test-svc.fnl b/pkgs/anoia/test-svc.fnl
@@ -0,0 +1,17 @@
+(local svc (require :anoia.svc))
+(local { : view } (require :fennel))
+
+(local ex (svc.open "./example-output"))
+
+(assert (= (ex:output "name") "eth1"))
+
+(assert (=
+ (table.concat (ex:output "colours"))
+ (table.concat { :red "ff0000" :green "00ff00" :blu "0000ff" :black "000000" })))
+
+(assert (=
+ (table.concat (ex:output "addresses"))
+ (table.concat {:1 {:attribute "a11"}
+ :3 {:attribute "a33"}
+ :5 {:attribute "a55"}
+ :6 {:attribute "a66"}})))