Advertisement
Rolcam

Computercraft Pastebin Updater

Aug 29th, 2023 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.60 KB | None | 0 0
  1. args = {...}
  2. --Variable Configuration
  3. -- 0 = pastebin override used - will not use config file | 1 = use config file for the pastebin code
  4. files = 0
  5. -- Pastebin file code
  6. codeTxt = " "
  7. prog = " "
  8. -- Various variables - x is for error checking
  9. x = 1
  10. y = 1
  11. z = 1
  12. if args[1] == "Help" or args[1] == "help" or args[1] == "HELP" then
  13.     term.clear()
  14.     term.setCursorPos(1,1)
  15.     term.setTextColor(colors.orange)
  16.     print("Pastebin Updater Help - Page 1")
  17.     print(" ")
  18.     term.setTextColor(colors.white)
  19.     print("Command Usage:")
  20.     print("update [file name] [pastebin code]")
  21.     print("The pastebin code is only needed if you want to use a different one than provided in the configuration file")
  22.     print(" ")
  23.     print("Press any key to continue")
  24.     os.pullEvent( 'key' )
  25.     term.clear()
  26.     term.setCursorPos(1,1)
  27.     term.setTextColor(colors.orange)
  28.     print("Pastebin Updater Help - Page 2")
  29.     print(" ")
  30.     term.setTextColor(colors.white)
  31.     print("If a config file isn't found for a program, the updater will make one and ask you to provide the pastebin code.")
  32.     print(" ")
  33.     print("The code is the text that comes after pastebin.com/")
  34.     print(" ")
  35.     print("If you incorrectly type the pastebin code, the only way to change it is to either edit the config file, or delete it and have the updater make a new one.")
  36.     print(" ")
  37.     print("Update files are named as \"program.pbu\"")
  38.     print("For example, the update file for the program \"test\" would be \"test.pbu\"")
  39.     print("Press any key to continue")
  40.     os.pullEvent( 'key' )
  41.     print("end of help segment")
  42.     x = 0
  43. end
  44.    
  45. if args[1] == nil then
  46.     x = 0
  47.     term.setTextColor(colors.red)
  48.     print("Error: Missing Parameter! File name is required!")
  49. else
  50.     prog = args[1]
  51. end
  52. if args[2] ~= nil then
  53.     print("Override parameter detected! Using this instead of config file.")
  54.     codeTxt = args[2]
  55. else
  56.     files = 1
  57. end
  58.  
  59.  
  60. -- Updater
  61. term.setTextColor(colors.white)
  62. if x == 1 then
  63.     term.clear()
  64.     term.setCursorPos(1,1)
  65.     -- checks if it is to use the config file or just use the override if provided
  66.     if files == 1 then
  67.         print("Checking for " .. prog .. "'s config file")
  68.         -- Checks if the config file exists. If it doesn't, it creates one
  69.         if not fs.exists(prog ..".pbu") then
  70.             y = 0
  71.             term.setTextColor(colors.red)
  72.             print("Warning: Config file not found!")
  73.             term.setTextColor(colors.white)
  74.             print("We will now create one for future reference")
  75.             -- Code Input Loop
  76.             while y == 0 do
  77.                 term.setTextColor(colors.white)
  78.                 print("Please enter the pastebin code we will use to update this program:")
  79.                 codeTxt = read()
  80.                 z = 0
  81.                 -- Code Confirmation Loop
  82.                 while z == 0 do
  83.                     term.setTextColor(colors.white)
  84.                     print(codeTxt .. " - Is this code correct? y/n")
  85.                     confirm = read()
  86.                     if confirm == "n" then
  87.                         z = 1
  88.                     elseif confirm == "y" then
  89.                         term.setTextColor(colors.green)
  90.                         print("Code confirmed!")
  91.                         term.setTextColor(colors.white)
  92.                         y = 1
  93.                         z = 1
  94.                     else
  95.                         term.setTextColor(colors.red)
  96.                         print("Invalid input! Please try again.")
  97.                     end
  98.                 end              
  99.             end
  100.         print("Generating file and updating the program")
  101.         config = fs.open(prog ..".pbu" , "w")
  102.         config.write(codeTxt)
  103.         config.close()
  104.         print("Config file generated! Now updating your program")
  105.         fs.delete(prog)
  106.         shell.run("pastebin get " .. codeTxt .. " " .. prog)
  107.         print("Program updated!")
  108.         else
  109.             print("Config file detected!")
  110.             sleep(1)
  111.             term.clear()
  112.             term.setCursorPos(1,1)
  113.             config = fs.open(prog .. ".pbu", "r")
  114.             codeTxt = config.readLine()
  115.             config.close()
  116.             print("Pastebin code obtained!")
  117.             print("Updating Program!")
  118.             fs.delete(prog)
  119.             shell.run("pastebin get " .. codeTxt .. " " .. prog)
  120.             print("Program updated!")
  121.         end
  122.     else
  123.         print("Updating progam using the override...")
  124.         fs.delete(prog)
  125.         shell.run("pastebin get " .. codeTxt .. " " .. prog)
  126.         print("Program updated!")
  127.     end
  128. else
  129.     print("Updater Terminated")
  130. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement