local function printUsage()
print( "Usage: gist get <code> <filename>" )
end
local tArgs = { ... }
if #tArgs < 2 then
printUsage()
return
end
if not http then
print( "Gist requires http API" )
print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
return
end
local sCommand = tArgs[1]
if sCommand == "get" then
-- Download a file from gist.github.com
if #tArgs < 3 then
printUsage()
return
end
-- Determine file to download
local sCode = tArgs[2]
local sFile = tArgs[3]
local sPath = shell.resolve( sFile )
if fs.exists( sPath ) then
print( "File already exists" )
return
end
-- GET the contents from github
print( "Connecting to gist.github.com... " )
local response = http.get( "https://gist.github.com/"..sCode.."/raw" )
print( "https://gist.github.com/"..sCode.."/raw" )
if response then
print( "Success." )
local sResponse = response.readAll()
response.close()
local file = fs.open( sPath, "w" )
file.write( sResponse )
file.close()
print( "Downloaded as "..sFile )
else
print( "Failed." )
end
else
printUsage()
return
end