-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.lua
More file actions
170 lines (141 loc) · 3.26 KB
/
github.lua
File metadata and controls
170 lines (141 loc) · 3.26 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
local function printUsage()
print( "Usages:" )
print( "github get <repository> <branch> <directory> <filename>" )
print( "github run <repository> <branch> <directory> <arguments>" )
end
local function getRepositoryDetail(repository)
local val = {}
local temp
local url = repository
temp = string.find(url, "https://github.com/")
if temp~=1 then
return
end
url = string.sub(url, 20)
temp = string.find(url, "/")
if temp==1 or temp==nil then
return
end
val["username"]=string.sub(url, 1, temp-1)
url = string.sub(url, temp+1)
temp = string.find(url, ".git")
if temp==1 then
return
elseif temp==nil then
val["repository"]=string.sub(url, 1)
else
val["repository"]=string.sub(url, 1, temp-1)
end
return val
end
local function getString(username, repository, branch, directory)
local response = http.get(
"https://raw.githubusercontent.com/"..textutils.urlEncode( username ).."/"..textutils.urlEncode( repository ).."/"..textutils.urlEncode( branch ).."/"..textutils.urlEncode( directory )
)
if response then
local string = response.readAll()
response.close()
return string
else
return
end
end
function get(repository, branch, directory, filename)
local val = getRepositoryDetail(repository)
if val==nil then
return false
end
local username = val["username"]
local repname = val["repository"]
if fs.exists( filename ) then
return false
end
local string = getString(username, repname, branch, directory)
if val==string then
return false
end
local file = fs.open(filename, "w")
file.write(string)
file.close()
return true
end
function run(...)
local rArgs = {...}
local val = getRepositoryDetail(rArgs[1])
if val==nil then
return
end
local username = val["username"]
local repname = val["repository"]
local branch = rArgs[2]
local directory = rArgs[3]
local string = getString(username, repname, branch, directory)
if val==string then
return
end
local n = username.."_"..repname.."_"..directory
local func, err = load(string, n, "t", _ENV)
if not func then
return err
end
local success, msg = pcall(func, table.unpack(rArgs, 5))
if not success then
return msg
end
return
end
-----------------------------
local tArgs = { ... }
if not http then
printError( "CCGetGithub requires http API" )
printError( "Set http_enable to true in ComputerCraft.cfg" )
return
end
local val = {}
if tArgs[1]=="get" then
if #tArgs < 5 then
printUsage()
return
end
local success = get(tArgs[2], tArgs[3], tArgs[4], tArgs[5])
if success then
print( "Downloaded as "..tArgs[5])
else
print("Failed")
end
elseif tArgs[1]=="run" then
if #tArgs < 5 then
printUsage()
return
end
local val = getRepositoryDetail(tArgs[2])
if val==nil then
return
end
local username = val["username"]
local repname = val["repository"]
local branch = tArgs[3]
local directory = tArgs[4]
local string = getString(username, repname, branch, directory)
if val==string then
return
end
local n = username.."_"..repname.."_"..directory
local func, err = load(string, n, "t", _ENV)
if not func then
printError(err)
return
end
local success, msg = pcall(func, table.unpack(tArgs, 5))
if not success then
printError(msg)
end
return
else
if tArgs[1]==nil then
return
else
printUsage()
return
end
end