|
| 1 | +module mcl.main; |
| 2 | + |
1 | 3 | import std.stdio : writefln, writeln, stderr; |
2 | 4 | import std.array : replace; |
3 | 5 | import std.getopt : getopt; |
4 | 6 | import std.logger : infof, errorf, LogLevel; |
5 | | - |
| 7 | +import std.sumtype : SumType, match; |
| 8 | +import std.string : stripRight, stripLeft; |
| 9 | +import std.algorithm : endsWith; |
| 10 | +import std.format : format; |
6 | 11 | import mcl.utils.path : rootDir; |
7 | 12 | import mcl.utils.tui : bold; |
8 | 13 |
|
9 | | -import cmds = mcl.commands; |
| 14 | +import mcl.commands; |
10 | 15 |
|
11 | | -alias supportedCommands = imported!`std.traits`.AliasSeq!( |
12 | | - cmds.get_fstab, |
13 | | - cmds.deploy_spec, |
14 | | - cmds.ci_matrix, |
15 | | - cmds.print_table, |
16 | | - cmds.shard_matrix, |
17 | | - cmds.host_info, |
18 | | - cmds.ci, |
19 | | - cmds.machine, |
20 | | - cmds.config, |
21 | | -); |
| 16 | +import argparse; |
22 | 17 |
|
23 | | -int main(string[] args) |
| 18 | + |
| 19 | +@(Command(" ").Description(" ")) |
| 20 | +struct unknown_command_args {} |
| 21 | +int unknown_command(unknown_command_args unused) |
24 | 22 | { |
25 | | - if (args.length < 2) |
26 | | - return wrongUsage("no command selected"); |
| 23 | + stderr.writeln("Unknown command. Use --help for a list of available commands."); |
| 24 | + return 1; |
| 25 | +} |
27 | 26 |
|
28 | | - string cmd = args[1]; |
29 | | - LogLevel logLevel = LogLevel.info; |
30 | | - args.getopt("log-level", &logLevel); |
| 27 | +template genSubCommandArgs() |
| 28 | +{ |
| 29 | + const char[] genSubCommandArgs = |
| 30 | + "@SubCommands\n"~ |
| 31 | + "SumType!("~ |
| 32 | + "Default!unknown_command_args"~ |
| 33 | + ") cmd;"; |
| 34 | +} |
31 | 35 |
|
32 | | - setLogLevel(logLevel); |
| 36 | +template genSubCommandMatch() |
| 37 | +{ |
| 38 | + const char[] generateMatchString = () { |
| 39 | + alias CmdTypes = typeof(MCLArgs.cmd).Types; |
| 40 | + string match = "int result = args.cmd.match!("; |
33 | 41 |
|
34 | | - infof("Git root: '%s'", rootDir.bold); |
| 42 | + static foreach (CmdType; CmdTypes) |
| 43 | + {{ |
| 44 | + string name = CmdType.stringof.replace("Default!(", "").stripRight(")"); |
| 45 | + match ~= format("\n\t(%s a) => %s(a)", name, name.replace("_args", "")) ~ ", "; |
| 46 | + }} |
| 47 | + match = match.stripRight(", "); |
| 48 | + match ~= "\n);"; |
35 | 49 |
|
36 | | - try switch (cmd) |
37 | | - { |
38 | | - default: |
39 | | - return wrongUsage("unknown command: `" ~ cmd ~ "`"); |
| 50 | + return match; |
| 51 | + }(); |
| 52 | +} |
40 | 53 |
|
41 | | - static foreach (command; supportedCommands) |
42 | | - case __traits(identifier, command): |
43 | | - { |
| 54 | +struct MCLArgs |
| 55 | +{ |
| 56 | + @NamedArgument(["log-level"]) |
| 57 | + LogLevel logLevel = cast(LogLevel)-1; |
| 58 | + mixin(genSubCommandArgs!()); |
| 59 | +} |
| 60 | + |
| 61 | +mixin CLI!MCLArgs.main!((args) |
| 62 | +{ |
| 63 | + static assert(is(typeof(args) == MCLArgs)); |
| 64 | + |
| 65 | + LogLevel logLevel = LogLevel.info; |
| 66 | + if (args.logLevel != cast(LogLevel)-1) |
| 67 | + logLevel = args.logLevel; |
| 68 | + setLogLevel(logLevel); |
| 69 | + |
| 70 | + mixin genSubCommandMatch; |
44 | 71 |
|
45 | | - infof("Running %s task", cmd.bold); |
46 | | - command(args[2..$]); |
47 | | - infof("Execution Succesfull"); |
48 | 72 | return 0; |
49 | | - } |
50 | | - } |
51 | | - catch (Exception e) |
52 | | - { |
53 | | - errorf("Task %s failed. Error:\n%s", cmd.bold, e); |
54 | | - return 1; |
55 | | - } |
56 | | -} |
| 73 | +}); |
57 | 74 |
|
58 | 75 | void setLogLevel(LogLevel l) |
59 | 76 | { |
60 | 77 | import std.logger : globalLogLevel, sharedLog; |
61 | 78 | globalLogLevel = l; |
62 | 79 | (cast()sharedLog()).logLevel = l; |
63 | 80 | } |
64 | | - |
65 | | -int wrongUsage(string error) |
66 | | -{ |
67 | | - writefln("Error: %s.", error); |
68 | | - writeln("Usage:\n"); |
69 | | - static foreach (cmd; supportedCommands) |
70 | | - writefln(" mcl %s", __traits(identifier, cmd)); |
71 | | - |
72 | | - return 1; |
73 | | -} |
|
0 commit comments