require = requireInjector(getfenv(1)) local Event = require('event') local UI = require('ui') local Socket = require('socket') local Config = require('config') local GROUPS_PATH = 'usr/groups' local SCRIPTS_PATH = 'usr/scripts' multishell.setTitle(multishell.getCurrent(), 'Script') UI:configure('script', ...) local config = { showGroups = false, variables = [[{ COMPUTER_ID = os.getComputerID(), }]], } Config.load('script', config) local width = math.floor(UI.term.width / 2) - 1 if UI.term.width % 2 ~= 0 then width = width + 1 end function processVariables(script) local fn = loadstring('return ' .. config.variables) if fn then local variables = fn() for k,v in pairs(variables) do local token = string.format('{%s}', k) script = script:gsub(token, v) end end return script end function invokeScript(computer, scriptName) local script = Util.readFile(scriptName) if not script then print('Unable to read script file') end local socket = Socket.connect(computer.id, 161) if not socket then print('Unable to connect to ' .. computer.id) return end script = processVariables(script) Util.print('Running %s on %s', scriptName, computer.label) socket:write({ type = 'script', args = script }) --[[ local response = socket:read(2) if response and response.result then if type(response.result) == 'table' then print(textutils.serialize(response.result)) else print(tostring(response.result)) end else printError('No response') end --]] socket:close() end function runScript(computerOrGroup, scriptName) if computerOrGroup.id then invokeScript(computerOrGroup, scriptName) else local list = computerOrGroup.list if computerOrGroup.path then list = Util.readTable(computerOrGroup.path) end if list then for _,computer in pairs(list) do invokeScript(computer, scriptName) end end end end local function getActiveComputers(t) t = t or { } Util.clear(t) for k,computer in pairs(_G.network) do if computer.active then t[k] = computer end end return t end local function getTurtleList() local turtles = { label = 'Turtles', list = { }, } for k,computer in pairs(getActiveComputers()) do if computer.fuel then turtles.list[k] = computer end end return turtles end local args = { ... } if #args == 2 then local key = args[1] local script = args[2] local target if tonumber(key) then target = _G.network[tonumber(key)] elseif key == 'All' then target = { list = Util.shallowCopy(getActiveComputers()), } elseif key == 'Localhost' then target = { id = os.getComputerID() } elseif key == 'Turtles' then target = getTurtleList() else target = Util.readTable(fs.combine(GROUPS_PATH, key)) end if not target then error('Syntax: Script