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

how to do my logic?

Started by IsaacTBeast, 30 May 2014 - 02:12 PM
IsaacTBeast #1
Posted 30 May 2014 - 04:12 PM
Hello guys! Can you help me make this logic? please :)/>

This is my logic..


apt-get add-repo http://www.example.com/packages/

apt-get add-repo http://www.example2.com/packages/


It sets all the link in lines..
This is will happen.

http://www.example.com –> Line 1 of config.cfg
http://www.example2.com –> Line 2 of config.cfg
~~~~Another Logic~~~~~

This is my another logic:


apt-get install example
apt-get install example2

This will happen if it runs the code:
apt-get install example <— config.cfg and finds the app from the repos.
apt-get install example2 <— dis-same as above.

~~~~Another Logic~~~

making a folder.

test.lua:


local sFile = args[1]

test.lua testfolder

and then the testfolder will go to /tmp/testfolder
Edited on 30 May 2014 - 02:19 PM
apemanzilla #2
Posted 30 May 2014 - 04:32 PM
…what are you trying to do, exactly?
IsaacTBeast #3
Posted 30 May 2014 - 05:31 PM
…what are you trying to do, exactly?

I am trying to make a package manager and remake apt-get from debian to ComputerCraft but this is for my program that I am making now :)/>
IsaacTBeast #4
Posted 30 May 2014 - 06:02 PM
This is what I do so far :)/>


local args = { ... }
local internet = {}
local installapplic = "/etc/applications"
local root = "/"
local function save(data,file)
		local file = shell.resolve(file)
		if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
				if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
				fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
		end
		local f = fs.open(file,"w")
		f.write(data)
		f.close()
end

local function download(url, file)
		save(http.get(url).readAll(),file)
end
function printUsage()
print("Usage: \n islx-app update --updates the config file. \n islx-app oldconfig --downloads the oldconfig. \n islx-app add-repo --to add your repo.")
end
if #args < 1 then
printUsage()
end
local sCommand = args[1]
local sAppfname = args[2]
local repolink = args[2]
if sCommand == "update" then
if fs.isDir("/tmp") then
  local fileH = http.get("https://raw.githubusercontent.com/iansapelino/islxcc/master/helloworld.lua")
  write(fileH.readAll())
  fileH.close()
  sleep(0.5)
  fs.delete("/tmp")
else
  fs.makeDir("/tmp")
  local fileH = http.get("https://raw.githubusercontent.com/iansapelino/islxcc/master/helloworld.lua")
  write(fileH.readAll())
  fileH.close()
  sleep(0.5)
  fs.delete("/tmp")
  end
end
if sCommand == "oldconfig" then
if fs.isDir("/etc") then
  if fs.exists("/etc/islxcc/islxcc.icfg") then
   fs.delete("/etc/islxcc/islxcc.icfg")
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Downloading the Old Config for the packages..")
	  sleep(0.5)
	  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
  else
   term.clear()
	  term.setCursorPos(1,1)
	  print("Downloading the Old Config for the packages..")
	  sleep(0.5)
	  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
  end
else
  fs.makeDir("/etc")
  fs.makeDir("/etc/islxcc/")
  term.clear()
  term.setCursorPos(1,1)
  print("Downloading the Old Config for the packages..")
  sleep(0.5)
  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
end
end
if sCommand == "add-repo" then
   fs.makeDir( "/etc/"..repolink )
end
Edited on 30 May 2014 - 04:03 PM
Bomb Bloke #5
Posted 31 May 2014 - 08:10 AM
At a glance, it looks like you've got an understanding of reading/writing files, creating directories and getting execution parameters already - I'm a little confused as to where exactly you're getting stuck?
IsaacTBeast #6
Posted 31 May 2014 - 09:01 AM
Can you guys help me?? I get an error called Unexpected String from number 91.

Heres my code.


local args = { ... }
local internet = {}
local installapplic = "/etc/applications"
local root = "/"
local etc = "/etc/applications/app"
--Credits to ApeManzilla
local function save(data,file)
	    local file = shell.resolve(file)
	    if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
			    if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
			    fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
	    end
	    local f = fs.open(file,"w")
	    f.write(data)
	    f.close()
end
--Credits to ApeManzilla
local function download(url, file)
	    save(http.get(url).readAll(),file)
