liminix/pkgs/ifwait/ifwait.fnl

52 lines
1.6 KiB
Text
Raw Normal View History

(local nl (require :anoia.nl))
; (local { : view} (require :fennel))
(local { : assoc : system } (require :anoia))
(fn parse-args [args]
(match args
["-v" & rest] (assoc (parse-args rest) :verbose true)
["-t" timeout & rest] (assoc (parse-args rest) :timeout (tonumber timeout))
["-s" service & rest] (assoc (parse-args rest) :service service)
[linkname "up"] {:link linkname :expecting "up"}
[linkname "running"] {:link linkname :expecting "running"}
[linkname "present"] {:link linkname :expecting "present"}
[linkname nil] {:link linkname :expecting "present"}
_ nil))
2024-03-05 00:11:04 +01:00
(fn run-event [params v]
(let [got
(match v
;; - up: Reflects the administrative state of the interface (IFF_UP)
;; - running: Reflects the operational state (IFF_RUNNING).
2024-03-05 00:11:04 +01:00
{:event "newlink" :name params.link :up :yes :running :yes}
{:present true :up true :running true}
2024-03-05 00:11:04 +01:00
{:event "newlink" :name params.link :up :yes}
{:present :true :up true}
2024-03-05 00:11:04 +01:00
{:event "newlink" :name params.link}
{:present true }
_
{})]
2024-03-05 20:43:42 +01:00
(. got params.expecting)))
(fn run [args event-fn]
(let [parameters
(assert (parse-args args)
(.. "Usage: ifwait [-v] ifname [present|up|running]"))]
(when parameters.verbose
(print (.. "ifwait: waiting for "
parameters.link " to be " parameters.expecting)))
2024-03-05 20:43:42 +01:00
(each [e (event-fn)
&until (run-event parameters e)]
true)))
(when (not (= (. arg 0) "test"))
(run arg #(nl.events {:link true})))
{ : run }