MineOS/Applications/github.lua
2015-08-05 04:36:29 +03:00

106 lines
3.5 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local internet = require("internet")
local fs = require("filesystem")
local seri = require("serialization")
local shell = require("shell")
local config = require("config")
local args, options = shell.parse(...)
local function printUsage()
io.write("\nИспользование:")
io.write("github set <ссылка на репозиторий> - установить указанный репозиторий в качестве постоянного\n")
io.write("github get <ссылка> <путь сохранения> - загрузить указанный файл из текущего репозитория\n")
io.write("github fast <ссылка на raw файл> <путь сохранения>- скачать файл без ебли мозгов\n\n")
io.write(" Примеры:")
io.write(" github set IgorTimofeev/OpenComputers\n")
io.write(" github get Applications/Home.lua Home.lua\n")
io.write(" github fast IgorTimofeev/OpenComputers/master/Applications/Home.lua Home.lua\n")
end
if #args < 3 or string.lower(tostring(args[1])) == "help" then
printUsage()
return
end
local quiet = false
if args[1] == "fast" then quiet = true end
local pathToConfig = "System/GitHub/Repository.cfg"
local currentRepository
local userUrl = "https://raw.githubusercontent.com/"
--pastebin run SthviZvU IgorTimofeev/OpenComputers/master/Applications.txt hehe.txt
------------------------------------------------------------------------------------------
local function info(text)
if not quiet then print(text) end
end
--ЗАГРУЗОЧКА С ГИТХАБА
local function getFromGitHub(url, path)
local sContent = ""
info(" ")
info("Подключаюсь к GitHub по адресу "..url)
local result, response = pcall(internet.request, url)
if not result then
return nil
end
info(" ")
if result == "" or result == " " or result == "\n" then info("Файл пустой, либо ссылка неверная."); return end
if fs.exists(path) then
info("Файл уже существует, удаляю старый.")
fs.remove(path)
end
fs.makeDirectory(fs.path(path))
local file = io.open(path, "w")
for chunk in response do
file:write(chunk)
sContent = sContent .. chunk
end
file:close()
info("Файл загружен и находится в /"..path)
info(" ")
return sContent
end
--БЕЗОПАСНАЯ ЗАГРУЗОЧКА
local function getFromGitHubSafely(url, path)
local success, sRepos = pcall(getFromGitHub, url, path)
if not success then
io.stderr:write("Не удалось подключиться по данной ссылке. Вероятно, она неверная, либо отсутствует подключение к Интернету.")
return nil
end
return sRepos
end
if args[1] == "set" then
if fs.exists(pathToConfig) then fs.remove(pathToConfig) end
fs.makeDirectory(fs.path(pathToConfig))
config.write(pathToConfig, "currentRepository", args[2])
currentRepository = args[2]
info("Текущий репозиторий изменен на "..currentRepository)
elseif args[1] == "get" then
if not fs.exists(pathToConfig) then
io.stderr:write("Текущий репозиторий не установлен. Используйте \"github set <путь>\".")
else
currentRepository = config.readAll(pathToConfig).currentRepository
getFromGitHubSafely(userUrl .. currentRepository .. "/master/" .. args[2], args[3])
end
elseif args[1] == "fast" then
getFromGitHubSafely(userUrl .. args[2], args[3])
else
printUsage()
return
end