This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sivarias's profile picture

Creating/reading From Tables For Shell.run()

Started by Sivarias, 10 October 2013 - 12:58 PM
Sivarias #1
Posted 10 October 2013 - 02:58 PM
I have the following code that I can download from pastebin and then run to get all of the code I have on pastebin so I'm not typing up a lot of random characters. I know the code works, but I'm wondering you guys would clean it up and then utilize it for easier code?

    --This is for GAME STARTUP ONLY!! and will download all codes on pastebin
    shell.run("delete", "startup")
    shell.run("delete", "!MissionControl")
    shell.run("delete", "!refuel")
    shell.run("delete", "!tunnel")
    shell.run("delete", "!message")
    shell.run("delete", "turtlestartup")
    shell.run("delete", "!chat")
    shell.run("pastebin get", "Z5jC3sV3", "startup")
    shell.run("pastebin get", "sVY4Px6t", "!MissionControl")
    shell.run("pastebin get", "dqDwAsN4", "!refuel")
    shell.run("pastebin get", "iZVtZTg7", "!tunnel")
    shell.run("pastebin get", "SFvmNy2c", "!message")
    shell.run("pastebin get", "eN3UcXK0", "!chat")
    shell.run("pastebin get", "GeBw9Ycg", "turtlestartup")

LBPHacker #2
Posted 10 October 2013 - 03:07 PM
Have this (pastebin get aAdRKvzB installer). Should work. Not tested. If it gives you an attempt to call nil on line 4, that means I messed up the URLs.

BTW, I don't like seeing people using shell.run for everything.
AgentE382 #3
Posted 11 October 2013 - 11:52 AM
Using my Pastebin API saved in a file named "pb":

os.unloadAPI "pb"
os.loadAPI "pb"

local pastes = {
	Z5jC3sV3 = "startup",
	sVY4Px6t = "!MissionControl",
	dqDwAsN4 = "!refuel",
	iZVtZTg7 = "!tunnel",
	SFvmNy2c = "!message",
	eN3UcXK0 = "!chat",
	GeBw9Ycg = "turtlestartup"
}

for k,v in pairs(pastes) do
	local paste = pb.get(k)
	if paste then
		local file, err = io.open(v, "w")
		if file then
			file:write(paste)
			file:close()
		else
			printError(v,": ",err)
		end
	else
		printError("Failed to download '",v,"'with pasteID '",k,"'!")
	end
end
You can add additional pastes to the table. No need for quotes around the pasteIDs unless they start with a number. Then, you'll need to do
["1234id"] = "filename"
Note that the filepaths are absolute. Also note that there is no need to delete the previous versions before getting the new ones. You can use this as-is as long as you have my API.
LBPHacker #4
Posted 11 October 2013 - 12:37 PM
-snip-
You know, installers are supposed to be standalone.
AgentE382 #5
Posted 11 October 2013 - 02:48 PM
Then he can just copy/paste this code instead of loading the API:
function pb.get(api_paste_key)
	assert(type(api_paste_key) == "string", "Enter a valid paste key as a string!")
	local response = http.get("http://pastebin.com/raw.php?i="..url_encode(api_paste_key))
	return response and response.readAll()
end
I wasn't looking at it as an installer, but I guess that's really what's needed. Well, that can be my next project.