From 62581f38f5fcf65943e4f49fcf064b214a80ef12 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Fri, 25 Oct 2024 11:13:10 +0200 Subject: [PATCH] disko2: Split into modules --- disko.nu | 94 +------------------------- lib/errors.nu | 26 +++++++ eval-config.nix => lib/eval-config.nix | 0 lib/eval-config.nu | 68 +++++++++++++++++++ lib/mod.nu | 2 + 5 files changed, 97 insertions(+), 93 deletions(-) create mode 100644 lib/errors.nu rename eval-config.nix => lib/eval-config.nix (100%) create mode 100644 lib/eval-config.nu create mode 100644 lib/mod.nu diff --git a/disko.nu b/disko.nu index 8166628..883c25d 100755 --- a/disko.nu +++ b/disko.nu @@ -1,98 +1,6 @@ #!/usr/bin/env nu -use std log - -alias nix = ^nix --extra-experimental-features nix-command --extra-experimental-features flakes -alias nix-eval-expr = nix eval --impure --json --expr - -def eval-config [args: record]: nothing -> record { - do { nix-eval-expr $"import ./eval-config.nix \(builtins.fromJSON ''($args | to json -r)'')" } - | complete - | if $in.exit_code != 0 { - { - success: false - messages: [ - $"Failed to evaluate disko config with args ($args)!" - $"Error from nix eval: ($in.stderr)" - ] - } - } else { - $in.stdout - | from json - | { - success: true - value: $in - } - } -} - -export def eval-disko-file []: path -> record { - let config = $in - - if not ($config | path exists) { - return { - success: false - messages: [ - $"File (ansi blue)($config)(ansi reset) does not exist." - ] - } - } - - # If the file is valid JSON, parse and return it - open $config | try { into record } | if $in != null { - return { - success: true - value: $in - } - } - - eval-config { diskoFile: $config } -} - -export def eval-flake []: string -> record { - let flakeUri = $in - let parseResult = $flakeUri | parse "{flake}#{flakeAttr}" | if not ( $in | is-empty ) { - $in - } else { - return { - success: false - messages: [ - $"Flake-uri ($flakeUri) does not contain an attribute." - "Please append an attribute like \"#foo\" to the flake-uri." - ] - } - } - - let flake = $parseResult.0.flake | if ($in | path exists) { $in | path expand } else { $in } - let flakeAttr = $parseResult.0.flakeAttr - - eval-config { flake: $flake, flakeAttr: $flakeAttr } -} - -def exit-on-error [context: string] { - if $in.success { - log debug $"Success: ($context)" - log debug $"Return value: ($in.value)" - return $in.value - } - - p_err $"Failed to ($context)!" - for msg in $in.messages { - print $"(ansi red)│(ansi reset) ($msg)" - } - print $"(ansi red)╰────────(ansi reset)" - exit 1 -} - -def p_err [msg: string] { - print $"(ansi bg_red) ERROR: (ansi reset) ($msg)" -} -def p_info [msg: string] { - print $"(ansi bg_green) INFO: (ansi reset) ($msg)" -} -def p_help [msg: string] { - print $"(ansi bg_light_magenta) HELP: (ansi reset) ($msg)" -} +use lib [eval-disko-file eval-flake p_err p_help exit-on-error] def modes [] { ["destroy", "format", "mount", "format,mount", "destroy,format,mount"]} diff --git a/lib/errors.nu b/lib/errors.nu new file mode 100644 index 0000000..e859ab3 --- /dev/null +++ b/lib/errors.nu @@ -0,0 +1,26 @@ +use std log + +export def exit-on-error [context: string] { + if $in.success { + log debug $"Success: ($context)" + log debug $"Return value: ($in.value)" + return $in.value + } + + p_err $"Failed to ($context)!" + for msg in $in.messages { + print $"(ansi red)│(ansi reset) ($msg)" + } + print $"(ansi red)╰────────(ansi reset)" + exit 1 +} + +export def p_err [msg: string] { + print $"(ansi bg_red) ERROR: (ansi reset) ($msg)" +} +export def p_info [msg: string] { + print $"(ansi bg_green) INFO: (ansi reset) ($msg)" +} +export def p_help [msg: string] { + print $"(ansi bg_light_magenta) HELP: (ansi reset) ($msg)" +} \ No newline at end of file diff --git a/eval-config.nix b/lib/eval-config.nix similarity index 100% rename from eval-config.nix rename to lib/eval-config.nix diff --git a/lib/eval-config.nu b/lib/eval-config.nu new file mode 100644 index 0000000..9ac08d6 --- /dev/null +++ b/lib/eval-config.nu @@ -0,0 +1,68 @@ +alias nix = ^nix --extra-experimental-features nix-command --extra-experimental-features flakes +alias nix-eval-expr = nix eval --impure --json --expr + +def eval-config [args: record]: nothing -> record { + let eval_config_nix = $env.FILE_PWD + "/lib/eval-config.nix" + + do { nix-eval-expr $"import ($eval_config_nix) \(builtins.fromJSON ''($args | to json -r)'')" } + | complete + | if $in.exit_code != 0 { + { + success: false + messages: [ + $"Failed to evaluate disko config with args ($args)!" + $"Error from nix eval: ($in.stderr)" + ] + } + } else { + $in.stdout + | from json + | { + success: true + value: $in + } + } +} + +export def eval-disko-file []: path -> record { + let config = $in + + if not ($config | path exists) { + return { + success: false + messages: [ + $"File (ansi blue)($config)(ansi reset) does not exist." + ] + } + } + + # If the file is valid JSON, parse and return it + open $config | try { into record } | if $in != null { + return { + success: true + value: $in + } + } + + eval-config { diskoFile: $config } +} + +export def eval-flake []: string -> record { + let flakeUri = $in + let parseResult = $flakeUri | parse "{flake}#{flakeAttr}" | if not ( $in | is-empty ) { + $in + } else { + return { + success: false + messages: [ + $"Flake-uri ($flakeUri) does not contain an attribute." + "Please append an attribute like \"#foo\" to the flake-uri." + ] + } + } + + let flake = $parseResult.0.flake | if ($in | path exists) { $in | path expand } else { $in } + let flakeAttr = $parseResult.0.flakeAttr + + eval-config { flake: $flake, flakeAttr: $flakeAttr } +} diff --git a/lib/mod.nu b/lib/mod.nu new file mode 100644 index 0000000..6135208 --- /dev/null +++ b/lib/mod.nu @@ -0,0 +1,2 @@ +export use eval-config.nu [eval-disko-file eval-flake] +export use errors.nu [exit-on-error p_err p_info p_help]