end
--Credits to me.. xD
function printUsage()
print("Usage: \n islx-app update --updates the config file. \n islx-app oldconfig --downloads the oldconfig. \n islx-app add-repo --to add your repo.")
end
if #args < 1 then
printUsage()
end
--Main. This is what I create :)/>
local sCommand = args[1]
local sAppfname = args[2]
local repolink = args[2]
if sCommand == "update" then
if fs.isDir("/tmp") then
  local fileH = http.get("https://raw.githubusercontent.com/iansapelino/islxcc/master/helloworld.lua")
  write(fileH.readAll())
  fileH.close()
  sleep(0.5)
  fs.delete("/tmp")
else
  fs.makeDir("/tmp")
  local fileH = http.get("https://raw.githubusercontent.com/iansapelino/islxcc/master/helloworld.lua")
  write(fileH.readAll())
  fileH.close()
  sleep(0.5)
  fs.delete("/tmp")
  end
end
if sCommand == "oldconfig" then
if fs.isDir("/etc") then
  if fs.exists("/etc/islxcc/islxcc.icfg") then
   fs.delete("/etc/islxcc/islxcc.icfg")
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Downloading the Old Config for the packages..")
	  sleep(0.5)
	  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
  else
   term.clear()
	  term.setCursorPos(1,1)
	  print("Downloading the Old Config for the packages..")
	  sleep(0.5)
	  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
  end
else
  fs.makeDir("/etc")
  fs.makeDir("/etc/islxcc/")
  term.clear()
  term.setCursorPos(1,1)
  print("Downloading the Old Config for the packages..")
  sleep(0.5)
  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
end
end
if sCommand == "add-repo" then
if fs.exists(tostring(repolink)) then
  fs.makeDir(tostring(repolink))
	 fs.copy(tostring(repolink), "etc/"..args[2])
	 fs.delete(tostring(repolink))
else
  fs.makeDir(tostring(repolink))
	 fs.copy(tostring(repolink), "etc/"..args[2])
	 fs.delete(tostring(repolink))
end
end
if sCommand == "install" then
download("https://raw.githubusercontent.com/iansapelino/islxcc/master/Programs/"..args[2], ..args[4])
end
Lyqyd #7
Posted 31 May 2014 - 11:23 AM
Threads merged. Please stick to one topic for all questions about a given piece of code.
IsaacTBeast #8
Posted 31 May 2014 - 11:36 AM
At a glance, it looks like you've got an understanding of reading/writing files, creating directories and getting execution parameters already - I'm a little confused as to where exactly you're getting stuck?

This is the one……… I am stuck at downloading install using a argument..

Can you guys help me?? I get an error called Unexpected String from number 91.

Heres my code.


local args = { ... }
local internet = {}
local installapplic = "/etc/applications"
local root = "/"
local etc = "/etc/applications/app"
--Credits to ApeManzilla
local function save(data,file)
		local file = shell.resolve(file)
		if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
				if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
				fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
		end
		local f = fs.open(file,"w")
		f.write(data)
		f.close()
end
--Credits to ApeManzilla
local function download(url, file)
		save(http.get(url).readAll(),file)
end
--Credits to me.. xD
function printUsage()
print("Usage: \n islx-app update --updates the config file. \n islx-app oldconfig --downloads the oldconfig. \n islx-app add-repo --to add your repo.")
end
if #args < 1 then
printUsage()
end
--Main. This is what I create :)/>/>/>
local sCommand = args[1]
local sAppfname = args[2]
local repolink = args[2]
if sCommand == "update" then
if fs.isDir("/tmp") then
  local fileH = http.get("https://raw.githubusercontent.com/iansapelino/islxcc/master/helloworld.lua")
  write(fileH.readAll())
  fileH.close()
  sleep(0.5)
  fs.delete("/tmp")
else
  fs.makeDir("/tmp")
  local fileH = http.get("https://raw.githubusercontent.com/iansapelino/islxcc/master/helloworld.lua")
  write(fileH.readAll())
  fileH.close()
  sleep(0.5)
  fs.delete("/tmp")
  end
end
if sCommand == "oldconfig" then
if fs.isDir("/etc") then
  if fs.exists("/etc/islxcc/islxcc.icfg") then
   fs.delete("/etc/islxcc/islxcc.icfg")
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Downloading the Old Config for the packages..")
	  sleep(0.5)
	  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
  else
   term.clear()
	  term.setCursorPos(1,1)
	  print("Downloading the Old Config for the packages..")
	  sleep(0.5)
	  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
  end
else
  fs.makeDir("/etc")
  fs.makeDir("/etc/islxcc/")
  term.clear()
  term.setCursorPos(1,1)
  print("Downloading the Old Config for the packages..")
  sleep(0.5)
  download("https://raw.githubusercontent.com/iansapelino/islxcc/master/islxcc.icfg", "/etc/islxcc/islxcc.icfg")
