· 4 years ago · Jun 19, 2021, 08:38 PM
1local tArgs, gBranch = {...}, ""
2local usage = [[
3 update type
4 Everything inside the directory will be
5 downloaded to downloads/CC_Syrup/.
6 ]]
7
8local title = "Updating "..gBranch
9local fileList = {dirs={},files={}}
10local x , y = term.getSize()
11
12-- GUI
13function printTitle()
14 local line = 2
15 term.setCursorPos(1,line)
16 for i = 2, x, 1 do write("-") end
17 term.setCursorPos((x-title:len())/2,line+1)
18 print(title)
19 for i = 2, x, 1 do write("-") end
20end
21
22function writeCenter( str )
23 term.clear()
24 printTitle()
25 term.setCursorPos((x-str:len())/2-1,y/2-1)
26 for i = -1, str:len(), 1 do write("-") end
27 term.setCursorPos((x-str:len())/2-1,y/2)
28 print("|"..str.."|")
29 term.setCursorPos((x-str:len())/2-1,y/2+1)
30 for i = -1, str:len(), 1 do write("-") end
31end
32
33function printUsage()
34 local str = "Press space key to continue"
35 term.clear()
36 printTitle()
37 term.setCursorPos(1,y/2-4)
38 print(usage)
39 term.setCursorPos((x-str:len())/2,y/2+7)
40 print(str)
41 while true do
42 local event, param1 = os.pullEvent("key")
43 if param1 == 57 then
44 sleep(0)
45 break
46 end
47 end
48 term.clear()
49 term.setCursorPos(1,1)
50end
51
52-- Download File
53function downloadFile( path, url, name )
54 writeCenter("Downloading File: "..name)
55 dirPath = path:gmatch('([%w%_%.% %-%+%,%;%:%*%#%=%/]+)/'..name..'$')()
56 if dirPath ~= nil and not fs.isDir(dirPath) then fs.makeDir(dirPath) end
57 local content = http.get(url)
58 local file = fs.open(path,"w")
59 file.write(content.readAll())
60 file.close()
61end
62
63-- Get Directory Contents
64function getGithubContents( path )
65 local pType, pPath, pName, checkPath = {}, {}, {}, {}
66 local response = http.get("https://api.github.com/repos/KojoOkami/CC_Syrup/contents//?ref="..gBranch)
67 if response then
68 response = response.readAll()
69 if response ~= nil then
70 for str in response:gmatch('"type":"(%w+)"') do table.insert(pType, str) end
71 for str in response:gmatch('"path":"([^\"]+)"') do table.insert(pPath, str) end
72 for str in response:gmatch('"name":"([^\"]+)"') do table.insert(pName, str) end
73 end
74 else
75 writeCenter( "Error: Can't resolve URL" )
76 sleep(2)
77 term.clear()
78 term.setCursorPos(1,1)
79 error()
80 end
81 return pType, pPath, pName
82end
83
84-- Blacklist Function
85function isBlackListed( path )
86 if blackList:gmatch("@"..path)() ~= nil then
87 return true
88 end
89end
90
91-- Download Manager
92function downloadManager( path )
93 local fType, fPath, fName = getGithubContents( path )
94 for i,data in pairs(fType) do
95 if data == "file" then
96 checkPath = http.get("https://raw.github.com/KojoOkami/CC_Syrup/"..gBranch.."/"..fPath[i])
97 if checkPath == nil then
98
99 fPath[i] = fPath[i].."/"..fName[i]
100 end
101 local path = "/CC_Syrup/"..fPath[i]
102 if not fileList.files[path] and not isBlackListed(fPath[i]) then
103 fileList.files[path] = {"https://raw.github.com/KojoOkami/CC_Syrup/"..gBranch.."/"..fPath[i],fName[i]}
104 end
105 end
106 end
107 for i, data in pairs(fType) do
108 if data == "dir" then
109 local path = "/CC_Syrup/"..fPath[i]
110 if not fileList.dirs[path] then
111 writeCenter("Listing directory: "..fName[i])
112 fileList.dirs[path] = {"https://raw.github.com/KojoOkami/CC_Syrup/"..gBranch.."/"..fPath[i],fName[i]}
113 downloadManager( fPath[i] )
114 end
115 end
116 end
117end
118
119-- Main Function
120function main( path )
121 writeCenter("Connecting to Github")
122 downloadManager(path)
123 for i, data in pairs(fileList.files) do
124 downloadFile( i, data[1], data[2] )
125 end
126 writeCenter("Download completed")
127 sleep(2,5)
128 term.clear()
129 term.setCursorPos(1,1)
130end
131
132-- Parse User Input
133function parseInput( user, repo , dldir, path, branch )
134 if path == nil then path = "" end
135 if branch ~= nil then gBranch = branch end
136 if repo == nil then printUsage()
137 else
138 gUser = user
139 gRepo = repo
140 main( path )
141 end
142end
143
144if not http then
145 writeCenter("You need to enable the HTTP API!")
146 sleep(3)
147 term.clear()
148 term.setCursorPos(1,1)
149else
150 for i=1, 5, 1 do
151 if tArgs[i] == "." then tArgs[i] = nil end
152 end
153 parseInput( tArgs[1], tArgs[2], tArgs[3], tArgs[4], tArgs[5] )
154end