-
Notifications
You must be signed in to change notification settings - Fork 5
feat/mcl/jcli #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat/mcl/jcli #235
Changes from all commits
63aa2bc
9b08e5a
655cb3e
edb705b
6f79657
e04a7dd
10f9f35
a8f72d9
8f652d4
f5f1d52
c7628d8
f67558b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ runs: | |
CACHIX_AUTH_TOKEN: ${{ inputs.cachix-auth-token }} | ||
PRECALC_MATRIX: ${{ inputs.precalc_matrix }} | ||
MCL_BRANCH: ${{ github.repository == 'metacraft-labs/nixos-modules' && github.sha || 'main' }} | ||
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl print_table | ||
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl print-table | ||
|
||
- name: Update GitHub Comment | ||
uses: marocchino/[email protected] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,89 @@ | ||
module mcl.main; | ||
|
||
import std.stdio : writefln, writeln, stderr; | ||
import std.array : replace; | ||
import std.getopt : getopt; | ||
import std.logger : infof, errorf, LogLevel; | ||
|
||
import std.sumtype : SumType, match; | ||
import std.string : stripRight, stripLeft; | ||
import std.algorithm : endsWith; | ||
import std.format : format; | ||
import mcl.utils.path : rootDir; | ||
import mcl.utils.tui : bold; | ||
|
||
import cmds = mcl.commands; | ||
import mcl.commands; | ||
|
||
alias supportedCommands = imported!`std.traits`.AliasSeq!( | ||
cmds.get_fstab, | ||
cmds.deploy_spec, | ||
cmds.ci_matrix, | ||
cmds.print_table, | ||
cmds.shard_matrix, | ||
cmds.host_info, | ||
cmds.ci, | ||
cmds.machine, | ||
cmds.config, | ||
); | ||
import argparse; | ||
|
||
int main(string[] args) | ||
{ | ||
if (args.length < 2) | ||
return wrongUsage("no command selected"); | ||
|
||
string cmd = args[1]; | ||
LogLevel logLevel = LogLevel.info; | ||
args.getopt("log-level", &logLevel); | ||
@(Command(" ").Description(" ")) | ||
struct unknown_command_args {} | ||
int unknown_command(unknown_command_args unused) | ||
{ | ||
stderr.writeln("Unknown command. Use --help for a list of available commands."); | ||
return 1; | ||
} | ||
|
||
setLogLevel(logLevel); | ||
template genSubCommandArgs() | ||
{ | ||
const char[] genSubCommandArgs = | ||
"@SubCommands\n"~ | ||
"SumType!("~ | ||
"get_fstab_args,"~ | ||
"deploy_spec_args,"~ | ||
"host_info_args,"~ | ||
"config_args,"~ | ||
"machine_args,"~ | ||
"ci_matrix_args,"~ | ||
"ci_args,"~ | ||
"print_table_args,"~ | ||
"shard_matrix_args,"~ | ||
"Default!unknown_command_args"~ | ||
") cmd;"; | ||
Comment on lines
+29
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be done in a similar fashion to the following |
||
} | ||
|
||
infof("Git root: '%s'", rootDir.bold); | ||
template genSubCommandMatch() | ||
{ | ||
const char[] generateMatchString = () { | ||
alias CmdTypes = typeof(MCLArgs.cmd).Types; | ||
string match = "int result = args.cmd.match!("; | ||
|
||
try switch (cmd) | ||
{ | ||
default: | ||
return wrongUsage("unknown command: `" ~ cmd ~ "`"); | ||
static foreach (CmdType; CmdTypes) | ||
{{ | ||
string name = CmdType.stringof.replace("Default!(", "").stripRight(")"); | ||
match ~= format("\n\t(%s a) => %s(a)", name, name.replace("_args", "")) ~ ", "; | ||
}} | ||
match = match.stripRight(", "); | ||
match ~= "\n);"; | ||
|
||
static foreach (command; supportedCommands) | ||
case __traits(identifier, command): | ||
{ | ||
return match; | ||
}(); | ||
} | ||
|
||
infof("Running %s task", cmd.bold); | ||
command(args[2..$]); | ||
infof("Execution Succesfull"); | ||
return 0; | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
errorf("Task %s failed. Error:\n%s", cmd.bold, e); | ||
return 1; | ||
} | ||
struct MCLArgs | ||
{ | ||
@NamedArgument(["log-level"]) | ||
LogLevel logLevel = cast(LogLevel)-1; | ||
mixin(genSubCommandArgs!()); | ||
} | ||
|
||
mixin CLI!MCLArgs.main!((args) | ||
{ | ||
static assert(is(typeof(args) == MCLArgs)); | ||
|
||
LogLevel logLevel = LogLevel.info; | ||
if (args.logLevel != cast(LogLevel)-1) | ||
logLevel = args.logLevel; | ||
setLogLevel(logLevel); | ||
|
||
mixin genSubCommandMatch; | ||
|
||
return 0; | ||
}); | ||
|
||
void setLogLevel(LogLevel l) | ||
{ | ||
import std.logger : globalLogLevel, sharedLog; | ||
globalLogLevel = l; | ||
(cast()sharedLog()).logLevel = l; | ||
} | ||
|
||
int wrongUsage(string error) | ||
{ | ||
writefln("Error: %s.", error); | ||
writeln("Usage:\n"); | ||
static foreach (cmd; supportedCommands) | ||
writefln(" mcl %s", __traits(identifier, cmd)); | ||
|
||
return 1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ import std.exception : enforce; | |
import std.format : fmt = format; | ||
import std.logger : tracef, infof, errorf, warningf; | ||
|
||
import argparse; | ||
|
||
import mcl.utils.env : optional, MissingEnvVarsException, parseEnv; | ||
import mcl.utils.string : enumToString, StringRepresentation, MaxWidth, writeRecordAsTable; | ||
import mcl.utils.json : toJSON; | ||
|
@@ -141,16 +143,15 @@ version (unittest) | |
]; | ||
} | ||
|
||
immutable Params params; | ||
Params params; | ||
|
||
version (unittest) {} else | ||
shared static this() | ||
@(Command("ci-matrix", "ci_matrix").Description("Print a table of the cache status of each package")) | ||
struct ci_matrix_args | ||
{ | ||
params = parseEnv!Params; | ||
} | ||
|
||
export void ci_matrix(string[] args) | ||
export void ci_matrix(ci_matrix_args args) | ||
{ | ||
params = parseEnv!Params; | ||
createResultDirs(); | ||
nixEvalForAllSystems().array.printTableForCacheStatus(); | ||
} | ||
|
@@ -186,13 +187,20 @@ Package[] checkCacheStatus(Package[] packages) | |
return packages; | ||
} | ||
|
||
export void print_table(string[] args) | ||
@(Command("print-table", "print_table").Description("Print a table of the cache status of each package")) | ||
struct print_table_args | ||
{ | ||
} | ||
|
||
export int print_table(print_table_args args) | ||
Comment on lines
-189
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we instead try supporting argument-less commands by providing an aptly argument-less function returning |
||
{ | ||
createResultDirs(); | ||
|
||
getPrecalcMatrix() | ||
.checkCacheStatus() | ||
.printTableForCacheStatus(); | ||
|
||
return 0; | ||
} | ||
|
||
struct Params | ||
|
@@ -203,6 +211,7 @@ struct Params | |
@optional() int maxWorkers; | ||
@optional() int maxMemory; | ||
@optional() bool isInitial; | ||
|
||
string cachixCache; | ||
string cachixAuthToken; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer capitalized names (
PascalCase
, to be exact) forstruct
s. Applies to the rest of the_args
struct
s too.