From 870da62a1e970a9beb4cbab4a234c229bc300669 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sat, 9 Sep 2023 00:16:21 +0100 Subject: [PATCH] anoia.svc outputs may be directories (read as table) --- .../example-output/addresses/1/attribute | 1 + .../example-output/addresses/3/attribute | 1 + .../example-output/addresses/5/attribute | 1 + .../example-output/addresses/6/attribute | 1 + pkgs/anoia/example-output/colours/black | 1 + pkgs/anoia/example-output/colours/blue | 1 + pkgs/anoia/example-output/colours/green | 1 + pkgs/anoia/example-output/colours/red | 1 + pkgs/anoia/example-output/name | 1 + pkgs/anoia/fs.fnl | 6 ++++- pkgs/anoia/svc.fnl | 22 +++++++++++++++++-- pkgs/anoia/test-svc.fnl | 17 ++++++++++++++ 12 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 pkgs/anoia/example-output/addresses/1/attribute create mode 100644 pkgs/anoia/example-output/addresses/3/attribute create mode 100644 pkgs/anoia/example-output/addresses/5/attribute create mode 100644 pkgs/anoia/example-output/addresses/6/attribute create mode 100644 pkgs/anoia/example-output/colours/black create mode 100644 pkgs/anoia/example-output/colours/blue create mode 100644 pkgs/anoia/example-output/colours/green create mode 100644 pkgs/anoia/example-output/colours/red create mode 100644 pkgs/anoia/example-output/name create mode 100644 pkgs/anoia/test-svc.fnl diff --git a/pkgs/anoia/example-output/addresses/1/attribute b/pkgs/anoia/example-output/addresses/1/attribute new file mode 100644 index 0000000..b54f8c3 --- /dev/null +++ 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 new file mode 100644 index 0000000..711c1a1 --- /dev/null +++ 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 new file mode 100644 index 0000000..8092345 --- /dev/null +++ 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 new file mode 100644 index 0000000..9139598 --- /dev/null +++ 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 new file mode 100644 index 0000000..c4949eb --- /dev/null +++ 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 new file mode 100644 index 0000000..df785ee --- /dev/null +++ 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 new file mode 100644 index 0000000..9f07fb6 --- /dev/null +++ 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 new file mode 100644 index 0000000..db6f1d8 --- /dev/null +++ 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 new file mode 100644 index 0000000..dbf1b39 --- /dev/null +++ b/pkgs/anoia/example-output/name @@ -0,0 +1 @@ +eth1 diff --git a/pkgs/anoia/fs.fnl b/pkgs/anoia/fs.fnl index f021349..1e3aa03 100644 --- 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 index 002583c..468434e 100644 --- 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 new file mode 100644 index 0000000..89830ee --- /dev/null +++ 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"}})))