|
| 1 | +module mcl.commands.config; |
| 2 | + |
| 3 | +import std.algorithm : canFind; |
| 4 | +import std.array : array; |
| 5 | +import std.process : ProcessPipes, Redirect, wait, environment; |
| 6 | +import std.range : drop, front; |
| 7 | +import std.stdio : writeln; |
| 8 | +import std.string : indexOf; |
| 9 | + |
| 10 | +import mcl.utils.env : optional, parseEnv; |
| 11 | +import mcl.utils.fetch : fetchJson; |
| 12 | +import mcl.utils.log : errorAndExit; |
| 13 | +import mcl.utils.nix : nix, queryStorePath; |
| 14 | +import mcl.utils.process : execute; |
| 15 | +import mcl.utils.string : camelCaseToCapitalCase; |
| 16 | + |
| 17 | +export void config(string[] args) { |
| 18 | + if (args.length == 0) { |
| 19 | + errorAndExit("Usage: mcl config <subcommand> [args]"); |
| 20 | + } |
| 21 | + if (!checkRepo()) { |
| 22 | + errorAndExit("This command must be run from a repository containing a NixOS machine configuration"); |
| 23 | + } |
| 24 | + |
| 25 | + string subcommand = args.front; |
| 26 | + |
| 27 | + switch (subcommand) { |
| 28 | + case "sys": |
| 29 | + sys(args.drop(1)); |
| 30 | + break; |
| 31 | + case "home": |
| 32 | + home(args.drop(1)); |
| 33 | + break; |
| 34 | + case "start-vm": |
| 35 | + startVM(args.drop(1)); |
| 36 | + break; |
| 37 | + default: |
| 38 | + errorAndExit("Unknown config subcommand " ~ subcommand ~ ". Supported subcommands: sys, home, start-vm"); |
| 39 | + break; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +bool checkRepo() |
| 44 | +{ |
| 45 | + const string[] validRepos = ["nixos-machine-config", "infra-lido"]; |
| 46 | + string remoteOriginUrl = execute(["git", "config", "--get", "remote.origin.url"], false); |
| 47 | + |
| 48 | + foreach (string repo; validRepos) { |
| 49 | + if (remoteOriginUrl.indexOf(repo) != -1) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + } |
| 53 | + return false; |
| 54 | +} |
| 55 | + |
| 56 | +void executeCommand(string command) { |
| 57 | + auto exec = execute!ProcessPipes(command, true, false, Redirect.stderrToStdout); |
| 58 | + wait(exec.pid); |
| 59 | +} |
| 60 | + |
| 61 | +void edit(string type, string path) { |
| 62 | + string editor = environment.get("EDITOR", "vim"); |
| 63 | + string user = environment.get("USER", "root"); |
| 64 | + writeln("Editing " ~ path ~ " configuration from: ", path); |
| 65 | + final switch (type) { |
| 66 | + case "system": |
| 67 | + executeCommand(editor ~ " machines/*/" ~ path ~ "/*.nix"); |
| 68 | + break; |
| 69 | + case "user": |
| 70 | + executeCommand(editor~ " users/" ~ user ~ "/gitconfig " ~ "users/" ~ user ~ "/*.nix " ~ "users/" ~ user ~ "/home-"~path~"/*.nix"); |
| 71 | + break; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void sys(string[] args) |
| 76 | +{ |
| 77 | + if ((args.length < 1 || args.length > 2) && !["apply", "edit"].canFind(args.front)) |
| 78 | + { |
| 79 | + errorAndExit("Usage: mcl config sys apply or mcl config sys apply <machine-name>\n"~ |
| 80 | + " mcl config sys edit or mcl config sys edit <machine-name>"); |
| 81 | + } |
| 82 | + |
| 83 | + string machineName = args.length > 1 ? args[1] : ""; |
| 84 | + final switch (args.front) { |
| 85 | + case "apply": |
| 86 | + writeln("Applying system configuration from: ", machineName); |
| 87 | + executeCommand("just switch-system " ~ machineName); |
| 88 | + break; |
| 89 | + case "edit": |
| 90 | + edit("system", machineName); |
| 91 | + break; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void home(string[] args) |
| 96 | +{ |
| 97 | + if ((args.length != 2) && args.front != "apply") |
| 98 | + { |
| 99 | + errorAndExit("Usage: mcl config home apply <desktop/server>\n"~ |
| 100 | + " mcl config home edit <desktop/server>"); |
| 101 | + } |
| 102 | + |
| 103 | + auto type = args[1]; |
| 104 | + final switch (args.front) { |
| 105 | + case "apply": |
| 106 | + writeln("Applying home configuration from: ", type); |
| 107 | + executeCommand("just switch-home " ~ type); |
| 108 | + break; |
| 109 | + case "edit": |
| 110 | + edit("user", type); |
| 111 | + break; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void startVM(string[] args) |
| 116 | +{ |
| 117 | + if (args.length != 1) |
| 118 | + { |
| 119 | + errorAndExit("Usage: mcl config start-vm <vm-name>"); |
| 120 | + } |
| 121 | + |
| 122 | + string vmName = args.front; |
| 123 | + writeln("Starting VM: ", vmName); |
| 124 | + executeCommand("just start-vm " ~ vmName); |
| 125 | +} |
0 commit comments