2024-10-23 16:49:22 +00:00
|
|
|
#!/usr/bin/env nu
|
|
|
|
|
2024-10-27 19:44:53 +00:00
|
|
|
use lib [eval-disko-file eval-flake exit-on-error print-info print-help generate-config]
|
2024-10-25 21:04:10 +00:00
|
|
|
|
2024-10-27 19:44:53 +00:00
|
|
|
def modes [] { ["destroy", "format", "mount", "format,mount", "destroy,format,mount", "generate"] }
|
2024-10-25 08:32:59 +00:00
|
|
|
|
2024-10-27 16:43:11 +00:00
|
|
|
def with-context [context: string] {
|
|
|
|
$in | insert context $context
|
|
|
|
}
|
2024-10-23 16:49:22 +00:00
|
|
|
|
2024-10-27 16:43:11 +00:00
|
|
|
export def run [
|
|
|
|
args: record
|
|
|
|
]: [
|
|
|
|
nothing -> record<success: bool, context: string, value: any>
|
|
|
|
nothing -> record<success: bool, context: string, messages: list<record<code: string>>>
|
|
|
|
nothing -> record<success: bool, context: string, messages: list<record<code: string, details: record<any>>>>
|
|
|
|
] {
|
2024-10-25 21:04:10 +00:00
|
|
|
|
2024-10-27 16:43:11 +00:00
|
|
|
if not ($args.mode in (modes)) {
|
|
|
|
return {
|
2024-10-25 21:04:10 +00:00
|
|
|
success: false,
|
2024-10-27 16:43:11 +00:00
|
|
|
messages: [ { code: ERR_INVALID_MODE, details: { mode: $args.mode, valid_modes: (modes) } } ]
|
|
|
|
context: "validate mode"
|
|
|
|
}
|
2024-10-25 08:32:59 +00:00
|
|
|
}
|
2024-10-23 16:49:22 +00:00
|
|
|
|
2024-10-27 19:44:53 +00:00
|
|
|
|
|
|
|
match $args.mode {
|
|
|
|
"generate" => (mode-generate $args)
|
|
|
|
_ => (mode-format $args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export def mode-generate [args: record] {
|
|
|
|
generate-config | with-context "generate config"
|
|
|
|
}
|
|
|
|
|
|
|
|
export def mode-format [args: record] {
|
2024-10-27 16:43:11 +00:00
|
|
|
if not ($args.flake? != null xor $args.disko_file? != null) {
|
|
|
|
return {
|
|
|
|
success: false,
|
2024-10-27 16:52:33 +00:00
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
code: (
|
|
|
|
if ($args.flake? == null) { 'ERR_MISSING_ARGUMENTS' } else { 'ERR_TOO_MANY_ARGUMENTS' }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
],
|
2024-10-27 16:43:11 +00:00
|
|
|
context: "validate arguments"
|
|
|
|
}
|
2024-10-23 16:49:22 +00:00
|
|
|
}
|
|
|
|
|
2024-10-27 16:43:11 +00:00
|
|
|
let eval_result = if $args.disko_file? != null {
|
|
|
|
$args.disko_file | eval-disko-file | with-context "evaluate config"
|
2024-10-23 16:49:22 +00:00
|
|
|
} else {
|
2024-10-27 16:43:11 +00:00
|
|
|
$args.flake | eval-flake | with-context "evaluate flake"
|
2024-10-23 16:49:22 +00:00
|
|
|
}
|
|
|
|
|
2024-10-27 16:43:11 +00:00
|
|
|
# TODO: Continue implementation here
|
|
|
|
$eval_result
|
|
|
|
}
|
|
|
|
|
|
|
|
export def main [
|
2024-10-27 19:44:53 +00:00
|
|
|
mode: string@modes, # Mode to use. Allowed values are 'destroy', 'format', 'mount', 'format,mount', 'destroy,format,mount', 'generate'
|
2024-10-27 16:43:11 +00:00
|
|
|
disko_file?: path, # File to read the disko configuration from. Can be a .nix file or a .json file
|
|
|
|
--flake (-f): string # Flake URI to search for the disko configuration
|
|
|
|
]: nothing -> nothing {
|
|
|
|
|
|
|
|
run {
|
|
|
|
mode: $mode,
|
|
|
|
disko_file: $disko_file,
|
|
|
|
flake: $flake
|
|
|
|
}
|
|
|
|
| exit-on-error
|
|
|
|
| to json | print-info
|
2024-10-23 16:49:22 +00:00
|
|
|
}
|