end
end
if sCommand == "add-repo" then
if fs.exists(tostring(repolink)) then
  fs.makeDir(tostring(repolink))
	 fs.copy(tostring(repolink), "etc/"..args[2])
	 fs.delete(tostring(repolink))
else
  fs.makeDir(tostring(repolink))
	 fs.copy(tostring(repolink), "etc/"..args[2])
	 fs.delete(tostring(repolink))
end
end
if sCommand == "install" then
download("https://raw.githubusercontent.com/iansapelino/islxcc/master/Programs/"..args[2], ..args[2])
end

This is the code.. :)/>

I got stuck at islx-app install because it says that unexpected string at number line 91.
Edited on 31 May 2014 - 09:36 AM
Bomb Bloke #9
Posted 31 May 2014 - 12:11 PM
Line 91 in what file? The code you've posted doesn't have that many lines. What's the full error?
GamerNebulae #10
Posted 31 May 2014 - 12:53 PM
Maybe post it on Pastebin… Then we can see syntax and line numbers.
IsaacTBeast #11
Posted 31 May 2014 - 01:07 PM
Here you go http://pastebin.com/d8KczPYV
IsaacTBeast #12
Posted 31 May 2014 - 01:47 PM
any replies?
Bomb Bloke #13
Posted 31 May 2014 - 02:17 PM
Line 91 is blank in that version. Again, please provide the full error.
IsaacTBeast #14
Posted 31 May 2014 - 02:58 PM
Line 91 is blank in that version. Again, please provide the full error.

Thanks but I think I fixed it now I changed it.. and look at the link again..

But the error
line 92 attempt to index ? (a nil value)
Edited on 31 May 2014 - 01:06 PM
GamerNebulae #15
Posted 31 May 2014 - 07:00 PM
Line 91 is blank in that version. Again, please provide the full error.

Thanks but I think I fixed it now I changed it.. and look at the link again..

But the error
line 92 attempt to index ? (a nil value)

It appears that args[2] seems to be a nil value. In other words: args[2] has no value.
Dog #16
Posted 31 May 2014 - 07:33 PM
I noticed you only do a sanity check for 1 argument but then require more than one. Is it possible you are not passing the second argument, thus resulting in args[2] being nil?

EDIT: ninja'd by GamerNebulae
Edited on 31 May 2014 - 08:14 PM
GamerNebulae #17
Posted 31 May 2014 - 09:05 PM
I noticed you only do a sanity check for 1 argument but then require more than one. Is it possible you are not passing the second argument, thus resulting in args[2] being nil? I also noticed that you're calling 'http.get' with a write. Should the 'get' be 'post'? (I'm not sure, apologies if I'm misleading you here).

As I tried to clarify above your post, lol. What I and Dog are trying to say is this.

--If you typed this in your Computer:
<program title> variable1 variable2
In your program, variable1 is args[1] and variable2 is args[2]. If you are not planning to add more commands, then you should change the code so it checks if args[1] is "add-repo" or "oldconfig". Something like this:

if args[1] == "oldconfig" then
  sCommand = args[1]
  <actions>
elseif args[1] == "add-repo" then
  sCommand = args[1]
  <actions>
end
Dog #18
Posted 31 May 2014 - 10:19 PM
As I tried to clarify above your post, lol. What I and Dog are trying to say is this.

--If you typed this in your Computer:
<program title=""> variable1 variable2
In your program, variable1 is args[1] and variable2 is args[2]. If you are not planning to add more commands, then you should change the code so it checks if args[1] is "add-repo" or "oldconfig". Something like this:

if args[1] == "oldconfig" then
  sCommand = args[1]
  <actions>
elseif args[1] == "add-repo" then
  sCommand = args[1]
  <actions>
end

That's not what I was trying to say. Allow me to clarify a bit…

Your clarification (second post) mentions args[2], but it doesn't really address or explain the nil value of args[2]. My question was whether IsaacTBeast is even passing a second argument to the program. Currently the program only checks to see if the first argument was passed, but is complaining that the second argument is nil. There's no sanity check for the second argument, so if one is never provided it will be nil from the get-go.

Looking at the code, I can only see one reason why args[2] would be nil (thus causing the error IsaacTBeast is encountering): The second argument isn't being passed to the program.

No amount of tinkering with args[1] is going to fix that.

My theory can easily be verified by changing line 27 from

if #args < 1 then

to

if #args < 2 then

If the second argument isn't being passed, the usage screen will be displayed instead of the program continuing.

