-
Notifications
You must be signed in to change notification settings - Fork 1
/
git
113 lines (104 loc) · 3.6 KB
/
git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
local tArgs, gUser, gRepo, gPath, gBranch = {...}, nil, nil, "", "master"
local usage = [[
github <user> <repo> [path] [remote path] [branch]
Remote path defaults to the root of the repo.
Path defaults to the download folder.
Branch defaults to master.
If you want to leave an option empty use a dot.
Example: github johnsmith hello-world . foo
Everything inside the directory foo will be
downloaded to downloads/hello-world/.
]]
local blackList = [[
@blacklistedfile
]]
local title = "Github Repo Downloader"
local fileList = {dirs={},files={}}
local x , y = term.getSize()
-- Download File
function downloadFile( path, url, name )
dirPath = path:gmatch('([%w%_%.% %-%+%,%;%:%*%#%=%/]+)/'..name..'$')()
if dirPath ~= nil and not fs.isDir(dirPath) then fs.makeDir(dirPath) end
local content = http.get(url)
local file = fs.open(path,"w")
file.write(content.readAll())
file.close()
end
-- Get Directory Contents
function getGithubContents( path )
local pType, pPath, pName, checkPath = {}, {}, {}, {}
local response = http.get("https://api.github.com/repos/"..gUser.."/"..gRepo.."/contents/" .. path .. "/?ref="..gBranch)
if response then
response = response.readAll()
if response ~= nil then
for str in response:gmatch('"type":"(%w+)"') do table.insert(pType, str) end
for str in response:gmatch('"path":"([^\"]+)"') do table.insert(pPath, str) end
for str in response:gmatch('"name":"([^\"]+)"') do table.insert(pName, str) end
end
else
print( "Error: Can't resolve URL" )
print(response)
error()
end
return pType, pPath, pName
end
-- Blacklist Function
function isBlackListed( path )
if blackList:gmatch("@"..path)() ~= nil then
return true
end
end
-- Download Manager
function downloadManager( path )
local fType, fPath, fName = getGithubContents( path )
for i,data in pairs(fType) do
if data == "file" then
checkPath = http.get("https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i])
if checkPath == nil then
fPath[i] = fPath[i].."/"..fName[i]
end
local path = fPath[i]
if gPath ~= "" then path = gPath.."/"..gRepo.."/"..fPath[i] end
if not fileList.files[path] and not isBlackListed(fPath[i]) then
fileList.files[path] = {"https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i],fName[i]}
end
end
end
for i, data in pairs(fType) do
if data == "dir" then
local path = gRepo .. "/" .. fPath[i]
if gPath ~= "" then path = gPath.."/"..gRepo.."/"..fPath[i] end
if not fileList.dirs[path] then
fileList.dirs[path] = {"https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i],fName[i]}
downloadManager( fPath[i] )
end
end
end
end
-- Main Function
function main( path )
downloadManager(path)
for i, data in pairs(fileList.files) do
downloadFile( i, data[1], data[2] )
end
end
-- Parse User Input
function parseInput( user, repo , dldir, path, branch )
if path == nil then path = "" end
if branch ~= nil then gBranch = branch end
if repo == nil then print("No repo")
else
gUser = user
gRepo = repo
if dldir ~= nil then gPath = dldir end
main( path )
end
end
print("Downloading github repo!")
if http then
for i=1, 5, 1 do
if tArgs[i] == "." then tArgs[i] = nil end
end
parseInput( tArgs[1], tArgs[2], tArgs[3], tArgs[4], tArgs[5] )
end
print("Done!")