-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitdir.lua
133 lines (119 loc) · 2.95 KB
/
gitdir.lua
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
local user = "tlsomers"
local repo = "ccstuff"
local cacheTime = 10
local gitfs = {}
local cache = {}
function http.getJSON(link, ...)
if cache[link] then
return cache[link].response
else
local req, err = http.get(link, ...)
if req then
local result = textutils.unserializeJSON(req.readAll())
req.close()
cache[link] = {response = result}
return result, err
else
return req, err
end
end
end
function gitfs.isReadOnly(fs)
return true
end
function gitfs.list(_, path)
local link = "https://api.github.com/repos/"..user.."/"..repo.."/contents/" .. path
local req, err = http.getJSON(link)
if not req then
return {}
else
local items = {}
for _,v in pairs(req) do
items[#items+1] = v.name
end
return items
end
end
function gitfs.isDir(fs, path)
if path == "" then return true end
local parent = fs.getDir(path)
local link = "https://api.github.com/repos/"..user.."/"..repo.."/contents/"..parent
local req, err = http.getJSON(link)
if not req then
return false
end
for _,v in pairs(req) do
if v.name == fs.getName(path) then
return v.type == "dir"
end
end
return false
end
function gitfs.exists(fs, path)
if path == "" then return true end
local parent = fs.getDir(path)
local link = "https://api.github.com/repos/"..user.."/"..repo.."/contents/"..parent
local req, err = http.getJSON(link)
if not req then
return false
end
for _,v in pairs(req) do
if v.name == fs.getName(path) then
return true
end
end
return false
end
function gitfs.getSize(fs, path)
local parent = fs.getDir(path)
local link = "https://api.github.com/repos/"..user.."/"..repo.."/contents/"..parent
local req, err = http.getJSON(link)
for _,v in pairs(req) do
if v.name == fs.getName(path) then
return v.size
end
end
return 0
end
function gitfs.getDrive()
return "git"
end
function gitfs.getAttibutes(fs, path)
local parent = fs.getDir(path)
local link = "https://api.github.com/repos/"..user.."/"..repo.."/contents/"..parent
local req, err = http.getJSON(link)
for _,v in pairs(req) do
if v.name == fs.getName(path) then
return {
size = v.size,
isDir = v.type == "dir",
created = 0,
modified = 0
}
end
end
return {}
end
function gitfs.open(fs, path, mode)
if mode ~= "r" then
error("Cannot write to read only files")
else
local link = "https://api.github.com/repos/"..user.."/"..repo.."/contents/"..path
local r1, err = http.getJSON(link)
if not r1 then
return nil, path..": No such file"
else
local r2, err = http.get(r1.download_url)
if not r2 then
return nil, path..": Cannot open file"
else
local file = fs.open(fs.combine("git",path), "w")
file.write(r2.readAll())
file.close()
r2.close()
return fs.open(fs.combine("git", path), "r")
end
end
end
end
fs.mount("git", gitfs)