Skip to content

Commit 9e53461

Browse files
committed
fix commits later
1 parent e2ae8ff commit 9e53461

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

packages/mcl/src/main.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ alias supportedCommands = imported!`std.traits`.AliasSeq!(
1616
cmds.shard_matrix,
1717
cmds.host_info,
1818
cmds.ci,
19-
cmds.machine
19+
cmds.machine,
20+
cmds.config
2021
);
2122

2223
int main(string[] args)
Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
module mcl.commands.config;
22

3-
import std.stdio : writeln;
3+
import std.stdio : writeln, write;
44
import std.conv : to;
55
import std.json : JSONValue;
66
import std.format : fmt = format;
77
import std.exception : enforce;
88
import std.range : front;
9+
import std.string : indexOf, strip;
10+
import std.logger: errorf;
11+
import core.stdc.stdlib: exit;
12+
import std.algorithm : each;
13+
import std.array : array;
14+
import std.process : Redirect, ProcessPipes, wait;
915

1016
import mcl.utils.env : optional, parseEnv;
1117
import mcl.utils.fetch : fetchJson;
@@ -15,46 +21,78 @@ import mcl.utils.process : execute;
1521

1622
export void config(string[] args)
1723
{
18-
const params = parseEnv!Params;
24+
if (args.length == 0)
25+
{
26+
27+
errorf("Usage: mcl config <subcommand> [args]");
28+
exit(1);
29+
}
30+
if (!checkRepo())
31+
{
32+
errorf("This command must be run from a repository containing a NixOS machine configuration");
33+
exit(1);
34+
}
1935
switch (args.front) {
2036
case "sys":
21-
sys(params, args);
37+
sys(args[1..$]);
2238
break;
2339
case "home":
24-
home(params, args);
40+
home(args[1..$]);
2541
break;
2642
case "start-vm":
27-
startVM(params, args);
43+
startVM(args[1..$]);
2844
break;
2945
default:
30-
assert(false, "Unknown config subcommand" ~ args.front);
46+
errorf("Unknown config subcommand" ~ args.front ~ ". Supported subcommands: sys, home, start-vm");
3147
}
3248
}
3349

34-
void sys(Params params, string[] args)
50+
bool checkRepo()
3551
{
52+
string remoteOriginUrl = execute(["git", "config", "--get", "remote.origin.url"], false);
53+
return remoteOriginUrl.indexOf("nixos-machine-config") != -1 || remoteOriginUrl.indexOf("infra-lido") != -1;
3654
}
3755

38-
void home(Params params, string[] args)
56+
void sys(string[] args)
3957
{
58+
if ((args.length < 1 || args.length > 2) && args.front != "apply")
59+
{
60+
errorf("Usage: mcl config sys apply or mcl config sys apply <machine-name>");
61+
exit(1);
62+
}
63+
else {
64+
string machineName = args.length > 1 ? args[1] : "";
65+
writeln("Applying system configuration from: ", machineName);
66+
auto exec = execute!ProcessPipes( "just switch-system " ~ machineName, true, false, Redirect.stderrToStdout);
67+
wait(exec.pid);
68+
};
4069
}
4170

42-
void startVM(Params params, string[] args)
71+
void home(string[] args)
4372
{
44-
if (args.length < 2 || args.length > 2)
45-
assert(false, "Usage: mcl config start-vm <vm-name>");
73+
if ((args.length != 2) && args.front != "apply")
74+
{
75+
errorf("Usage: mcl config home apply <desktop/server>");
76+
exit(1);
77+
}
4678
else {
47-
string vmName = args[1];
48-
writeln("Starting VM: ", vmName);
49-
execute(["just", "start-vm", vmName]);
50-
};
79+
auto type = args[1];
80+
writeln("Applying home configuration from: ", type);
81+
auto exec = execute!ProcessPipes( ["just", "switch-home", type], true, false, Redirect.stderrToStdout);
82+
wait(exec.pid);
83+
}
5184
}
5285

53-
struct Params
86+
void startVM(string[] args)
5487
{
55-
56-
void setup()
88+
if (args.length < 1 || args.length > 1)
5789
{
58-
90+
errorf("Usage: mcl config start-vm <vm-name>");
91+
exit(1);
5992
}
93+
else {
94+
string vmName = args.front;
95+
writeln("Starting VM: ", vmName);
96+
execute(["just", "start-vm", vmName]);
97+
};
6098
}

packages/mcl/src/src/mcl/commands/shard_matrix.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import mcl.utils.json : toJSON;
1818
import mcl.utils.nix : nix;
1919
import mcl.utils.path : createResultDirs, resultDir, rootDir;
2020

21-
export void shard_matrix()
21+
export void shard_matrix(string[] args)
2222
{
2323
const params = parseEnv!Params;
2424
auto matrix = generateShardMatrix();

packages/mcl/src/src/mcl/utils/process.d

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ import mcl.utils.test;
44

55
import mcl.utils.tui : bold;
66

7-
import std.process : ProcessPipes;
7+
import std.process : ProcessPipes, Redirect;
88
import std.string : split, strip;
99
import core.sys.posix.unistd : geteuid;
1010
import std.json : JSONValue, parseJSON;
1111

1212
bool isRoot() => geteuid() == 0;
1313

14-
T execute(T = string)(string args, bool printCommand = true, bool returnErr = false) if (is(T == string) || is(T == ProcessPipes) || is(T == JSONValue))
14+
T execute(T = string)(string args, bool printCommand = true, bool returnErr = false, Redirect redirect = Redirect.all) if (is(T == string) || is(T == ProcessPipes) || is(T == JSONValue))
1515
{
16-
return execute!T(args.split(" "), printCommand, returnErr);
16+
return execute!T(args.strip.split(" "), printCommand, returnErr, redirect);
1717
}
18-
T execute(T = string)(string[] args, bool printCommand = true, bool returnErr = false) if (is(T == string) || is(T == ProcessPipes) || is(T == JSONValue))
18+
T execute(T = string)(string[] args, bool printCommand = true, bool returnErr = false, Redirect redirect = Redirect.all) if (is(T == string) || is(T == ProcessPipes) || is(T == JSONValue))
1919
{
2020
import std.exception : enforce;
2121
import std.format : format;
22-
import std.process : pipeShell, wait, Redirect, escapeShellCommand;
22+
import std.process : pipeShell, wait, escapeShellCommand;
2323
import std.logger : tracef, errorf, infof;
2424
import std.array : join;
2525
import std.algorithm : map;
@@ -31,7 +31,7 @@ T execute(T = string)(string[] args, bool printCommand = true, bool returnErr =
3131
{
3232
infof("\n$ `%s`", cmd.bold);
3333
}
34-
auto res = pipeShell(cmd, Redirect.all);
34+
auto res = pipeShell(cmd, redirect);
3535
static if (is(T == ProcessPipes))
3636
{
3737
return res;

0 commit comments

Comments
 (0)