disko2: Move to error codes and dedicated error rendering

This splits the concern of how to display errors off from the business
logic. This should make it easier to test functionality and allow us
to concentrate on how to actually display and communicate errors to the
user. At some point this might also make i18n feasible, because you only
have to swap out a single file.

This hasn't quite fixed the debug/trace story yet. It should be possible
to simply insert a "debug" command into any pipeline and see the context
and state at that point.
This commit is contained in:
Felix Uhl committed 2024-10-25 23:10:57 +02:00
1 parent 684adb7e64
commit f2618b5490
4 files changed
+169 -42

No files matched your search

+10 -14
View File
@@ -1,29 +1,25 @@
#!/usr/bin/env nu
use lib [eval-disko-file eval-flake p_err p_help exit-on-error]
use lib [eval-disko-file eval-flake exit-on-error print-info print-help]
def modes [] { ["destroy", "format", "mount", "format,mount", "destroy,format,mount"] }
def modes [] { ["destroy", "format", "mount", "format,mount", "destroy,format,mount"]}
def main [
mode: string@modes, # Mode to use. Allowed values are 'destroy', 'format', 'mount', 'format,mount', 'destroy,format,mount'
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 {
if not ($mode in (modes)) {
p_err $"Invalid mode: (ansi red)($mode)(ansi reset)"
let valid_modes = modes
| each { |mode| $"(ansi green)($mode)(ansi reset)" }
| str join ', '
p_help $"Valid modes are: ($valid_modes)"
exit 1
{
success: false,
messages: [ { code: ERR_INVALID_MODE, details: { mode: $mode, valid_modes: (modes) } } ]
} | exit-on-error "validate mode"
}
if not ($flake != null xor $disko_file != null) {
p_err "Missing arguments!"
p_help ($"Provide either (ansi magenta_italic)disko_file(ansi reset) as the second argument or " +
$"(ansi green)--flake(ansi reset)/(ansi green)-f(ansi reset) (ansi magenta_italic)flakeref(ansi reset)")
exit 1
{ success: false, messages: [ { code: ERR_MISSING_ARGUMENTS } ] } | exit-on-error "validate arguments"
}
let config = if $disko_file != null {
@@ -32,5 +28,5 @@ def main [
$flake | eval-flake | exit-on-error "evaluate flake"
}
$config | to json | print
$config | to json | print-info
}