Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- for k,v in pairs(paths) do
- -- local splitpath ={}
- -- join(v:split('/',nil,true),splitpath)
- -- for i=1,#splitpath,1 do
- -- if i == 1 then path = "/repos/test" else path = path.."/"..splitpath[i] end
- -- if not fs.exists(path) then
- -- if i == #splitpath then
- -- -- code for pulling down a file from github goes here
- -- -- file should be made at "path" from github path
- -- else
- -- fs.mkDir(path)
- -- end
- -- end
- -- end
- -- none of the above is needed at all. ccTweaked's CraftOS is capable of creating nested dirs/files where they didn't previously exist (no need to iterate over parts of a path)
- function join(src,dest)
- if src == dest then
- local temp = {}
- join(src,temp)
- join(temp,dest)
- else
- for k,v in pairs(src) do
- -- print("src entry:",k,v)
- -- if #dest > 0 then print("dest@len:", #dest,dest[#dest]) end
- dest[#dest+1] = v
- -- print("dest@new len:", #dest,dest[#dest])
- -- os.sleep(5)
- end
- end
- end
- targs = {...}
- paths={}
- assert(targs[1]~=nil and fs.exists(targs[1]))
- input=fs.open(targs[1],"r")
- line=input.readLine()
- while line ~= nil do
- join({line},paths)
- line=input.readLine()
- end
- input.close()
- tbPath="repos/Kammon/ccTweaked" -- TODO: make this user-provided for on-board download function outside of startup
- rbPath="https://raw.githubusercontent.com/Kammon/ccTweaked/main" -- TODO: also make this user-provided outside of startup
- -- build paths
- for k,v in pairs(paths) do
- if string.find(v,"/",1,true)~=1 then paths[k]="/"..paths[k] end -- prepend w/ '/' if not present
- paths[k] = {base=paths[k],repo=paths[k],turtle=tbPath..paths[k]} -- build various paths
- -- clean up turtle paths to avoid trailing whitespace errors
- paths[k].turtle = string.gsub(paths[k].turtle,"%s*$","") -- remove trailing whitespace, if any
- paths[k].turtle = string.gsub(paths[k].turtle,"%s","_") -- replace whitespace with underscores
- -- url encoding characters for git repo
- paths[k].repo = string.gsub(paths[k].repo,"%%","%%25") -- replace all '%' first, as it's used in encoding
- paths[k].repo = string.gsub(paths[k].repo," ","%%20")
- paths[k].repo = string.gsub(paths[k].repo,"!","%%21")
- paths[k].repo = string.gsub(paths[k].repo,'"','%%22')
- paths[k].repo = string.gsub(paths[k].repo,"#","%%23")
- paths[k].repo = string.gsub(paths[k].repo,"%$","%%24")
- paths[k].repo = string.gsub(paths[k].repo,"&","%%26")
- paths[k].repo = string.gsub(paths[k].repo,"'","%%27")
- paths[k].repo = string.gsub(paths[k].repo,"%(","%%28")
- paths[k].repo = string.gsub(paths[k].repo,"%)","%%29")
- paths[k].repo = string.gsub(paths[k].repo,"%*","%%2A")
- paths[k].repo = string.gsub(paths[k].repo,"%+","%%2B")
- paths[k].repo = string.gsub(paths[k].repo,",","%%2C")
- paths[k].repo = string.gsub(paths[k].repo,"%-","%%2D")
- paths[k].repo = string.gsub(paths[k].repo,"%.","%%2E")
- paths[k].repo = string.gsub(paths[k].repo,":","%%3A")
- paths[k].repo = string.gsub(paths[k].repo,";","%%3B")
- paths[k].repo = string.gsub(paths[k].repo,"<","%%3C")
- paths[k].repo = string.gsub(paths[k].repo,"=","%%3D")
- paths[k].repo = string.gsub(paths[k].repo,">","%%3E")
- paths[k].repo = string.gsub(paths[k].repo,"%?","%%3F")
- paths[k].repo = string.gsub(paths[k].repo,"@","%%40")
- paths[k].repo = string.gsub(paths[k].repo,"%[","%%5B")
- paths[k].repo = string.gsub(paths[k].repo,"\\","%%5C")
- paths[k].repo = string.gsub(paths[k].repo,"%]","%%5D")
- paths[k].repo = string.gsub(paths[k].repo,"%^","%%5E")
- paths[k].repo = string.gsub(paths[k].repo,"_","%%5F")
- paths[k].repo = string.gsub(paths[k].repo,"`","%%60")
- paths[k].repo = string.gsub(paths[k].repo,"{","%%7B")
- paths[k].repo = string.gsub(paths[k].repo,"|","%%7C")
- paths[k].repo = string.gsub(paths[k].repo,"}","%%7D")
- paths[k].repo = string.gsub(paths[k].repo,"~","%%7E")
- paths[k].repo = rbPath..paths[k].repo -- prepend repo url with base repository url
- end
- -- start downloading files
- for k,v in pairs(paths) do
- local resp = http.get(paths[k].repo)
- if resp.getResponseCode() == 200 and tonumber(resp.getResponseHeaders()["content-length"]) > 0 then
- if fs.exists(paths[k].turtle) then print("File Exists on Turtle. Overwriting...") end
- local file = fs.open(paths[k].turtle,"w")
- local line = resp.readLine(true)
- while line ~= nil do
- file.write(line)
- line=resp.readLine(true)
- end
- file.close()
- end
- resp.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement