Advertisement
Guest User

github.lua

a guest
Jan 26th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. --edit of the pastebin program to get stuff from Github, edited by Vexatos
  2. local term = require("term")
  3. local shell = require("shell")
  4. local fs = require("filesystem")
  5. local component = require("component")
  6. local internet = require("internet")
  7.  
  8. local tArgs, options = shell.parse(...)
  9.  
  10. local function printUsage()
  11.     print( "Usages:" )
  12.     print( "github get username/reponame/branch/path <filename>" )
  13.     print( "github run username/reponame/branch/path <arguments>" )
  14.     print( "'github help' for more information" )
  15. end
  16.  
  17. if not component.isAvailable("internet") then
  18.   print( "Error: Github requires an Internet Card to run" )
  19.   return
  20. end
  21.  
  22. local function get(url, sPathN, sFile)
  23.   term.write( "Connecting to github.com... " )
  24.   local sResponse = ""
  25.   local file, reason = io.open(sPathN, "w")
  26.   if not file then
  27.     print("failed opening file for writing: " .. reason)
  28.     return
  29.   end
  30.   local result, response = pcall( internet.request, url)
  31.  
  32.   if result then
  33.     print( "Success." )
  34.     for chunk in response do
  35.       file:write( chunk )
  36.     end
  37.       file:close()
  38.       print( "Downloaded as "..sFile )
  39.     return sResponse
  40.   else
  41.     file:close()
  42.     print( "Failed: "..response )
  43.   end
  44. end
  45.  
  46. local function run(url, sArg)
  47. local tmpFile = "/tmp/tmp_github.lua"
  48.     get(url, shell.resolve(tmpFile), "tmp_github.lua")
  49.     term.clear()
  50.     print("Running...")
  51. local success, msg = shell.execute(tmpFile, _ENV, table.unpack(tArgs, sArg))
  52.     if not success then
  53.       print( msg )
  54.     end
  55.     fs.remove(tmpFile)
  56.  
  57. end
  58.  
  59. local sCommand = tArgs[1]
  60. if sCommand == "get" then
  61.     -- Download a file from github.com
  62.     if #tArgs < 6 then
  63.       if #tArgs == 3 then
  64.  
  65.         -- Determine file to download
  66.         local sFile = tArgs[3]
  67.         local sPathN = shell.resolve( sFile )
  68.         local url = "https://raw.github.com/"..tArgs[2]
  69.         if fs.exists( sPathN ) then
  70.           if not options.f then
  71.             print("file already exists")
  72.             return
  73.           end
  74.         end
  75.  
  76.         -- GET the contents from github
  77.         get(url, sPathN, sFile)
  78.       else
  79.         printUsage()
  80.         return
  81.       end
  82.     elseif #tArgs == 6 then
  83.       -- Determine file to download
  84.       local sFile = tArgs[6]
  85.       local sPathN = shell.resolve( sFile )
  86.       local url = "https://raw.github.com/"..tArgs[2].."/"..tArgs[3].."/"..tArgs[4].."/"..tArgs[5]
  87.       if fs.exists( sPathN ) then
  88.         if not options.f then
  89.           print( "File already exists" )
  90.           return
  91.         end
  92.       end
  93.  
  94.       -- GET the contents from github
  95.       get(url,sPathN,sFile)
  96.  
  97.     else
  98.       printUsage()
  99.       return
  100.     end
  101.  
  102. elseif sCommand == "run" then
  103.   if #tArgs >= 2 then
  104.     if string.find(tArgs[2],"/") then
  105.       local url = "https://raw.github.com/"..tArgs[2]
  106.       run(url, 3)
  107.     else
  108.       if #tArgs >= 5 then
  109.         local url = "https://raw.github.com/"..tArgs[2].."/"..tArgs[3].."/"..tArgs[4].."/"..tArgs[5]
  110.         run(url, 6)
  111.       else
  112.         printUsage()
  113.         return
  114.       end
  115.     end
  116.   else
  117.     printUsage()
  118.     return
  119.   end
  120. elseif sCommand == "help" then
  121.   term.clear()
  122.   print( "github is a program that allows you to get files" )
  123.   print( "from a Github repository and store or run them in your Computer." )
  124.   print ("")
  125.   printUsage()
  126.   print ("")
  127.   print("There is also another spelling:")
  128.   print("github get <username> <reponame> <branch> <path> <filename>")
  129.   print("github run <username> <reponame> <branch> <path> [arguments]")
  130.   print("Note: Do not use the '/' (slash) character with this spelling, except in <path>!")
  131. else
  132.     printUsage()
  133.     return
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement