Download multiple files from custom repo:tag
authorOlav Bakke Svendsen <mail@olavbs.no>
Tue, 17 Oct 2023 21:48:18 +0000 (23:48 +0200)
committerOlav Bakke Svendsen <mail@olavbs.no>
Tue, 17 Oct 2023 21:48:18 +0000 (23:48 +0200)
common/dl.lua

index 53d53597549f2b437a0544cc56c2e616dbce7292..5eb2e8169e218ad6c091f6f3375a0d8e8486dc09 100644 (file)
@@ -1,29 +1,35 @@
 -- dl.lua
 -- download from git repo
 -- usage:
--- dl path/to/file.lua             - saves to file.lua
--- dl path/to/file.lua -o          - overwrites file.lua
+-- dl repo[:tag] remote/path:local/path[:o] [remote/path:local/path[:o] ...]
 
-local from, overwrite_flag = table.unpack({...})
-local overwrite = overwrite_flag == "-o"
-
-local download = function(f,t)
-  return shell.run("wget "..f.." "..t)
+local args = table.unpack({...})
+local repo = ""
+local tag = ""
+local files = {}
+do
+  local repotag = table.remove(args, 1)
+  repo, tag = string.match(args[1], "^([^:]+):?(.*)$")
+  if tag == "" then tag = "HEAD" end
+  for _, file in pairs(args) do
+    local from, rest = string.match(file, "^/?([^:%s]+):?(.*)$")
+    local to, rest = string.match(rest, "^([^:%s]+):?(.*)$")
+    local overwrite = string.match(rest, "^(o)$") and true or false
+    table.insert(files, 1, { from = from, to = to, overwrite = overwrite })
+  end
 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 overwrite then
+for _, file in pairs(files) do
+  local url = "https://git.olavbs.no/?p="..repo..".git;a=blob_plain;f="..from..";hb="..tag
+  if fs.exists(file.to) and file.overwrite then
     local tmp = textutils.serialize(math.random(100000,999999))
-    if download(url,tmp) then
-      print("Overwriting "..to)
-      fs.delete(to)
-      fs.move(tmp,to)
+    local ok = shell.run("wget "..file.from.." "..tmp)
+    if ok then
+      print("Overwriting "..file.to)
+      fs.delete(file.to)
+      fs.move(tmp,file.to)
     end
   else
-    download(url,to)
+    shell.run("wget "..file.from.." "..file.to)
   end
 end