Created
April 7, 2020 23:13
-
-
Save velddev/75e6b6007a6374caf1857306ba1298ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- pkg cli | |
local fs = require("filesystem") | |
local http = require("http") | |
local shell = require("shell") | |
local args, ops = shell.parse(...) | |
local b = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
.. "abcdefghijklmnopqrstuvwxyz" | |
.. "0123456789+/" | |
function toBase64(data) | |
return ((data:gsub(".", function(x) | |
local r,b="",x:byte() | |
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and "1" or "0") end | |
return r | |
end).."0000"):gsub("%d%d%d?%d?%d?%d?", function(x) | |
if #x < 6 then return "" end | |
local c = 0 | |
for i=1,6 do c=c+(x:sub(i,i)=="1" and 2^(6-i) or 0) end | |
return b:sub(c+1, c+1) | |
end)..({'', '==', '='})[#data%3+1]) | |
end | |
function fromBase64(data) | |
data = string.gsub(data, "[^" .. b .. "=]", "") | |
return (data:gsub(".", function(x) | |
if(x == "=") then return '' end | |
local r, f="",(b:find(x)-1) | |
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and "1" or "0") end | |
return r | |
end):gsub("%d%d%d?%d?%d?%d?%d?%d?", function(x) | |
if #x ~= 8 then return "" end | |
local c=0 | |
for i=1,8 do c=c+(x:sub(i,i)=="1" and 2^(8-i) or 0) end | |
return string.char(c) | |
end)) | |
end | |
local function isVerbose() | |
return ops["V"] or ops["verbose"] | |
end | |
local function vlog(message) | |
if isVerbose() then | |
io.write(message .. "\n") | |
end | |
end | |
-- Fetches package from package manager. | |
local function fetch(package) | |
vlog("Fetching package '" | |
.. package | |
.. "' from net") | |
local result = http.get( | |
"https://pkg.veld.dev/" .. package) | |
local body = result.getBody() | |
if body == "404" then | |
io.write("Package '" | |
.. package | |
.. "' was not found.") | |
return nil | |
end | |
return body | |
end | |
local function readFile(path) | |
vlog("reading file from '..path..'.") | |
local file = io.open(path) | |
local buffer = file:read("*a") | |
file:close() | |
return buffer | |
end | |
local function writeToDisk(path, content) | |
vlog("writing to package '"..path.."'.") | |
local file = io.open(path, "w") | |
file:write(content) | |
file:flush() | |
file:close() | |
end | |
local function push(package, filePath) | |
end | |
local function help() | |
io.write("PKG CLI - help\n") | |
io.write("* pkg <package>\n") | |
io.write("* pkg push <package> <file>\n") | |
io.write("* pkg install <package>\n") | |
io.write("Parameters:\n") | |
io.write("* --global [-G]: install package globally\n") | |
io.write("* --verbose [-V]: enables verbose logging\n") | |
end | |
if #args < 1 then | |
help() | |
elseif #args == 1 then | |
local package = args[1] | |
local filename = package .. ".lua" | |
if ops["G"] == true | |
or ops["global"] then | |
filename = "/lib/" .. filename | |
else | |
filename = shell.getWorkingDirectory() | |
.. "/" | |
.. filename | |
end | |
local fileExists = fs.exists(filename) | |
if fileExists then | |
io.write("File '"..filename.."' already exists, overwrite? [Y/n] ") | |
local input = io.read() | |
if input == "n" or input == "N" then | |
return | |
end | |
end | |
local script = fetch(args[1]) | |
if script == nil then | |
return | |
end | |
writeToDisk(filename, fromBase64(script)) | |
io.write("added package '"..args[1].."'.\n") | |
else | |
if args[1] == "push" then | |
local payload = readFile( | |
shell.getWorkingDirectory() | |
.. "/" | |
.. args[3]) | |
local json = '{"body":"'..toBase64(payload)..'"}' | |
local result = http.post( | |
"https://pkg.veld.dev/"..args[2], json) | |
if result.getBody() == "ok" then | |
io.write("package '"..args[2].."' pushed.") | |
end | |
elseif args[1] == "install" then | |
local payload = fetch(args[2]) | |
if payload == nil then | |
return | |
end | |
writeToDisk( | |
"/bin/" .. args[2] .. ".lua", | |
fromBase64(payload)) | |
io.write("Installed app '"..args[2].."'."); | |
else | |
help() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment