From 6a5fed83dd375531eb619778ad087c4aff53acec Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 5 Sep 2024 10:39:59 +0100 Subject: [PATCH] conditional fetch in json-to-fstree --- pkgs/json-to-fstree/json-to-fstree.fnl | 30 +++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pkgs/json-to-fstree/json-to-fstree.fnl b/pkgs/json-to-fstree/json-to-fstree.fnl index 4f578c1..d9f06a4 100644 --- a/pkgs/json-to-fstree/json-to-fstree.fnl +++ b/pkgs/json-to-fstree/json-to-fstree.fnl @@ -4,19 +4,25 @@ (local { : utime } (require :lualinux)) (fn download [url dest] - (match (http.fetch url) - (nil code str) - (assert nil (.. "error " code ": " str)) + (let [state (.. dest "/state") + previously (ll.lstat state 12)] + (match (http.fetch url "i" previously) + (nil 10 _) ; not modified + (print (.. url " not modified, already up to date")) - (body { : last-modified }) - (let [service (svc.open dest) - lock (.. dest "/.lock") - state (.. dest "/state")] - (with-open [fout (io.open lock :w)] (fout:write "")) - (service:output "." (json.decode body)) - (with-open [fout (io.open state :w)] (fout:write "ok")) - (os.remove lock) - (utime dest last-modified)))) + (nil code str) + (assert nil (.. "error " code ": " str)) + + (body { : last-modified }) + (let [service (svc.open dest) + lock (.. dest "/.lock")] + (with-open [fout (io.open lock :w)] (fout:write "")) + (service:output "." (json.decode body)) + (with-open [fout (io.open state :w)] (fout:write "ok")) + (utime state last-modified) + (os.remove lock)) + + ))) (fn run [] (download (. arg 1) (. arg 2)))