Skip to content

Commit b77534a

Browse files
monyarmPetarKirov
authored andcommitted
feat(commands/machine): Enhance machine command with argparse integration and structured argument handling
1 parent eed0198 commit b77534a

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed

packages/mcl/src/main.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ template genSubCommandArgs()
3333
"deploy_spec_args,"~
3434
"host_info_args,"~
3535
"config_args,"~
36+
"machine_args,"~
3637
"Default!unknown_command_args"~
3738
") cmd;";
3839
}

packages/mcl/src/src/mcl/commands/machine.d

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module mcl.commands.machine;
22

33
import std;
4+
5+
import argparse;
6+
47
import mcl.utils.log : prompt;
58
import mcl.utils.process : execute;
69
import mcl.utils.nix : nix, toNix, Literal, mkDefault;
@@ -173,22 +176,22 @@ string[] getGroups()
173176
return groups;
174177
}
175178

176-
User createUser() {
179+
User createUser(create_machine_args args) {
177180

178-
auto createUser = params.createUser || prompt!bool("Create new user");
181+
auto createUser = args.createUser || prompt!bool("Create new user");
179182
if (!createUser)
180183
{
181184
string[] existingUsers = getExistingUsers();
182-
string userName = params.userName != "" ? params.userName : prompt!string("Select an existing username", existingUsers);
185+
string userName = args.userName != "" ? args.userName : prompt!string("Select an existing username", existingUsers);
183186
return getUser(userName);
184187
}
185188
else
186189
{
187190
User user;
188-
user.userName = params.userName != "" ? params.userName : prompt!string("Enter the new username");
189-
user.userInfo.description = params.description != "" ? params.description : prompt!string("Enter the user's description/full name");
190-
user.userInfo.isNormalUser = params.isNormalUser || prompt!bool("Is this a normal or root user");
191-
user.userInfo.extraGroups = (params.extraGroups != "" ? params.extraGroups : prompt!string("Enter the user's extra groups (comma delimited)", getGroups())).split(",").map!(strip).array;
191+
user.userName = args.userName != "" ? args.userName : prompt!string("Enter the new username");
192+
user.userInfo.description = args.description != "" ? args.description : prompt!string("Enter the user's description/full name");
193+
user.userInfo.isNormalUser = args.isNormalUser || prompt!bool("Is this a normal or root user");
194+
user.userInfo.extraGroups = (args.extraGroups != "" ? args.extraGroups : prompt!string("Enter the user's extra groups (comma delimited)", getGroups())).split(",").map!(strip).array;
192195
createUserDir(user);
193196
return user;
194197
}
@@ -220,8 +223,8 @@ struct MachineConfiguration
220223
MachineUserInfo users;
221224
}
222225

223-
void createMachine(MachineType machineType, string machineName, User user) {
224-
auto infoJSON = execute(["ssh", params.sshPath, "sudo nix --experimental-features \\'nix-command flakes\\' --refresh --accept-flake-config run github:metacraft-labs/nixos-modules/#mcl host_info"],false, false);
226+
void createMachine(create_machine_args args, MachineType machineType, string machineName, User user) {
227+
auto infoJSON = execute(["ssh", args.sshPath, "sudo nix --experimental-features \\'nix-command flakes\\' --refresh --accept-flake-config run github:metacraft-labs/nixos-modules/#mcl host_info"],false, false);
225228
auto infoJSONParsed = infoJSON.parseJSON;
226229
Info info = infoJSONParsed.fromJSON!Info;
227230

@@ -273,7 +276,7 @@ void createMachine(MachineType machineType, string machineName, User user) {
273276
// Disks
274277
hardwareConfiguration.disko.DISKO.makeZfsPartitions.swapSizeGB = (info.hardwareInfo.memoryInfo.totalGB.to!double*1.5).to!int;
275278
auto nvmeDevices = info.hardwareInfo.storageInfo.devices.filter!(a => a.dev.indexOf("nvme") != -1 || a.model.indexOf("SSD") != -1).array.map!(a => a.model.replace(" ", "_") ~ "_" ~ a.serial).array;
276-
string[] disks = (nvmeDevices.length == 1 ? nvmeDevices[0] : (params.disks != "" ? params.disks : prompt!string("Enter the disks to use (comma delimited)", nvmeDevices))).split(",").map!(strip).array.map!(a => "/dev/disk/by-id/nvme-" ~ a).array;
279+
string[] disks = (nvmeDevices.length == 1 ? nvmeDevices[0] : (args.disks != "" ? args.disks : prompt!string("Enter the disks to use (comma delimited)", nvmeDevices))).split(",").map!(strip).array.map!(a => "/dev/disk/by-id/nvme-" ~ a).array;
277280
hardwareConfiguration.disko.DISKO.makeZfsPartitions.disks = disks;
278281

279282
hardwareConfiguration = hardwareConfiguration.uniqArrays;
@@ -365,43 +368,58 @@ struct HardwareConfiguration {
365368
Services services;
366369
}
367370

368-
void createMachineConfiguration()
371+
int createMachineConfiguration(create_machine_args args)
369372
{
370373
checkifNixosMachineConfigRepo();
371-
auto machineType = cast(int)params.machineType != 0 ? params.machineType : prompt!MachineType("Machine type");
372-
auto machineName = params.machineName != "" ? params.machineName : prompt!string("Enter the name of the machine");
374+
auto machineType = cast(int)args.machineType != 0 ? args.machineType : prompt!MachineType("Machine type");
375+
auto machineName = args.machineName != "" ? args.machineName : prompt!string("Enter the name of the machine");
373376
User user;
374-
user = createUser();
375-
machineType.createMachine( machineName, user);
377+
user = createUser(args);
378+
args.createMachine(machineType, machineName, user);
379+
return 0;
376380
}
377381

378-
Params params;
379382

380-
export void machine(string[] args)
383+
export int machine(machine_args args)
381384
{
382-
params = parseEnv!Params;
383-
switch (args.front)
384-
{
385-
case "create":
386-
createMachineConfiguration();
387-
break;
388-
default:
389-
assert(0, "Unknown machine action: " ~ args.front);
390-
}
385+
return args.cmd.match!(
386+
(create_machine_args a) => createMachineConfiguration(a),
387+
(unknown_command_args a) => unknown_command(a)
388+
);
391389
}
392-
struct Params
393-
{
390+
@(Command("create").Description("Create a new machine"))
391+
struct create_machine_args {
392+
393+
@(PositionalArgument(0).Placeholder("ssh").Description("SSH path to the machine"))
394394
string sshPath;
395-
@optional() bool createUser;
396-
@optional() string userName;
397-
@optional() string machineName;
398-
@optional() string description;
399-
@optional() bool isNormalUser;
400-
@optional() string extraGroups;
401-
@optional() MachineType machineType = cast(MachineType)0;
402-
@optional() string disks;
403-
404-
void setup()
405-
{
406-
}
395+
@(NamedArgument(["create-user"]).Placeholder("true/false").Description("Create a new user"))
396+
bool createUser;
397+
@(NamedArgument(["user-name"]).Placeholder("username").Description("Username"))
398+
string userName;
399+
@(NamedArgument(["machine-name"]).Placeholder("machine-name").Description("Name of the machine"))
400+
string machineName;
401+
@(NamedArgument(["description"]).Placeholder("description").Description("Description of the user"))
402+
string description;
403+
@(NamedArgument(["is-normal-user"]).Placeholder("true/false").Description("Is this a normal user"))
404+
bool isNormalUser;
405+
@(NamedArgument(["extra-groups"]).Placeholder("group1,group2").Description("Extra groups for the user"))
406+
string extraGroups;
407+
@(NamedArgument(["machine-type"]).Placeholder("desktop/server/container").Description("Type of machine"))
408+
MachineType machineType = cast(MachineType)0;
409+
@(NamedArgument(["disks"]).Placeholder("CT2000P3PSSD8_2402E88C1519,...").Description("Disks to use"))
410+
string disks;
411+
}
412+
@(Command(" ").Description(" "))
413+
struct unknown_command_args {}
414+
int unknown_command(unknown_command_args unused)
415+
{
416+
stderr.writeln("Unknown machine command. Use --help for a list of available commands.");
417+
return 1;
418+
}
419+
420+
@(Command("machine").Description("Manage machines"))
421+
struct machine_args
422+
{
423+
424+
@SubCommands SumType!(create_machine_args,Default!unknown_command_args) cmd;
407425
}

packages/mcl/src/src/mcl/commands/package.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ public import mcl.commands.ci_matrix : ci_matrix, print_table;
66
public import mcl.commands.shard_matrix : shard_matrix;
77
public import mcl.commands.ci : ci;
88
public import mcl.commands.host_info : host_info, host_info_args;
9-
public import mcl.commands.machine : machine;
9+
public import mcl.commands.machine : machine, machine_args;
1010
public import mcl.commands.config : config, config_args;

0 commit comments

Comments
 (0)