-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgit.lua
More file actions
155 lines (127 loc) · 4.48 KB
/
sgit.lua
File metadata and controls
155 lines (127 loc) · 4.48 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
-- Author: Chase Riewestahl
-- Date: 1/29/2023
-- Description: Downloads all the .lua files from a github repository maintaining the directory structure
-- Usage: "sgit OPTIONAL_BRANCH" if no branch is passed defaults to "main"
local args = {...}
local githubName = "Riewest"
local repositoryName = "CC_Scripts" -- The repository must be public for this script to work
local branch = args[1] or "main"
local debug = false
local debugDir = "Debug"
local LOG_LEVELS = {}
LOG_LEVELS.info = 0 -- print minimal info to console
LOG_LEVELS.debug = 1 -- print every log to console
local logLevel = LOG_LEVELS.info -- Only applies to console output
local logFileEnabled = true -- All log messages will appear in log file if enabled no matter the loglevel
local logFile = "sgit.log"
local logDir = "logs"
local logPath = string.format("%s/%s", logDir, logFile)
---------------------------------
-- Auto Labeling Start
---------------------------------
local counterName = "ccscripts"
local httpEndpoint = "https://riecount.suicidesquid.net/count?name=" .. counterName
local postData = ""
local count
local label = "Squid_"
if not os.getComputerLabel() then
local response = http.post(httpEndpoint, postData)
if response then
local body = response.readAll()
response.close()
local ok, data = pcall(textutils.unserializeJSON, body)
if ok and type(data) == "table" and type(data.count) == "number" then
count = data.count
else
count = os.getComputerID()
end
else
count = os.getComputerID()
end
label = label .. count
print("Setting Label:", label)
os.setComputerLabel(label)
else
print("Label already set:", os.getComputerLabel())
end
---------------------------------
-- Auto Labeling End
---------------------------------
local rawHeaders = {
["cache-control"] = "max-age=1",
}
local treeApiHeaders = {
["Accept"] = "application/vnd.github+json",
["X-GitHub-Api-Version"] = "2022-11-28"
}
if logFileEnabled then
logFile = fs.open(logPath, "w")
end
if debug then
debugFile = fs.open(debugDir .. "/debug.txt", "w") -- Debug file
end
local function log(message, level)
level = level or LOG_LEVELS.info
if level <= logLevel then
print(message)
end
if logFileEnabled then
logFile.writeLine(message)
end
end
local treeRequestUrl = string.format("https://api.github.com/repos/%s/%s/git/trees/%s?recursive=true", githubName, repositoryName, branch)
log("Tree Request URL: " .. treeRequestUrl)
local treeRequest = http.get(treeRequestUrl, headers)
if treeRequest then
treeJson = treeRequest.readAll()
repoTree = textutils.unserializeJSON(treeJson, { parse_null = true })
if debug then
local debugRepoRequest = fs.open(debugDir .. "/repoTree.json", "w")
debugRepoRequest.write(treeJson)
debugRepoRequest.close()
end
if repoTree["tree"] then
for k, v in pairs(repoTree["tree"]) do
local filepath = v["path"]
if string.find(filepath, ".lua") then
fileName = fs.getName(filepath)
fileDir = fs.getDir(filepath)
if fileDir and fileDir ~= "" and not fs.exists(fileDir) then
fs.makeDir(fileDir)
end
local rawUrl = string.format("https://raw.githubusercontent.com/%s/%s/%s/%s", githubName, repositoryName, branch, filepath)
log("Attempting Download of: " .. rawUrl, LOG_LEVELS.debug)
local rawRequest = http.get(rawUrl, rawHeaders)
if rawRequest then
local content = rawRequest.readAll() -- Read all lines in the lua file
local scriptFile = fs.open(filepath, "w")
scriptFile.write(content)
scriptFile.close()
log("File: " .. filepath .. " downloaded successfully")
else
log("File: " .. filepath .. " not found")
end
end
end
else
local err_msg = "No Files Found"
log(err_msg)
error(err_msg)
end
else
local err_msg = "Bad Tree Request"
log(err_msg)
error(err_msg)
end
local testCommand = "tests/sgit_test"
if args[1] then
testCommand = testCommand .. " " .. args[1]
end
log("Running Test Command: " .. testCommand, LOG_LEVELS.debug)
shell.run(testCommand)
if logFileEnabled then
logFile.close()
end
if debug then
debugFile.close()
end