From 6f579bd98a03fe2791701a030c73c53b00ba2946 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Sun, 27 Oct 2024 17:52:33 +0100 Subject: [PATCH] disko2: Handle too many arguments error --- disko.test.nu | 10 ++++++++++ disko2 | 8 +++++++- lib/errors.nu | 24 +++++++++++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/disko.test.nu b/disko.test.nu index 94fd3cf..f668e81 100755 --- a/disko.test.nu +++ b/disko.test.nu @@ -308,6 +308,16 @@ assert equal (disko { mode: "mount" }) { context: "validate arguments" } +assert equal (disko { mode: "mount", flake: "irrelevant", disko_file: "whatever" }) { + success: false, + messages: [ + { + code: ERR_TOO_MANY_ARGUMENTS + } + ], + context: "validate arguments" +} + def main [] { echo "All tests passed" } \ No newline at end of file diff --git a/disko2 b/disko2 index a386aa2..9f96254 100755 --- a/disko2 +++ b/disko2 @@ -27,7 +27,13 @@ export def run [ if not ($args.flake? != null xor $args.disko_file? != null) { return { success: false, - messages: [ { code: ERR_MISSING_ARGUMENTS } ], + messages: [ + { + code: ( + if ($args.flake? == null) { 'ERR_MISSING_ARGUMENTS' } else { 'ERR_TOO_MANY_ARGUMENTS' } + ) + } + ], context: "validate arguments" } } diff --git a/lib/errors.nu b/lib/errors.nu index 535e3f3..3e72172 100644 --- a/lib/errors.nu +++ b/lib/errors.nu @@ -27,16 +27,33 @@ def "lookup ERR_INVALID_MODE" [mode: string, valid_modes: list] { ] } +def err-arguments-help [] { + ($"Provide either (placeholder)disko_file(reset) as the second argument or " + + $"(flag)--flake(reset)/(flag)-f(reset) (placeholder)flakeref(reset)") +} + def "lookup ERR_MISSING_ARGUMENTS" [] { [ { type: error - msg: $"Missing arguments!" + msg: "Missing arguments!" } { type: help - msg: ($"Provide either (placeholder)disko_file(reset) as the second argument or " + - $"(flag)--flake(reset)/(flag)-f(reset) (placeholder)flakeref(reset)") + msg: (err-arguments-help) + } + ] +} + +def "lookup ERR_TOO_MANY_ARGUMENTS" [] { + [ + { + type: error + msg: "Too many arguments!" + } + { + type: help + msg: (err-arguments-help) } ] } @@ -83,6 +100,7 @@ def lookup-message []: [ match $code { ERR_INVALID_MODE => (lookup ERR_INVALID_MODE $details.mode $details.valid_modes) ERR_MISSING_ARGUMENTS => (lookup ERR_MISSING_ARGUMENTS) + ERR_TOO_MANY_ARGUMENTS => (lookup ERR_TOO_MANY_ARGUMENTS) ERR_EVAL_CONFIG_FAILED => (lookup ERR_EVAL_CONFIG_FAILED $details.args $details.stderr) ERR_FILE_NOT_FOUND => (lookup ERR_FILE_NOT_FOUND $details.path) ERR_FLAKE_URI_NO_ATTRIBUTE => (lookup ERR_FLAKE_URI_NO_ATTRIBUTE $details.flakeUri)