Added override flag on dl.lua
authorOlav Bakke Svendsen <mail@olavbs.no>
Tue, 10 Oct 2023 11:00:37 +0000 (13:00 +0200)
committerOlav Bakke Svendsen <mail@olavbs.no>
Tue, 10 Oct 2023 11:00:37 +0000 (13:00 +0200)
common/dl.lua

index 718fdb0d8e9d34fa4efc58a31c9baca783172f0a..be38e7e21239abf5c570758d339e8d15e063b290 100644 (file)
@@ -1,15 +1,28 @@
 -- dl.lua
 -- download from git repo
 -- usage:
--- dl path/to/file.lua    - saves to file.lua
+-- dl path/to/file.lua             - saves to file.lua
+-- dl path/to/file.lua -o          - overwrites file.lua
 
-local from = {...}
-if from[1] then
-  local url = "https://git.olavbs.no/?p=cc.git;a=blob_plain;f="..from[1]..";hb=HEAD"
-  local to = string.match(from[1], "([^/]-)$")
-  if not shell.run("wget "..url.." "..to) then
-    print("Couldn't download "..from[1])
-  end
-else
+local from, override_flag = table.unpack({...})
+local override = override_flag == "-o"
+
+local download = function(f,t)
+  return shell.run("wget "..f.." "..t)
+end
+
+if not from then
   print("Nothing to download")
+else
+  local url = "https://git.olavbs.no/?p=cc.git;a=blob_plain;f="..from..";hb=HEAD"
+  local to = string.match(from, "([^/]-)$")
+  if fs.exists(to) and override then
+    local tmp = textutils.serialize(math.random(100000,999999))
+    if download(url,tmp) then
+      fs.delete(to)
+      fs.move(tmp,to)
+    end
+  else
+    download(url,to)
+  end
 end