Tinybang_Studio

ComputerCraft APT

Aug 15th, 2020
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. -- apt by Nameless#9000
  2. local tArgs = { ... }
  3.  
  4. if not http then
  5.     printError( "apt requires http API" )
  6.     printError( "Set http_enable to true in ComputerCraft.cfg" )
  7.     return
  8. end
  9.  
  10. --functions
  11.  
  12. local function get(url)
  13.     --write( "Connecting to server... " )
  14.     local response = http.get(
  15.         "https://raw.githubusercontent.com/Nameless9000/CC/master/apt/"..textutils.urlEncode( url )
  16.     )
  17.  
  18.     if response then
  19.         --print( "Success." )
  20.  
  21.         local sResponse = response.readAll()
  22.         response.close()
  23.         return sResponse
  24.     else
  25.         printError( "Failed." )
  26.     end
  27. end
  28.  
  29. local function split(pString, pPattern)
  30.     local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  31.     local fpat = "(.-)" .. pPattern
  32.     local last_end = 1
  33.     local s, e, cap = pString:find(fpat, 1)
  34.     while s do
  35.            if s ~= 1 or cap ~= "" then
  36.           table.insert(Table,cap)
  37.            end
  38.            last_end = e+1
  39.            s, e, cap = pString:find(fpat, last_end)
  40.     end
  41.     if last_end <= #pString then
  42.            cap = pString:sub(last_end)
  43.            table.insert(Table, cap)
  44.     end
  45.     return Table
  46.  end
  47.  
  48. -- main
  49.  
  50. if #tArgs ~= 1 then
  51.   print("Usage: apt <mode>")
  52.   return
  53. end
  54. local mode = tArgs[1]
  55.  
  56. if mode == "help" or "-h" or "--help" then
  57.     print([[
  58.     Modes:\n
  59.         help\n
  60.         install\n
  61.         list\n
  62.     ]])
  63.     return
  64. end
  65.  
  66. if mode == "list" or "-l" or "--list" then
  67.     local raw = get("p.list")
  68.     local programs = split(raw,"|")
  69.     local sh = [[
  70.     Programs:\n
  71.  
  72.     ]]
  73.     for _,pr in pairs(programs) do
  74.         sh=sh..pr.."\n  "
  75.     end
  76.     print(sh)
  77.     return
  78. end
  79.  
  80. if mode == "install" or "-i" or "--install" then
  81.  
  82.     if #tArgs ~= 3 then
  83.         print("Usage: apt install <program> <location>")
  84.         return
  85.     end
  86.  
  87.     local program = tArgs[2]
  88.     local location = tArgs[3]
  89.     local sPath = shell.resolve( location )
  90.  
  91.     local res = get("p/"..program.."/main.lua")
  92.     if res then
  93.         local file = fs.open( sPath, "w" )
  94.         file.write( res )
  95.         file.close()
  96.  
  97.         print( "Downloaded as "..location )
  98.     end
  99.  
  100.     return
  101. end
Add Comment
Please, Sign In to add comment