Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. -- Custom variables
  2. repo = arg[1]
  3. token = arg[2]
  4.  
  5. if repo == nil and token == nil and fs.exists('/disk/github-config.json') then
  6. -- Load it from the config file
  7. fp = fs.open('/disk/github-config.json', 'r')
  8. config = textutils.unserialize(fp.readAll())
  9. fp.close()
  10. repo = config['repo']
  11. token = config['token']
  12. else
  13. -- Persist the Github config
  14. serializedConfig = textutils.serialize({['repo'] = repo, ['token'] = token})
  15. fp = fs.open('/disk/github-config.json', 'w')
  16. fp.write(serializedConfig)
  17. fp.close()
  18. end
  19.  
  20. -- HTTP configuration
  21. headers = { ['Authorization'] = 'token ' .. token, ['Accept'] = 'application/vnd.github.v3.raw'}
  22. baseUrl = 'https://api.github.com/repos/' .. repo
  23.  
  24. -- Get all the files and directories
  25. rawTree = http.get(baseUrl .. '/git/trees/master?recursive=1', headers).readAll()
  26. files = string.gmatch(rawTree, '"path":"([^"]+)"')
  27. types = string.gmatch(rawTree, '"type":"([^"]+)"')
  28. filesDat = {}
  29. directoriesDat = {}
  30.  
  31. for file in files
  32. do
  33. type = types()
  34. if type == 'tree' then
  35. table.insert(directoriesDat, file)
  36. print('Creating a directory at ' .. file)
  37. fs.makeDir('/disk/' .. file)
  38. else
  39. table.insert(filesDat, file)
  40. print('Downloading ' .. file)
  41. data = http.get(baseUrl .. '/contents/' .. file, headers).readAll()
  42. fp = fs.open('/disk/' .. file, 'w')
  43. fp.write(data)
  44. fp.close()
  45. end
  46. end
  47.  
  48. -- Save the list of files and directories
  49. fp = fs.open('/disk/files.dat', 'w')
  50. fp.write(textutils.serialize(filesDat))
  51. fp.close()
  52. fp = fs.open('/disk/directories.dat', 'w')
  53. fp.write(textutils.serialize(directoriesDat))
  54. fp.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement