Files
TPkgClient/main.lua
2025-08-01 20:33:36 +02:00

97 lines
2.8 KiB
Lua

local args = { ... }
local packageStoreageUrl = "/packages"
if args[1] == "list" then
local res = http.get("http://localhost:7000/api/repo/list")
local ds = res.readAll()
print(ds)
local json = textutils.unserializeJSON(ds)
print(json.name)
end
if args[1] == "install" then
if args[2] ~= nil then
if fs.exists(packageStoreageUrl .. "/" .. args[2]) then
print("Package already installed!")
return;
end
local ws, stat = http.websocket("ws://192.168.178.24:7000/repo/install")
local msg = {
repoName = args[2]
}
ws.send(textutils.serializeJSON(msg))
local full = 0
local chunks = {}
local received = {}
while true do
local msg = ws.receive()
local id, indexMeta, chunk = msg:match("([^|]+)|([^|]+)|(.+)")
local index, total = indexMeta:match("(%d+)/(%d+)")
index = tonumber(index)
total = tonumber(total)
if not chunks[id] then
chunks[id] = { total = total, parts = {} }
end
chunks[id].parts[index + 1] = chunk
-- Check if complete
local allReceived = true
for i = 1, total do
if not chunks[id].parts[i] then
allReceived = false
break
end
end
if allReceived and not received[id] then
received[id] = true
full = table.concat(chunks[id].parts)
print("Message received")
break
end
end
--------------
local rec_msg = full
print(rec_msg .. "ds")
local file = fs.open("output.json", "w")
file.write(rec_msg)
file.close()
local json = textutils.unserializeJSON(rec_msg)
print(json)
if json.statusCode == 404 then
print("The Requested package was not found!")
return
end
for k, v in pairs(json) do
local path = packageStoreageUrl .. v.relativePath
if fs.exists(path) then
print("File " .. path .. " already exists!")
return
end
shell.run("wget", v.downloadUrl, path)
end
else
print("Please enter a package name!")
return
end
end
if args[1] == "remove" then
if args[2] ~= nil then
if fs.exists(packageStoreageUrl .. "/" .. args[2]) then
fs.delete(packageStoreageUrl .. "/" .. args[2])
print(args[2] .. " Was removed from your system!")
else
print("No such package installed/found!")
return
end
else
print("Please enter a package name!")
return
end
end