Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --edit of the pastebin program to get stuff from Github, edited by Vexatos
- local term = require("term")
- local shell = require("shell")
- local fs = require("filesystem")
- local component = require("component")
- local internet = require("internet")
- local function printUsage()
- print( "Usages:" )
- print( "github get username/reponame/branch/path <filename>" )
- print( "github run username/reponame/branch/path <arguments>" )
- print( "'github help' for more information" )
- end
- local tArgs = { ... }
- --local http = component.internet
- -- if not http then
- -- print( "Github requires an Internet Card to run" )
- -- return
- -- end
- local function get(name,reponame,branch,path)
- write( "Connecting to github.com... " )
- local response = http.request(
- "https://raw.github.com/"..name.."/"..reponame.."/"..branch.."/"..path
- )
- if response then
- print( "Success." )
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- print( "Failed." )
- end
- end
- local function getFull(path)
- term.write( "Connecting to github.com... " )
- local sResponse = ""
- local result, response = pcall( internet.request,
- "https://raw.github.com/"..path )
- if result then
- print( "Success." )
- for chunk in response do
- if string.len(sResponse) <= 0 then
- sResponse = chunk
- else
- sResponse = string.gsub(sResponse,"\n","\n"..chunk)
- end
- end
- -- response.close()
- return sResponse
- else
- print( "Failed." )
- end
- end
- local sCommand = tArgs[1]
- if sCommand == "get" then
- -- Download a file from github.com
- if #tArgs < 6 then
- if #tArgs == 3 then
- -- Determine file to download
- local sPath = tArgs[2]
- local sFile = tArgs[3]
- local sPathN = shell.resolve( sFile )
- if fs.exists( sPathN ) then
- print( "File already exists" )
- return
- end
- -- GET the contents from github
- local res = getFull(sPath)
- if res then
- local file = fs.open( sPathN, "w" )
- file.write( res )
- file.close()
- print( "Downloaded as "..sFile )
- end
- else
- printUsage()
- return
- end
- elseif #tArgs == 6 then
- -- Determine file to download
- local sName = tArgs[2]
- local sRepo = tArgs[3]
- local sBranch = tArgs[4]
- local sPath = tArgs[5]
- local sFile = tArgs[6]
- local sPathN = shell.resolve( sFile )
- if fs.exists( sPathN ) then
- print( "File already exists" )
- return
- end
- -- GET the contents from github
- local res = get(sName,sRepo,sBranch,sPath)
- if res then
- local file = fs.open( sPathN, "w" )
- file.write( res )
- file.close()
- print( "Downloaded as "..sFile )
- end
- else
- printUsage()
- return
- end
- elseif sCommand == "run" then
- if string.find(tArgs[2],"/") then
- if #tArgs >= 2 then
- local sPath = tArgs[2]
- local res = getFull(sPath)
- if res then
- local func, err = loadstring(res)
- if not func then
- print( err )
- return
- end
- setfenv(func, getfenv())
- local success, msg = pcall(func, unpack(tArgs, 3))
- if not success then
- print( msg )
- end
- end
- else
- printUsage()
- return
- end
- else
- if #tArgs >= 5 then
- local sName = tArgs[2]
- local sRepo = tArgs[3]
- local sBranch = tArgs[4]
- local sPath = tArgs[5]
- local res = get(sName,sRepo,sBranch,sPath)
- if res then
- local func, err = loadstring(res)
- if not func then
- print( err )
- return
- end
- setfenv(func, getfenv())
- local success, msg = pcall(func, unpack(tArgs, 6))
- if not success then
- print( msg )
- end
- end
- else
- printUsage()
- return
- end
- end
- elseif sCommand == "help" then
- term.clear()
- print( "github is a program that allows you to get files" )
- print( "from a Github repository and store or run them in your Computer." )
- print ("")
- printUsage()
- print ("")
- print("There is also another spelling:")
- print("github get <username> <reponame> <branch> <path> <filename>")
- print("github run <username> <reponame> <branch> <path> [arguments]")
- print("Note: Do not use the '/' (slash) character with this spelling, except in <path>!")
- else
- printUsage()
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement