Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Created September 12, 2019 09:45
Show Gist options
  • Save DarkWiiPlayer/54fc982fe2bf6475bd0fe8344caaea33 to your computer and use it in GitHub Desktop.
Save DarkWiiPlayer/54fc982fe2bf6475bd0fe8344caaea33 to your computer and use it in GitHub Desktop.
local lfs = require 'lfs'
local commands = {}
setmetatable(commands, {__call = function(self, name, func, description)
if func then
self[name] = func
table.insert(self, {name=name, description=description})
else
for index, command in ipairs(self) do
print(command.name .. ': ' .. command.description)
end
end
end})
return commands
#!/usr/bin/env lua
require 'tasks'
local commands = require 'commands'
local command = commands[...]
if not command then
commands()
else
command(...)
end
require 'commands' ('run', function(name, task, ...)
if task == 'list' then
-- TODO: Check if directory exists
-- TODO: Tasks in folders
for file in lfs.dir('tasks') do
if file:sub(1,1)~='.' then
print((file:gsub('.lua$', '')))
end
end
elseif task then
local file = io.open('tasks/'..task..'.lua')
if not file then error("Task "..task.." not found!") end
local code = load(file:read('*a'))
file:close()
return code(...)
else
print(name..' [task]: Runs a given task')
end
end, 'Runs a task')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment