Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Custom variables
- rebootAfter = false
- repo = arg[1]
- token = arg[2]
- if repo == nil and token == nil and fs.exists('/disk/github-config.json') then
- -- Load it from the config file
- fp = fs.open('/disk/github-config.json', 'r')
- config = textutils.unserialize(fp.readAll())
- fp.close()
- repo = config['repo']
- token = config['token']
- else
- -- Persist the Github config
- serializedConfig = textutils.serialize({['repo'] = repo, ['token'] = token})
- fp = fs.open('/disk/github-config.json', 'w')
- fp.write(serializedConfig)
- fp.close()
- rebootAfter = true
- end
- -- HTTP configuration
- headers = { ['Authorization'] = 'token ' .. token, ['Accept'] = 'application/vnd.github.v3.raw'}
- baseUrl = 'https://api.github.com/repos/' .. repo
- -- Get all the files and directories
- rawTree = http.get(baseUrl .. '/git/trees/master?recursive=1', headers).readAll()
- files = string.gmatch(rawTree, '"path":"([^"]+)"')
- types = string.gmatch(rawTree, '"type":"([^"]+)"')
- filesDat = {}
- directoriesDat = {}
- for file in files
- do
- type = types()
- if type == 'tree' then
- table.insert(directoriesDat, file)
- print('Creating a directory at ' .. file)
- fs.makeDir('/disk/' .. file)
- else
- table.insert(filesDat, file)
- print('Downloading ' .. file)
- data = http.get(baseUrl .. '/contents/' .. file, headers).readAll()
- fp = fs.open('/disk/' .. file, 'w')
- fp.write(data)
- fp.close()
- end
- end
- -- Save the list of files and directories
- fp = fs.open('/disk/files.dat', 'w')
- fp.write(textutils.serialize(filesDat))
- fp.close()
- fp = fs.open('/disk/directories.dat', 'w')
- fp.write(textutils.serialize(directoriesDat))
- fp.close()
- if rebootAfter then
- os.reboot()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement