ValveCantCount

github

Nov 8th, 2020 (edited)
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. local args = { ... }
  2. os.loadAPI("json")
  3.  
  4. local user = ""
  5. local repo = ""
  6. local dest = ""
  7.  
  8. if #args == 2 then
  9. user = args[1]
  10. repo = args[2]
  11. dest = repo
  12. elseif #args == 3 then
  13. user = args[1]
  14. repo = args[2]
  15. dest = args[3]
  16. elseif #args > 3 then
  17. user = args[1]
  18. repo = args[2]
  19. dest = ""
  20. for i = 3, #args, 1 do
  21. dest = dest.." "..args[i]
  22. end
  23. else
  24. print("Usage:\ngithub <username> <repositoryName> [target directory]")
  25. return
  26. end
  27.  
  28. if string.sub(dest,-1) == "/" then
  29. dest = string.sub(dest,0,string.len(dest)-1)
  30. end
  31.  
  32. local allFiles = { }
  33.  
  34. function getRepo(url)
  35. local rs = http.get(url).readAll()
  36. local res = json.decode(rs)
  37. local files = { }
  38. for i,j in pairs(res) do
  39. local data = { }
  40.  
  41. if j.type == "file" then
  42. print("downloading "..j.download_url)
  43. data = http.get(j.download_url).readAll()
  44. elseif j.type == "dir" then
  45. data = getRepo(j.url)
  46. end
  47.  
  48. local entry = {
  49. type = j.type,
  50. name = j.name,
  51. contents = data
  52. }
  53. table.insert(files,entry)
  54.  
  55. end
  56. return files
  57. end
  58.  
  59. function createFiles(files, target)
  60.  
  61. if fs.exists(target) and #fs.list(target) > 0 then
  62. print("Please select an empty target directory.")
  63. return
  64. else
  65. fs.makeDir(target)
  66. end
  67.  
  68. for i,j in pairs(files) do
  69. if j.type == "file" then
  70. local file = fs.open(target.."/"..j.name,"w")
  71. file.write(j.contents)
  72. file.flush()
  73. file.close()
  74. elseif j.type == "dir" then
  75. createFiles(j.contents, target.."/"..j.name)
  76. end
  77. end
  78. end
  79.  
  80. allFiles = getRepo("https://api.github.com/repos/"..user.."/"..repo.."/contents")
  81. print("Saving...")
  82. createFiles(allFiles, dest)
Add Comment
Please, Sign In to add comment