conditional fetch in json-to-fstree

This commit is contained in:
Daniel Barlow 2024-09-05 10:39:59 +01:00
parent bcf5ab24e8
commit 6a5fed83dd
1 changed files with 18 additions and 12 deletions

View File

@ -4,19 +4,25 @@
(local { : utime } (require :lualinux)) (local { : utime } (require :lualinux))
(fn download [url dest] (fn download [url dest]
(match (http.fetch url) (let [state (.. dest "/state")
(nil code str) previously (ll.lstat state 12)]
(assert nil (.. "error " code ": " str)) (match (http.fetch url "i" previously)
(nil 10 _) ; not modified
(print (.. url " not modified, already up to date"))
(body { : last-modified }) (nil code str)
(let [service (svc.open dest) (assert nil (.. "error " code ": " str))
lock (.. dest "/.lock")
state (.. dest "/state")] (body { : last-modified })
(with-open [fout (io.open lock :w)] (fout:write "")) (let [service (svc.open dest)
(service:output "." (json.decode body)) lock (.. dest "/.lock")]
(with-open [fout (io.open state :w)] (fout:write "ok")) (with-open [fout (io.open lock :w)] (fout:write ""))
(os.remove lock) (service:output "." (json.decode body))
(utime dest last-modified)))) (with-open [fout (io.open state :w)] (fout:write "ok"))
(utime state last-modified)
(os.remove lock))
)))
(fn run [] (download (. arg 1) (. arg 2))) (fn run [] (download (. arg 1) (. arg 2)))