Advertisement
birdini

Turtle swarm git setup

Jul 6th, 2025 (edited)
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. --Package everything into one file so we don't have to rely on imports.
  2.  
  3. --[[BEGIN STRINGUTILS API]]
  4. function splitStr(inputStr,delim)
  5.     local pieces = {}
  6.     for str in string.gmatch(inputStr,"([^"..delim.."]+)") do
  7.         table.insert(pieces,str)
  8.     end
  9.     return pieces
  10. end
  11.  
  12. --[[BEGIN FILEUTILS API]]
  13. function exists(path)
  14.     return fs.exists(path)
  15. end
  16.  
  17. function writeString(data,path)
  18.     local file = io.open(path,"w")
  19.     file:write(tostring(data))
  20.     file:close()
  21. end
  22.  
  23. function createFile(path)
  24.     local pieces = splitStr(path,"/")
  25.     local appended = ""
  26.     for i=1,#pieces-1 do
  27.         appended = appended..pieces[i].."/"
  28.     end
  29.     if not (fs.exists(appended)) then
  30.         fs.makeDir(appended)
  31.     end
  32.     writeString("",path)
  33. end
  34.  
  35. function readString(path)
  36.     local file = io.open(path,"r")
  37.     data = file:read("*a")
  38.     file:close()
  39.     return data
  40. end
  41.  
  42. --[[BEGIN GITHUB API]]
  43. local baseURL = "https://raw.githubusercontent.com/lewibt01/ComputerCraftScripts/master/"
  44.  
  45. function requestFile(name)
  46.     local handle = http.get(baseURL..name)
  47.     local result = ""
  48.  
  49.     if(handle) then
  50.         result = handle.readAll()
  51.     end
  52.  
  53.     return result
  54. end
  55.  
  56. function pull(name,dest)
  57.     local data = requestFile(name)
  58.     createFile(dest)
  59.     local file = io.open(dest,"w")
  60.     file:write(data)
  61.     file:close()
  62. end
  63.  
  64. --[[BEGIN PROPERTIES API]]
  65. local props = {}
  66. local propFilePath = "/usr/apis.prop"
  67.  
  68. function getAll()
  69.     local propFile = readString(propFilePath)
  70.    
  71.     --make sure the property file wasn't empty
  72.     if(propFile == "") then return {} end
  73.  
  74.     local lines = splitStr(propFile,"\n")
  75.  
  76.     local propTable = {}
  77.     for i=1,#lines do
  78.         local pair = splitStr(lines[i],":")
  79.         propTable[pair[1]] = pair[2]
  80.     end
  81.     return propTable
  82. end
  83.  
  84. --[[BEGIN EXECUTION]]
  85. --get programs file
  86. print("Retrieving programs...")
  87. pull("PropertyFiles/swarmSetup.prop",propFilePath)
  88.  
  89. --programs will be in the format <git url>:<destination path>
  90. local programs = getAll()
  91. if(exists(propFilePath)) then
  92.     print(#programs)
  93.     --pull down all the programs into their respective folders, overwrite existing files
  94.     for k,v in pairs(programs) do
  95.         print("Downloading "..k.." to "..v)
  96.         pull(k,v)
  97.     end
  98.  
  99.     print("Rebooting")
  100.     os.sleep(2)
  101.     os.reboot()
  102. else
  103.     print(propFilePath.." is missing.")
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement