Advertisement
Guest User

github

a guest
Jan 25th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.57 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 function printUsage()
  9.     print( "Usages:" )
  10.     print( "github get username/reponame/branch/path <filename>" )
  11.     print( "github run username/reponame/branch/path <arguments>" )
  12.     print( "'github help' for more information" )
  13. end
  14.  
  15. local tArgs = { ... }
  16.  
  17. --local http = component.internet
  18. --  if not http then
  19. --    print( "Github requires an Internet Card to run" )
  20. --    return
  21. --  end
  22.  
  23. local function get(name,reponame,branch,path)
  24.     write( "Connecting to github.com... " )
  25.     local response = http.request(
  26.         "https://raw.github.com/"..name.."/"..reponame.."/"..branch.."/"..path
  27.     )
  28.  
  29.     if response then
  30.         print( "Success." )
  31.  
  32.         local sResponse = response.readAll()
  33.         response.close()
  34.         return sResponse
  35.     else
  36.         print( "Failed." )
  37.     end
  38. end
  39.  
  40. local function getFull(path)
  41.     term.write( "Connecting to github.com... " )
  42.     local sResponse = ""
  43.     local result, response = pcall( internet.request,
  44.      "https://raw.github.com/"..path )
  45. if result then
  46.   print( "Success." )
  47.   for chunk in response do
  48.       if string.len(sResponse) <= 0 then
  49.         sResponse = chunk
  50.       else
  51.         sResponse = string.gsub(sResponse,"\n","\n"..chunk)
  52. end
  53. end
  54.        -- response.close()
  55.         return sResponse
  56.     else
  57.         print( "Failed." )
  58.     end
  59. end
  60.  
  61. local sCommand = tArgs[1]
  62. if sCommand == "get" then
  63.     -- Download a file from github.com
  64.     if #tArgs < 6 then
  65.       if #tArgs == 3 then
  66.  
  67.         -- Determine file to download
  68.         local sPath = tArgs[2]
  69.         local sFile = tArgs[3]
  70.         local sPathN = shell.resolve( sFile )
  71.         if fs.exists( sPathN ) then
  72.           print( "File already exists" )
  73.           return
  74.         end
  75.  
  76.         -- GET the contents from github
  77.         local res = getFull(sPath)
  78.         if res then
  79.             local file = fs.open( sPathN, "w" )
  80.             file.write( res )
  81.             file.close()
  82.  
  83.             print( "Downloaded as "..sFile )
  84.         end
  85.         else
  86.           printUsage()
  87.           return
  88.         end
  89.     elseif #tArgs == 6 then
  90.       -- Determine file to download
  91.       local sName = tArgs[2]
  92.       local sRepo = tArgs[3]
  93.       local sBranch = tArgs[4]
  94.       local sPath = tArgs[5]
  95.       local sFile = tArgs[6]
  96.       local sPathN = shell.resolve( sFile )
  97.       if fs.exists( sPathN ) then
  98.           print( "File already exists" )
  99.           return
  100.       end
  101.  
  102.       -- GET the contents from github
  103.       local res = get(sName,sRepo,sBranch,sPath)
  104.       if res then
  105.           local file = fs.open( sPathN, "w" )
  106.           file.write( res )
  107.           file.close()
  108.  
  109.         print( "Downloaded as "..sFile )
  110.       end
  111.     else
  112.       printUsage()
  113.       return
  114.     end
  115.  
  116. elseif sCommand == "run" then
  117.   if string.find(tArgs[2],"/") then
  118.     if #tArgs >= 2 then
  119.     local sPath = tArgs[2]
  120.  
  121.     local res = getFull(sPath)
  122.     if res then
  123.       local func, err = loadstring(res)
  124.       if not func then
  125.         print( err )
  126.         return
  127.       end
  128.       setfenv(func, getfenv())
  129.       local success, msg = pcall(func, unpack(tArgs, 3))
  130.       if not success then
  131.         print( msg )
  132.       end
  133.     end
  134.     else
  135.       printUsage()
  136.       return
  137.     end
  138.   else
  139.     if #tArgs >= 5 then
  140.       local sName = tArgs[2]
  141.       local sRepo = tArgs[3]
  142.       local sBranch = tArgs[4]
  143.       local sPath = tArgs[5]
  144.  
  145.       local res = get(sName,sRepo,sBranch,sPath)
  146.       if res then
  147.         local func, err = loadstring(res)
  148.         if not func then
  149.           print( err )
  150.           return
  151.         end
  152.         setfenv(func, getfenv())
  153.         local success, msg = pcall(func, unpack(tArgs, 6))
  154.         if not success then
  155.           print( msg )
  156.         end
  157.       end
  158.     else
  159.       printUsage()
  160.       return
  161.     end
  162.   end
  163. elseif sCommand == "help" then
  164.   term.clear()
  165.   print( "github is a program that allows you to get files" )
  166.   print( "from a Github repository and store or run them in your Computer." )
  167.   print ("")
  168.   printUsage()
  169.   print ("")
  170.   print("There is also another spelling:")
  171.   print("github get <username> <reponame> <branch> <path> <filename>")
  172.   print("github run <username> <reponame> <branch> <path> [arguments]")
  173.   print("Note: Do not use the '/' (slash) character with this spelling, except in <path>!")
  174. else
  175.     printUsage()
  176.     return
  177. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement