Advertisement
CaptainSpaceCat

Computercraft Github API

Dec 9th, 2021 (edited)
1,031
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local args = {...}
  2.  
  3. --[[ Setting up variables ]]
  4. local fileWriteOK
  5. local url
  6. local filename
  7. local tArgs = { ... }
  8.  
  9.  
  10. local function getHttpBody( url )
  11.     http.request( url )
  12.    
  13.     while true do
  14.         local event, url, hBody = os.pullEvent()
  15.         if event == "http_success" then
  16.             print( "HTTP SUCCESS\nURL = "..url )
  17.             return hBody
  18.         elseif event == "http_failure" then
  19.             error( "HTTP FAILURE\nURL = "..url )    -- If the error is not catched, this will exit the program.
  20.             return nil    -- In case this function is called via pcall.
  21.         end
  22.     end
  23. end
  24.  
  25. local function processHttpBody( hBody, filename )
  26.     local hFile
  27.    
  28.     if hBody then
  29.         local body = hBody.readAll()    -- Read the whole body.
  30.         hFile = io.open( filename, "w" )    -- Open the provided filename for writing.
  31.         hFile:write( body )     -- Write the body to the file.
  32.         hFile:close()   -- Save the changes and close the file.
  33.     else
  34.         print( "Sorry, no body to process." )
  35.     end
  36.    
  37.     hBody.close()   -- Do not know for sure if this is really necessary, but just in case.
  38. end
  39.  
  40. local function checkOverwrite( filename )
  41.     term.setCursorBlink( false )
  42.     print("\nWarning: File already exists. Overwrite? (Y/N)")
  43.    
  44.     while true do
  45.         event, choice = os.pullEvent( "char" )  -- Only listen for "char" events.
  46.         if string.lower( choice ) == "y" then return true end
  47.         if string.lower( choice ) == "n" then return false end
  48.     end
  49. end
  50.  
  51.  
  52. local function getRepoPointer()
  53.     local handle = io.open("configs/git.config", "r")
  54.     local username, repo
  55.     if handle then
  56.         username =  handle:read("l")
  57.         repo =  handle:read("l")
  58.         handle:close()
  59.     end
  60.     return username, repo
  61. end
  62.  
  63. local function saveRepoConfig(username, repo)
  64.     local handle = io.open("configs/git.config", "w")
  65.     if handle then
  66.         handle:write(username .. "\n")
  67.         handle:write(repo .. "\n")
  68.         handle:close()
  69.         print("Set local repo config:")
  70.         print(username .. "/" .. repo)
  71.     else
  72.         print("Failed to write to repo config")
  73.     end
  74. end
  75.  
  76. local function printUsage(fn)
  77.     if not fn then
  78.         --print base usage
  79.         print("Usage:")
  80.         print("git [repo | push | pull | run]")
  81.     elseif fn == "repo" then
  82.         --print usage for repo command
  83.         print("Usage:")
  84.         print("git repo <username> <repo>")
  85.         print("Sets the local config file to point to a specific github repo")
  86.     elseif fn == "pull" then
  87.         --print usage for pull command
  88.         print("Usage:")
  89.         print("git pull <git filename> <local filename>")
  90.         print("Downloads <git filename> from the repo in your local config")
  91.         print("Saves this file to <local filename>")
  92.     elseif fn == "push" then
  93.         --print usage for push command
  94.         print("git push is still a WIP")
  95.     elseif fn == "run" then
  96.         --print usage for run command
  97.         print("Usage:")
  98.         print("git run <git filename>")
  99.         print("Downloads <git filename> from the repo in your local config")
  100.         print("Runs this file without saving it")
  101.     end
  102. end
  103.  
  104. local function warnNoRepo()
  105.     print("Missing repo pointer")
  106.     print("First run \"git repo\" to set up")
  107.     printUsage("repo")
  108. end
  109.  
  110. local function makeGitURL(username, repo, filename)
  111.     return "https://raw.githubusercontent.com/" .. username .. "/" .. repo .. "/main/" .. filename
  112. end
  113.  
  114. if #args == 0 then
  115.     printUsage()
  116. elseif args[1] == "pull" then
  117.     -- git pull <git file> <local file>?
  118.     if #args == 2 or #args == 3 then
  119.         username, repo = getRepoPointer()
  120.         if not username then
  121.             warnNoRepo()
  122.         else
  123.             --save to filename <args[3]> if provided, else <args[2]>
  124.             processHttpBody(getHttpBody(makeGitURL(username, repo, args[2])), args[3] or args[2])
  125.         end
  126.     else
  127.         printUsage("pull")
  128.     end
  129. elseif args[1] == "push" then
  130.     -- git push <local file> <git file>
  131. elseif args[1] == "run" then
  132.     -- git run <git file>
  133. elseif args[1] == "repo" then
  134.     --git repo <username> <repository>
  135.     --sets the pointer to the repo
  136.     if #args == 3 then
  137.         saveRepoConfig(args[2], args[3])
  138.     else
  139.         printUsage("repo")
  140.     end
  141. else
  142.     --error, undefined command
  143.     printUsage()
  144. end
  145.  
  146.  
Advertisement
RAW Paste Data Copied
Advertisement