-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdd.lua
executable file
·68 lines (58 loc) · 1.89 KB
/
mdd.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local modellib = require "model"
local argparse = require "argparse"
local commands = {
init = require "commands.init",
project = require "commands.project",
start = require "commands.start",
stop = require "commands.stop",
checkpoint = require "commands.checkpoint",
report = require "commands.report",
pause = require "commands.pause",
resume = require "commands.resume",
log = require "commands.log",
archive = require "commands.archive",
edit = require "commands.edit",
["list-projects"] = require "commands.list_projects",
}
local noop = function() end
local parser = argparse()
local function run_with_model(command_impl, parsed_args)
local model, err = modellib.make_model(parsed_args.force_project)
if not model then
return nil, err
end
local ok, err = command_impl(model, parsed_args)
if not ok then
return nil, err
end
return model:save()
end
local function run_without_model(command_impl, parsed_args)
return command_impl(parsed_args)
end
local function main(args)
parser:command_target("command")
parser:option("-p --project", "Optional project path"):target("force_project")
for command_name, command_module in pairs(commands) do
local configure = command_module.configure or noop
configure(parser:command(command_name))
local cmd_actions = command_module.actions or {}
for k, v in pairs(cmd_actions) do
modellib.register_action(k, v)
end
end
local parsed_args = parser:parse(args)
local command_name = parsed_args.command
local command_impl = commands[command_name].run
local needs_model = commands[command_name].needs_model
local run_cmd = (needs_model == false)
and run_without_model
or run_with_model
local ok, err = run_cmd(command_impl, parsed_args)
if not ok then
print("Command failed: " .. (err or "Unknown error"))
print(parser:get_usage())
os.exit(1)
end
end
return main(arg or {...})