Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/print-matrix/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/reusable-flake-checks-ci-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
CACHIX_CACHE: ${{ vars.CACHIX_CACHE }}
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
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 shard_matrix
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl shard-matrix
outputs:
gen_matrix: ${{ steps.generate-matrix.outputs.gen_matrix }}

Expand Down Expand Up @@ -91,7 +91,7 @@ jobs:
FLAKE_PRE: ${{ matrix.prefix }}
FLAKE_POST: ${{ matrix.postfix }}
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 ci_matrix
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl ci-matrix

- uses: actions/upload-artifact@v4
with:
Expand Down Expand Up @@ -222,4 +222,4 @@ jobs:
CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
CACHIX_ACTIVATE_TOKEN: '${{ secrets.CACHIX_ACTIVATE_TOKEN }}'
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 deploy_spec
run: nix run --accept-flake-config github:metacraft-labs/nixos-modules/${{ env.MCL_BRANCH }}#mcl deploy-spec
3 changes: 1 addition & 2 deletions packages/mcl/dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ buildType "unittest-debug" {
buildOptions "unittests" "debugMode" "debugInfo"
}

dflags "-preview=in"
dflags "-preview=shortenedMethods"
dflags "-defaultlib=libphobos2.so" platform="dmd"
lflags "-fuse-ld=gold" platform="dmd"
dflags "-mcpu=generic" platform="ldc"
dflags "-mcpu=baseline" platform="dmd"

dependency "mir-cpuid" version="~>1.2.11"
dependency "silly" version="~>1.1.1"
dependency "argparse" version="~>1.4.1"
1 change: 1 addition & 0 deletions packages/mcl/dub.selections.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"fileVersion": 1,
"versions": {
"argparse": "1.4.1",
"mir-core": "1.7.1",
"mir-cpuid": "1.2.11",
"silly": "1.1.1"
Expand Down
116 changes: 66 additions & 50 deletions packages/mcl/src/main.d
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 {}
Copy link
Member

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) for structs. Applies to the rest of the _args structs too.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be done in a similar fashion to the following genSubCommandMatch template?

}

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;
}
7 changes: 7 additions & 0 deletions packages/mcl/src/src/mcl/commands/ci.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import std.array : array, join;
import std.conv : to;
import std.process : ProcessPipes;

import argparse;

import mcl.utils.env : optional, parseEnv;
import mcl.commands.ci_matrix: nixEvalJobs, SupportedSystem, Params, flakeAttr;
import mcl.commands.shard_matrix: generateShardMatrix;
Expand All @@ -18,6 +20,11 @@ import mcl.utils.json : toJSON;

Params params;

@(Command("ci").Description("Run CI"))
struct ci_args
{
}

export void ci(string[] args)
{
params = parseEnv!Params;
Expand Down
23 changes: 16 additions & 7 deletions packages/mcl/src/src/mcl/commands/ci_matrix.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 void, with genSubCommandArgs being updated to dynamically check whether or not each sub-command handler function has arguments/return value at all before taking them into account?
This also applies to all _args structs annotated with @(Command(" ").Description(" ")).

{
createResultDirs();

getPrecalcMatrix()
.checkCacheStatus()
.printTableForCacheStatus();

return 0;
}

struct Params
Expand All @@ -203,6 +211,7 @@ struct Params
@optional() int maxWorkers;
@optional() int maxMemory;
@optional() bool isInitial;

string cachixCache;
string cachixAuthToken;

Expand Down
Loading
Loading