Advertisement
TangentFox

pkg v1.5.10 [cc-pkg]

Aug 24th, 2019 (edited)
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | None | 0 0
  1. -- to install: pastebin get 9Li3u4Rc /bin/pkg
  2. version = "1.5.10"
  3.  
  4. -- check data type
  5. -- "package" a list of files to download
  6. -- "list" a list of packages
  7. -- "unknown" anything else
  8. function type(data)
  9.   if data:match("^/[%Z%C\n]+=[^\n]+\n?") then
  10.     return "package"
  11.   elseif data:match("^[%w%-]+=[%w%-]+\n?") then
  12.     return "list"
  13.   else
  14.     return "unknown"
  15.   end
  16. end
  17.  
  18. -- recursively resolves names until a paste id is returned
  19. -- if a name fails to resolve, it will be returned
  20. function id(name_or_id)
  21.   if fs.exists("/etc/pkg/names.list") then
  22.     for line in io.lines("/etc/pkg/names.list") do
  23.       local first, last = line:find(name_or_id, 1, true)
  24.       local separator = line:find("=")
  25.       if first == 1 and last == (separator - 1) then
  26.         return id(line:sub(separator + 1))
  27.       end
  28.     end
  29.   end
  30.   return name_or_id
  31. end
  32.  
  33. function append(path, data)
  34.   local file = io.open(path, "a")
  35.   if not file then error("Failed to open " .. path) end
  36.   file:write(data .. "\n")
  37.   file:close()
  38. end
  39.  
  40. function save(path, data)
  41.   local file = io.open(path, "w")
  42.   if not file then error("Failed to open " .. path) end
  43.   file:write(data)
  44.   file:close()
  45. end
  46.  
  47. -- downloads pastes by id, returning their data, or erroring
  48. function down(id)
  49.   local response = http.get(
  50.     "https://pastebin.com/raw/"..textutils.urlEncode(id).."?cb="..("%x"):format(math.random(0, 2^30)),
  51.     { ["User-Agent"] = "pkg v" .. version }
  52.   )
  53.   if response and response.getResponseCode() == 200 then
  54.     local data = response.readAll()
  55.     response.close()
  56.     append("/etc/pkg/ids.list", id)
  57.     return data
  58.   else
  59.     error("Failed to download https://pastebin.com/" .. id .. "\nIs " .. id .. " a valid paste id? You may be missing dependencies.")
  60.   end
  61. end
  62.  
  63. -- adds a list of new packages to the master list
  64. function src(data)
  65.   local sources = {}
  66.   if fs.exists("/etc/pkg/names.list") then
  67.     for line in io.lines("/etc/pkg/names.list") do
  68.       local s = line:find("=")
  69.       sources[line:sub(1, s - 1)] = line:sub(s + 1)
  70.     end
  71.   end
  72.   for line in data:gmatch("([^\n]+)\n?") do
  73.     local s = line:find("=")
  74.     if s then
  75.       sources[line:sub(1, s - 1)] = line:sub(s + 1)
  76.     else
  77.       print("Invalid source: " .. line)
  78.     end
  79.   end
  80.   local file = io.open("/etc/pkg/names.list", "w")
  81.   if not file then error("Failed to open /etc/pkg/names.list") end
  82.   for k,v in pairs(sources) do
  83.     file:write(k .. "=" .. v .. "\n")
  84.   end
  85.   file:close()
  86. end
  87.  
  88. -- recursively handles getting any source lists, packages, or files
  89. -- path is used for saving files only
  90. function get(name_or_id, path)
  91.   local data = down(id(name_or_id))
  92.   local pkg_type = type(data)
  93.   if pkg_type == "package" then
  94.     print("Getting package: " .. name_or_id)
  95.     for line in data:gmatch("([^\n]+)\n?") do
  96.       local s = line:find("=")
  97.       if s then
  98.         local name_or_id = line:sub(s + 1)
  99.         path = line:sub(1, s - 1)
  100.         print("Downloading " .. name_or_id .. " to " .. path)
  101.         get(name_or_id, path)
  102.       else
  103.         print("Invalid file: " .. line)
  104.       end
  105.     end
  106.     save("/etc/pkg/" .. name_or_id, data)
  107.   elseif pkg_type == "list" then
  108.     print("Adding sources: " .. name_or_id)
  109.     src(data)
  110.   else
  111.     if not path then
  112.       path = "/" .. name_or_id
  113.       save("/etc/pkg/" .. name_or_id, path .. "=" .. name_or_id)
  114.     end
  115.     save(path, data)
  116.   end
  117. end
  118.  
  119. local function usage()
  120.   print("cc-pkg v"..version.."\nUsage:\n pkg get|run <package|id> <path>\nInstall or run a package or file by pastebin ID. Paths are used for files only.")
  121. end
  122.  
  123. if not fs.exists("/etc/pkg/names.list") then
  124.   save("/etc/pkg/ids.list", "9Li3u4Rc\n")
  125.   save("/etc/pkg/pkg", "/bin/pkg=9Li3u4Rc")
  126.   get("AH4zw4n0") -- default packages list
  127. end
  128.  
  129. local arg = {...}
  130. if #arg < 1 then usage() return end
  131. local command = table.remove(arg, 1) or ""
  132. -- command -> get | src | run | put
  133. -- arg[1]  -> name_or_id
  134. -- arg[2]  -> path
  135.  
  136. if #command > 0 and fs.exists("/lib/pkg-commands/"..command) then
  137.   shell.run("/lib/pkg-commands/"..command, unpack(arg))
  138. elseif command == "get" or command == "src" or command == "run" then
  139.   get(unpack(arg))
  140.   if command == "run" then
  141.     local path = shell.resolve(arg[2] or arg[1]) -- specified path or package name might be its main binary name
  142.     if not path then path = shell.resolve(id(arg[1])) end -- last chance, maybe the resolved paste id is accessible
  143.     if path then
  144.       shell.run(path)
  145.     else
  146.       error("Could not determine what to run.")
  147.     end
  148.   end
  149. elseif command == "put" then
  150.   print("Not implemented.")
  151. else
  152.   usage()
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement