Advertisement
tobast

[CC] github_get

Apr 20th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. --[[[
  2.     Attempts to retreive a given directory from github
  3.     Retreives the directory from tobast/computercraft_softs from github.
  4. --]]
  5.  
  6. RAW_GITHUB_BASEURL='https://raw.githubusercontent.com/tobast/computercraft_softs/master/'
  7. TREE_GITHUB_BASEURL='https://api.github.com/repos/tobast/computercraft_softs/git/trees/'
  8.  
  9. function getFileFromGithub(githubpath, localpath)
  10.     print('Get '..githubpath)
  11.     local fileContents = http.get(RAW_GITHUB_BASEURL..githubpath).readAll()
  12.     if(fileContents == nil) then
  13.         error('Could not fetch raw file '..githubpath)
  14.     end
  15.     if(fs.exists(localpath)) then
  16.         fs.delete(localpath)
  17.     end
  18.     local file = io.open(localpath,'w')
  19.     file:write(fileContents)
  20.     file:close()
  21. end
  22.  
  23. -- Auto-get json lib if not present, to make the script work easily
  24. if(fs.exists('/lib/json') == false) then
  25.     fs.makeDir('/lib')
  26.     getFileFromGithub('github_get/lib/json','/lib/json')
  27. end
  28. -- END auto-get json
  29. os.loadAPI('/lib/json')
  30.  
  31. function splitStringSlash(str)
  32.     local pos = str:find('/')
  33.     if(pos == nil) then
  34.         return {str,''}
  35.     else
  36.         return {str:sub(1,pos-1),str:sub(pos+1)}
  37.     end
  38. end
  39.  
  40. function retrieve(path,root,sha,github_path)
  41.     if(sha == nil) then
  42.         sha = 'HEAD'
  43.     end
  44.     if(github_path == nil) then
  45.         github_path = ''
  46.     end
  47.  
  48.     local jsonStr = http.get(TREE_GITHUB_BASEURL..sha)
  49.     if(jsonStr == nil) then
  50.         error('Could not fetch tree at '..github_path)
  51.     end
  52.     local jsonObject = json.decode(jsonStr.readAll())
  53.     local tree = jsonObject.tree
  54.  
  55.     if(path == '') then
  56.         for _,item in pairs(tree) do
  57.             if(item.type == 'blob') then -- File, get it!
  58.                 getFileFromGithub(github_path..(item.path), root..(item.path))
  59.             elseif(item.type == 'tree') then
  60.                 fs.makeDir(root..(item.path))
  61.                 retrieve('',root..(item.path)..'/',(item.sha),github_path..(item.path)..'/')
  62.             end
  63.         end
  64.     else
  65.         local splitted = splitStringSlash(path)
  66.         local pathHead = splitted[1]
  67.         local pathTail = splitted[2]
  68.  
  69.         local found = false
  70.         for _,item in pairs(tree) do
  71.             if(item.type == 'tree' and item.path == pathHead) then
  72.                 found = true
  73.                 retrieve(pathTail,root,item.sha,github_path..(item.path)..'/')
  74.             end
  75.         end
  76.         if(found == false) then
  77.             error(github_path..pathHead..' does not exist!')
  78.             return
  79.         end
  80.     end
  81. end
  82.  
  83. function main(argv)
  84.     if(#argv < 1) then
  85.         print('Missing argument: path. Exiting.')
  86.     else
  87.         local path = argv[1]
  88.         local root = '/'
  89.         if(argv[2] ~= nil) then
  90.             root = argv[2]
  91.         end
  92.  
  93.         if(root:sub(#root) ~= '/') then
  94.             root = root..'/'
  95.         end
  96.         if(fs.isDir(root) == false) then
  97.             fs.makeDir(root)
  98.         end
  99.  
  100.         retrieve(path,root)
  101.     end
  102. end
  103.  
  104. main({...})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement