-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitget
More file actions
159 lines (135 loc) · 3.88 KB
/
gitget
File metadata and controls
159 lines (135 loc) · 3.88 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
--[[ gitget
An updater for ComputerCraft scripts stored on a github.com
]]--
-- Configuration variables
local sConfigName = "gitget.cfg"
-- Returns full GitHub address
function createAddress(tConfig)
local sUrl = "https://raw.github.com"
tConfig["filename"] = string.gsub(tConfig["filename"], "%s", "%%20")
return (sUrl .. "/" .. tConfig["user"] .. "/" .. tConfig["project"] .. "/" .. tConfig["branch"] .. "/" .. tConfig["filename"])
end
-- Loads a table from file
function loadTable(sPath)
if not fs.exists(sPath) then
error("loadTable() file not found: " .. sPath)
end
if fs.isDir(sPath) then
error("loadTable() cannot open a directory")
end
local file = fs.open(sPath, "r")
local sTable = file.readAll()
file.close()
return textutils.unserialize(sTable)
end
-- Saves a table to file
-- Uses saveString()
function saveTable(tData, sPath)
local sSerializedData = textutils.serialize(tData)
return saveString(sSerializedData, sPath)
end
-- Saves a string to file
function saveString(sData, sPath)
if fs.isDir(sPath) then
error("saveString() cannot save over a directory")
end
local file = fs.open(sPath, "w")
file.write(sData)
file.close()
return true
end
-- Downloads a file from url provided
function download(sUrl, sPath)
print("Downloading: " .. sUrl)
if http then
local sData = http.get(sUrl)
if sData then
print("Fetched file")
return saveString(sData.readAll(), sPath)
else
print("Failed to fetch file: " .. sUrl)
return false
end
else
error("download() needs http api enabled to fetch files")
end
end
-- Writes a prompt at pos
function promptPos(sPrompt, nX, nY)
term.setCursorPos(nX, nY)
term.clearLine()
term.write(sPrompt)
return true
end
function assemblePromptTable(tConfig)
tPrompt = {
"Gitget - a github file fetcher.",
"Filename [" .. tConfig["filename"] .. "]: ",
"Branch [" .. tConfig["branch"] .. "]: ",
"Project [" .. tConfig["project"] .. "]: ",
"User [" .. tConfig["user"] .. "]: ",
"Press enter with no input to download.",
}
return tPrompt
end
-- Draws the config settings on the screen
-- returns prompt table
function drawPrompt(tPrompt)
for nKey, sValue in ipairs(tPrompt) do
promptPos(sValue, 1, nKey)
end
return true
end
-- Invokes read() after the line given
function userInput(nPos, tPrompt)
term.setCursorPos((#tPrompt[nPos] + 1), nPos)
return read()
end
-- Checks for first run and creates config file
function checkFirstInit()
if not fs.exists(sConfigName) then
tInit = {["filename"] = "", ["branch"] = "master", ["project"] = "swarm", ["user"] = githubUser or "keneo",}
saveTable(tInit, sConfigName)
end
end
function string.ends(String,End)
return End=='' or string.sub(String,-string.len(End))==End
end
-- Set some global variables
local nInputPos = 2
local tConfig
local tPrompt
local sInput
local tInputRow = {"prompt", "filename", "branch", "project", "user"}
-- Runtime section
checkFirstInit()
local filename = ({...})[1]
if not filename then
term.clear()
while true do
tConfig = loadTable(sConfigName)
tPrompt = assemblePromptTable(tConfig)
drawPrompt(tPrompt)
sInput = userInput(nInputPos, tPrompt)
if sInput == "" then
sInput = tConfig[tInputRow[nInputPos]]
break
elseif sInput == " " then
nInputPos = nInputPos + 1
if nInputPos > #tInputRow then nInputPos = 2 end
else
tConfig[tInputRow[nInputPos]] = sInput
saveTable(tConfig, sConfigName)
nInputPos = nInputPos + 1
if nInputPos > #tInputRow then nInputPos = 2 end
end
end
filename = tConfig["filename"]
end
tConfig["filename"]=filename
term.setCursorPos(1, (#tPrompt + 1))
download(createAddress(tConfig), filename)
if (string.ends(filename,".lua")) then
shell.run("rm " ..filename:gsub(".lua",""))
shell.run("mv " ..filename.. " " .. filename:gsub(".lua",""))
end