forked from DGNum/liminix
anoia.svc outputs may be directories (read as table)
This commit is contained in:
parent
0312f7a999
commit
870da62a1e
12 changed files with 51 additions and 3 deletions
1
pkgs/anoia/example-output/addresses/1/attribute
Normal file
1
pkgs/anoia/example-output/addresses/1/attribute
Normal file
|
@ -0,0 +1 @@
|
|||
a11
|
1
pkgs/anoia/example-output/addresses/3/attribute
Normal file
1
pkgs/anoia/example-output/addresses/3/attribute
Normal file
|
@ -0,0 +1 @@
|
|||
a33
|
1
pkgs/anoia/example-output/addresses/5/attribute
Normal file
1
pkgs/anoia/example-output/addresses/5/attribute
Normal file
|
@ -0,0 +1 @@
|
|||
a55
|
1
pkgs/anoia/example-output/addresses/6/attribute
Normal file
1
pkgs/anoia/example-output/addresses/6/attribute
Normal file
|
@ -0,0 +1 @@
|
|||
a66
|
1
pkgs/anoia/example-output/colours/black
Normal file
1
pkgs/anoia/example-output/colours/black
Normal file
|
@ -0,0 +1 @@
|
|||
000000
|
1
pkgs/anoia/example-output/colours/blue
Normal file
1
pkgs/anoia/example-output/colours/blue
Normal file
|
@ -0,0 +1 @@
|
|||
0000ff
|
1
pkgs/anoia/example-output/colours/green
Normal file
1
pkgs/anoia/example-output/colours/green
Normal file
|
@ -0,0 +1 @@
|
|||
00ff00
|
1
pkgs/anoia/example-output/colours/red
Normal file
1
pkgs/anoia/example-output/colours/red
Normal file
|
@ -0,0 +1 @@
|
|||
ff0000
|
1
pkgs/anoia/example-output/name
Normal file
1
pkgs/anoia/example-output/name
Normal file
|
@ -0,0 +1 @@
|
|||
eth1
|
|
@ -29,4 +29,8 @@
|
|||
(error (.. "can't remove " pathname " of kind \"" unknown.mode "\""))))
|
||||
|
||||
|
||||
{ : mktree : rmtree }
|
||||
{
|
||||
: mktree
|
||||
: rmtree
|
||||
: directory?
|
||||
}
|
||||
|
|
|
@ -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
17
pkgs/anoia/test-svc.fnl
Normal 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"}})))
|
Loading…
Reference in a new issue