IsaacTBeast - would you please post the exact command you are using to launch the program and (for good measure) verify that the file actually exists on Github.
Edited on 31 May 2014 - 08:23 PM
GamerNebulae #19
Posted 31 May 2014 - 10:48 PM
As I tried to clarify above your post, lol. What I and Dog are trying to say is this.

--If you typed this in your Computer:
<program title=""> variable1 variable2
In your program, variable1 is args[1] and variable2 is args[2]. If you are not planning to add more commands, then you should change the code so it checks if args[1] is "add-repo" or "oldconfig". Something like this:

if args[1] == "oldconfig" then
  sCommand = args[1]
  <actions>
elseif args[1] == "add-repo" then
  sCommand = args[1]
  <actions>
end

That's not what I was trying to say. Allow me to clarify a bit…

Your clarification (second post) mentions args[2], but it doesn't really address or explain the nil value of args[2]. My question was whether IsaacTBeast is even passing a second argument to the program. Currently the program only checks to see if the first argument was passed, but is complaining that the second argument is nil. There's no sanity check for the second argument, so if one is never provided it will be nil from the get-go.

Looking at the code, I can only see one reason why args[2] would be nil (thus causing the error IsaacTBeast is encountering): The second argument isn't being passed to the program.

No amount of tinkering with args[1] is going to fix that.

My theory can easily be verified by changing line 27 from

if #args < 1 then

to

if #args < 2 then

If the second argument isn't being passed, the usage screen will be displayed instead of the program continuing.

IsaacTBeast - would you please post the exact command you are using to launch the program and (for good measure) verify that the file actually exists on Github.

Yeah, that was sorta my explanation, but yours is way better… ;)/>
Bomb Bloke #20
Posted 01 June 2014 - 03:32 AM
92:

app.write(http.get( "https://raw.githubusercontent.com/iansapelino/islxcc/master/Programs/"..args[2] ).readAll())

You guys are misreading the error - an "attempt to index nil" doesn't mean that something like args[2] is nil, it means that something like args is nil. That can't be true, so presumably it doesn't like the attempt to index "readAll" from within "http.get()". Or "get" from within "http".

This could mean the http API isn't enabled, or that there's some other problem with the attempt to get the website handle.

It's a bad idea to do the "get" and the "read" on one line anyway. That means you don't retain the handle, which you should be closing after the read (impossible if you didn't keep the pointer):

local webHandle = http.get( "https://raw.githubusercontent.com/iansapelino/islxcc/master/Programs/"..args[2] )
app.write(webHandle.readAll())
webHandle.close()

Though I'm still suspecting the error is happening within some API this script is calling - I'd expect the full error to include information about the name of the file within which the error occurred.
IsaacTBeast #21
Posted 01 June 2014 - 12:10 PM
92:

app.write(http.get( "https://raw.githubusercontent.com/iansapelino/islxcc/master/Programs/"..args[2] ).readAll())

You guys are misreading the error - an "attempt to index nil" doesn't mean that something like args[2] is nil, it means that something like args is nil. That can't be true, so presumably it doesn't like the attempt to index "readAll" from within "http.get()". Or "get" from within "http".

This could mean the http API isn't enabled, or that there's some other problem with the attempt to get the website handle.

It's a bad idea to do the "get" and the "read" on one line anyway. That means you don't retain the handle, which you should be closing after the read (impossible if you didn't keep the pointer):

local webHandle = http.get( "https://raw.githubusercontent.com/iansapelino/islxcc/master/Programs/"..args[2] )
app.write(webHandle.readAll())
webHandle.close()

Though I'm still suspecting the error is happening within some API this script is calling - I'd expect the full error to include information about the name of the file within which the error occurred.

THANK YOU!! :D/> and God Bless :)/>
Bomb Bloke #22
Posted 01 June 2014 - 12:35 PM
Wait, what? That wasn't supposed to fix your specific error… :blink:/>
IsaacTBeast #23
Posted 01 June 2014 - 12:42 PM
Wait, what? That wasn't supposed to fix your specific error… :blink:/>

WHAT?!?!! xD… but thanks if thats not the answer.. but how do you if the application idoesn't exist in the repository?
Bomb Bloke #24
Posted 01 June 2014 - 01:01 PM
Well, if you wanted to check if the http.get() request was successful, you could do:

local webHandle = http.get( "https://raw.githubusercontent.com/iansapelino/islxcc/master/Programs/"..args[2] )

if webHandle then
  app.write(webHandle.readAll())
  webHandle.close()
else error("Couldn't find "..args[2].." in the repo.") end