anoia.svc outputs may be directories (read as table)

This commit is contained in:
Daniel Barlow 2023-09-09 00:16:21 +01:00
parent 0312f7a999
commit 870da62a1e
12 changed files with 51 additions and 3 deletions

View file

@ -0,0 +1 @@
a11

View file

@ -0,0 +1 @@
a33

View file

@ -0,0 +1 @@
a55

View file

@ -0,0 +1 @@
a66

View file

@ -0,0 +1 @@
000000

View file

@ -0,0 +1 @@
0000ff

View file

@ -0,0 +1 @@
00ff00

View file

@ -0,0 +1 @@
ff0000

View file

@ -0,0 +1 @@
eth1

View file

@ -29,4 +29,8 @@
(error (.. "can't remove " pathname " of kind \"" unknown.mode "\""))))
{ : mktree : rmtree }
{
: mktree
: rmtree
: directory?
}

View file

@ -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)
}))

17
pkgs/anoia/test-svc.fnl Normal file
View file

@ -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"}})))