Advertisement
Ocawesome101

github.lua

Mar 5th, 2020
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. -- Downloads GitHub repositories to your OpenComputers computer. --
  2. --[[MIT License
  3.  
  4. (c) 2020 Ocawesome101
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7.  
  8. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  9.  
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11. ]]
  12.  
  13. local internet = require("component").internet or require("internet")
  14. local shell = shell or require("shell")
  15. local fs = fs or require("filesystem")
  16.  
  17. if not internet then
  18.   return print("This program requires an internet card.")
  19. end
  20.  
  21. local args, options = shell.parse(...) -- Open Kernel has argument parsing now! :D
  22.  
  23. if #args < 1 or options.help then
  24.   print("Usage: github <user/repo> <branch> [destination]")
  25.   print("  user/repo: The username and repository; for example, ocawesome101/open-kernel-2")
  26.   print("  branch:    The desired repository branch. Defaults to master")
  27.   return
  28. end
  29.  
  30. local repo = args[1]
  31. local branch = args[2] or "master"
  32. local inst = (args[3] and shell.resolve(args[3])) or fs.name(repo)
  33.  
  34. fs.makeDirectory(package.libdir or "/usr/lib")
  35. fs.makeDirectory(inst)
  36.  
  37. local function download(url, dest)
  38.   checkArg(1, url, "string")
  39.   checkArg(2, dest, "string")
  40.   local handle, err
  41.   if internet.request then
  42.     handle, err = internet.request(url)
  43.   else
  44.     handle, err = internet.get(url)
  45.   end
  46.   if not handle then
  47.     return print("github: WARNING: failed to download " .. url .. ": " .. err)
  48.   end
  49.   local fileData = ""
  50.   repeat
  51.     local chunk = handle.read(math.huge)
  52.     fileData = fileData .. (chunk or "")
  53.   until not chunk
  54.   handle.close()
  55.   local handle = io.open(dest, "w")
  56.   handle:write(fileData)
  57.   handle:close()
  58. end
  59.  
  60. if not fs.exists((package.libdir or "/usr/lib") .. "/json.lua") then
  61.   print("github: JSON library not found. Downloading....")
  62.   download("https://raw.githubusercontent.com/rxi/json.lua/master/json.lua", (package.libdir or "/usr/lib") .. "/json.lua")
  63. end
  64.  
  65. local json, err = require("json")
  66. if not json then
  67.   return print(err)
  68. end
  69.  
  70. print("github: getting repository data")
  71. local api, err
  72. if internet.request then
  73.   api, err = internet.request("https://api.github.com/repos/" .. repo .. "/git/trees/" .. branch .. "?recursive=1")
  74. else
  75.   api, err = internet.get("https://api.github.com/repos/" .. repo .. "/git/trees/" .. branch .. "?recursive=1")
  76. end
  77. if not api then
  78.   return print(err)
  79. end
  80.  
  81. local repoData = ""
  82. repeat
  83.   local chunk = api.read(math.huge)
  84.   repoData = repoData .. (chunk or "")
  85. until not chunk
  86. api.close()
  87.  
  88. print("github: parsing repository data")
  89. repoData = json.decode(repoData)
  90.  
  91. print("github: making directories")
  92. for _,v in pairs(repoData.tree) do
  93.   if v.type == "tree" then
  94.     fs.makeDirectory(inst .. "/" .. v.path)
  95.   end
  96. end
  97.  
  98. print("github: downloading files from repository")
  99. for _,v in pairs(repoData.tree) do
  100.   if v.type == "blob" then
  101.     local url = "https://raw.githubusercontent.com/" .. repo .. "/" .. branch .. "/" .. v.path
  102.     download(url, inst .. "/" .. v.path)
  103.   end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement