Advertisement
Guest User

Untitled

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