Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local github = dofile("apis/github")
- local selectedAction = select(1, ...)
- local passed = { select(2, ...) }
- local subcommands = {
- clone = {
- usage = "github clone <user>/<repo> [-b <branchname> | -t <tagname>] [<destination>] [-a <username>]",
- flags = {
- ['-b'] = 'branch',
- ['--branch'] = 'branch',
- ['-t'] = 'tag',
- ['--tag'] = 'tag',
- ['-a'] = 'authed',
- ['-auth'] = 'authed'
- }
- },
- auth = {
- usage = "github auth <user> [<api token> | -d ]",
- flags = {
- ['-d'] = 'delete',
- ['--delete'] = 'delete'
- }
- }
- }
- local action = subcommands[selectedAction]
- if not action then
- for k, v in pairs(subcommands) do
- print(v.usage)
- print(" ")
- end
- return
- end
- -- make a bar of the form "[====> ] 50%"
- function progressbar(f)
- local left = "["
- local right = ("]%3d%%"):format(f * 100)
- local gap = select(1, term.getSize()) - #left - #right
- local at = math.floor(f * (gap + 1))
- for i = 1, gap do
- left = left .. (
- i < at and "=" or
- i > at and " " or
- ">"
- )
- end
- return left .. right
- end
- -- get flag arguments
- local function findFlag(arg)
- if arg.sub(1,1) == '-' then
- if not action.flags[arg] then
- return printError(('Unknown flag argument %s'):format(arg))
- end
- end
- return action.flags[arg]
- end
- local args = {}
- local flags = {}
- local lastFlagged = 0
- for k, arg in pairs(passed) do
- if k ~= lastFlagged then
- local flagName = findFlag(arg)
- if flagName then
- flags[flagName] = passed[k + 1] or true
- lastFlagged = k + 1 or true
- else
- table.insert(args, arg)
- end
- end
- end
- if flags.branch and flags.tag then
- return printError('--branch and --tag cannot both be specified')
- end
- local function hasEnoughSpace(repoSize, freeSpace)
- -- The value reported by github underestimates the one reported by CC. This tries
- -- to guess when this matters.
- local sizeError = 0.2
- local function warnAndContinue()
- write("Repository may be too large to download, attempt anyway? [Y/n]: ")
- local validAnswers = { [''] = 'yes', y = 'yes', yes = 'yes', n = 'no', no = 'no' }
- local input = io.read()
- while not validAnswers[input:lower()] do
- print("Please type [y]es or [n]o")
- input = io.read()
- end
- return validAnswers[input:lower()] == 'yes'
- end
- if repoSize > freeSpace then
- return false
- elseif repoSize * (1 + sizeError) > freeSpace then
- return warnAndContinue()
- else
- return true
- end
- end
- local function sizeStr(bytes)
- local unit = 1024
- if bytes < unit then
- return ("%s byte(s)"):format(bytes)
- else
- local multi = 10^(1)
- local KiB = math.floor((bytes/unit) * multi + 0.5) / multi
- return ("%s KiB"):format(KiB)
- end
- end
- if action == subcommands.clone then
- -- parse input
- local repo, dest = args[1], args[2]
- local treeName = flags.branch or flags.tag or 'master'
- if not repo then return printError("No repo specified") end
- user, repo = repo:match('^(.-)/(.+)$')
- if not (user and repo) then return printError("Invalid repo name - should be user/repo") end
- dest = shell.resolve(dest or repo)
- local auth = nil
- if flags.authed then
- -- if no user is found auth should remain nil.
- auth = github.Auth.get(flags.authed)
- end
- if flags.authed and not auth then
- return printError("Unknown user! Add one with 'git auth'")
- elseif flags.authed and not auth:checkToken() then
- return printError("Invalid Token!")
- end
- -- get file listings
- local repo = github.repo(user, repo, auth)
- print("Discovering files...")
- local tree = repo:tree(treeName)
- -- download the files
- local totalSize = tree.size
- local freeSpace = fs.getFreeSpace(dest)
- if not hasEnoughSpace(totalSize, freeSpace) then
- local errStr = "Repository is %s, but only %s are free on this computer. Aborting!"
- errStr = errStr:format(sizeStr(totalSize), sizeStr(freeSpace))
- return printError(errStr)
- end
- print("Downloading:")
- local size = 0
- tree:cloneTo(dest, function(item)
- -- called every time a download completes
- if getmetatable(item) == github.Blob then
- size = size + item.size
- term.clearLine()
- print(" "..item:fullPath())
- term.write(progressbar(size/totalSize))
- term.setCursorPos(1, select(2, term.getCursorPos()))
- end
- end)
- term.scroll(1)
- return
- end
- if action == subcommands.auth then
- local user, token = args[1], args[2]
- if not user then return printError("No user specified.") end
- if flags.delete then
- github.Auth.delete(user)
- print(('Deleted github token for user %s'):format(user))
- else
- if not token then return printError("No token specified.") end
- local auth = github.Auth.new('oauth', user, token)
- if auth:checkToken() then
- auth:save()
- print(('Saved github token for user %s'):format(auth.user))
- else
- return printError("Invalid token!")
- end
- end
- return
- end
- print(action.usage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement