Skip to content

Instantly share code, notes, and snippets.

@exerro
Created March 13, 2021 17:52
Show Gist options
  • Save exerro/2de16bc75c3c489ee156ff0d8a3f0314 to your computer and use it in GitHub Desktop.
Save exerro/2de16bc75c3c489ee156ff0d8a3f0314 to your computer and use it in GitHub Desktop.
local args = { ... }
local config
local CONFIG_PATH = ".ccsync.conf.lua"
local ERR_CONFIG_FAIL = "Failed to open CCSync config file"
local CONFIG_KEY_URL = "basic:url"
local CONFIG_KEY_LOCALPATH = "basic:localpath"
local PROMPT_URL = "URL:"
local PROMPT_LOCALPATH = "Local path:"
local HELP_URL = "The URL or IP of the CCSync server, including the port, with no protocol (no http://). E.g. 192.168.0.16:3781"
local HELP_LOCALPATH = "The local path to download files to."
local function printUsage(params, desc)
term.setTextColour(colours.grey)
write("- ")
term.setTextColour(colours.yellow)
write("ccsync ")
term.setTextColour(colours.cyan)
write(params)
term.setTextColour(colours.grey)
write(" - ")
term.setTextColour(colours.lightGrey)
print(desc)
end
local function printHelp(mode)
if mode == "connect" then
term.setTextColour(colours.lightGrey)
print("A failed connection can be caused by many things. Make sure that...")
-- who needs functions or loops when you can handwrite it!
term.setTextColour(colours.grey) write("- ") term.setTextColour(colours.cyan) print("you are connected to the internet")
term.setTextColour(colours.grey) write("- ") term.setTextColour(colours.cyan) print("the URL/IP is whitelisted (or not blacklisted)")
term.setTextColour(colours.grey) write("- ") term.setTextColour(colours.cyan) print("a CCSync server is running at the specified URL/IP")
term.setTextColour(colours.grey) write("- ") term.setTextColour(colours.cyan) print("portforwarding is set up correctly, if required")
print()
term.setTextColour(colours.lightGrey)
print("If you are running a server yourself on a personal computer, you likely need to portforward your router. Search online for instructions.")
else
term.setTextColour(colours.lightGrey)
print("CCSync allows syncing files between this computer in a specified directory and a remote computer running a CCSync server. The files are downloaded when this program is run. Settings such as URL and local path are cached and may be entered just once.")
term.setTextColour(colours.grey)
print("Usage:")
printUsage("[<url> <local path>]", "download files")
printUsage("help [connect]", "show help")
end
end
local function getInput(prompt, helpText)
print(prompt .. " ('help' for info)")
local input = read()
if input == "help" then
print(helpText)
return getInput(prompt, helpText)
end
return input
end
if args[1] == "help" or args[1] == "-h" or args[1] == "--help" then
printHelp(args[2])
return
end
if not fs.exists(CONFIG_PATH) then
config = {}
else
local h = io.open(CONFIG_PATH, "r") or error(ERR_CONFIG_FAIL, 0)
local content = h:read "*a"
h:close()
config = textutils.unserialize(content)
if type(config) ~= "table" then
printError("WARNING: CCSync config is invalid and will be overwritten")
config = {}
end
end
config[CONFIG_KEY_URL] = args[1] or config[CONFIG_KEY_URL] or getInput(PROMPT_URL, HELP_URL)
config[CONFIG_KEY_LOCALPATH] = args[2] or config[CONFIG_KEY_LOCALPATH] or getInput(PROMPT_LOCALPATH, HELP_LOCALPATH)
do
local confText = textutils.serialize(config)
local h = io.open(CONFIG_PATH, "w")
if h then
h:write(confText)
h:close()
end
end
local url = config[CONFIG_KEY_URL]
local path = config[CONFIG_KEY_LOCALPATH]
term.setTextColour(colours.lightGrey)
write("Downloading files from ")
term.setTextColour(colours.cyan)
write(url)
term.setTextColour(colours.lightGrey)
write(" to ")
term.setTextColour(colours.yellow)
print(path)
local h = http.get("http://" .. url) or error("Failed to get response from server. Run 'ccsync help connect' for more info.", 0)
local content = h.readAll()
h.close()
local ok, files = pcall(textutils.unserialize, content)
local len = 0
if not ok then
error("Failure decoding file list: " .. files, 0)
end
for k, v in pairs(files) do
local p = fs.combine(path, k)
local h = io.open(p, "w") or error("Failed to open local file '" .. p .. "'", 0)
h:write(v)
h:close()
len = len + 1
end
term.setTextColour(colours.green)
write"Success! "
term.setTextColour(colours.lightGrey)
print("Downloaded " .. len .. " files.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment