11module mcl.commands.config ;
22
3- import std.stdio : writeln;
3+ import std.stdio : writeln, write ;
44import std.conv : to;
55import std.json : JSONValue;
66import std.format : fmt = format;
77import std.exception : enforce;
88import 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
1016import mcl.utils.env : optional, parseEnv;
1117import mcl.utils.fetch : fetchJson;
@@ -15,46 +21,78 @@ import mcl.utils.process : execute;
1521
1622export 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}
0 commit comments