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 tArgs, options = shell.parse(...)
- 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
- if not component.isAvailable("internet") then
- print( "Error: Github requires an Internet Card to run" )
- return
- end
- local function get(url, sPathN, sFile)
- term.write( "Connecting to github.com... " )
- local sResponse = ""
- local file, reason = io.open(sPathN, "w")
- if not file then
- print("failed opening file for writing: " .. reason)
- return
- end
- local result, response = pcall( internet.request, url)
- if result then
- print( "Success." )
- for chunk in response do
- file:write( chunk )
- end
- file:close()
- print( "Downloaded as "..sFile )
- return sResponse
- else
- file:close()
- print( "Failed: "..response )
- end
- end
- local function run(url, sArg)
- local tmpFile = "/tmp/tmp_github.lua"
- get(url, shell.resolve(tmpFile), "tmp_github.lua")
- term.clear()
- print("Running...")
- local success, msg = shell.execute(tmpFile, _ENV, table.unpack(tArgs, sArg))
- if not success then
- print( msg )
- end
- fs.remove(tmpFile)
- 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 sFile = tArgs[3]
- local sPathN = shell.resolve( sFile )
- local url = "https://raw.github.com/"..tArgs[2]
- if fs.exists( sPathN ) then
- if not options.f then
- print("file already exists")
- return
- end
- end
- -- GET the contents from github
- get(url, sPathN, sFile)
- else
- printUsage()
- return
- end
- elseif #tArgs == 6 then
- -- Determine file to download
- local sFile = tArgs[6]
- local sPathN = shell.resolve( sFile )
- local url = "https://raw.github.com/"..tArgs[2].."/"..tArgs[3].."/"..tArgs[4].."/"..tArgs[5]
- if fs.exists( sPathN ) then
- if not options.f then
- print( "File already exists" )
- return
- end
- end
- -- GET the contents from github
- get(url,sPathN,sFile)
- else
- printUsage()
- return
- end
- elseif sCommand == "run" then
- if #tArgs >= 2 then
- if string.find(tArgs[2],"/") then
- local url = "https://raw.github.com/"..tArgs[2]
- run(url, 3)
- else
- if #tArgs >= 5 then
- local url = "https://raw.github.com/"..tArgs[2].."/"..tArgs[3].."/"..tArgs[4].."/"..tArgs[5]
- run(url, 6)
- else
- printUsage()
- return
- end
- end
- else
- printUsage()
- return
- 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