Krenair

gist command for computercraft turtles

Apr 28th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.13 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usage: gist get <code> <filename>" )
  3. end
  4.  
  5. local tArgs = { ... }
  6. if #tArgs < 2 then
  7.     printUsage()
  8.     return
  9. end
  10.  
  11. if not http then
  12.     print( "Gist requires http API" )
  13.     print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  14.     return
  15. end
  16.  
  17. local sCommand = tArgs[1]
  18. if sCommand == "get" then
  19.     -- Download a file from gist.github.com
  20.     if #tArgs < 3 then
  21.         printUsage()
  22.         return
  23.     end
  24.  
  25.     -- Determine file to download
  26.     local sCode = tArgs[2]
  27.     local sFile = tArgs[3]
  28.     local sPath = shell.resolve( sFile )
  29.     if fs.exists( sPath ) then
  30.         print( "File already exists" )
  31.         return
  32.     end
  33.    
  34.     -- GET the contents from github
  35.     print( "Connecting to gist.github.com... " )
  36.     local response = http.get( "https://gist.github.com/"..sCode.."/raw" )
  37.     print( "https://gist.github.com/"..sCode.."/raw" )
  38.        
  39.     if response then
  40.         print( "Success." )
  41.        
  42.         local sResponse = response.readAll()
  43.         response.close()
  44.        
  45.         local file = fs.open( sPath, "w" )
  46.         file.write( sResponse )
  47.         file.close()
  48.        
  49.         print( "Downloaded as "..sFile )
  50.        
  51.     else
  52.         print( "Failed." )
  53.     end
  54.  
  55. else
  56.     printUsage()
  57.     return
  58. end
Advertisement
Add Comment
Please, Sign In to add comment