mirror of
https://github.com/nix-community/disko.git
synced 2026-11-30 21:13:28 +01:00
disko2: Make CLI errors testable as well
This commit is contained in:
1 parent
47081e80ea
commit
196c746f7e
3 files changed
+98
-34
No files matched your search
+46
-8
@@ -1,10 +1,13 @@
|
||||
#!/usr/bin/env nu
|
||||
|
||||
use disko.nu
|
||||
use lib [eval-disko-file eval-flake exit-on-error print-info print-help]
|
||||
use disko2
|
||||
|
||||
use std assert
|
||||
|
||||
assert equal ("example/simple-efi.nix" | path expand | disko eval-disko-file) {
|
||||
alias disko = disko2 run
|
||||
|
||||
assert equal ("example/simple-efi.nix" | path expand | eval-disko-file) {
|
||||
success: true,
|
||||
value: {
|
||||
"disk": {
|
||||
@@ -90,7 +93,7 @@ assert equal ("example/simple-efi.nix" | path expand | disko eval-disko-file) {
|
||||
}
|
||||
}
|
||||
|
||||
assert equal ("example/with-lib.nix" | path expand | disko eval-disko-file) {
|
||||
assert equal ("example/with-lib.nix" | path expand | eval-disko-file) {
|
||||
success: true,
|
||||
value: {
|
||||
"disk": {
|
||||
@@ -163,7 +166,7 @@ assert equal ("example/with-lib.nix" | path expand | disko eval-disko-file) {
|
||||
}
|
||||
}
|
||||
|
||||
assert equal (".#testmachine" | disko eval-flake) {
|
||||
assert equal (".#testmachine" | eval-flake) {
|
||||
success: true,
|
||||
value: {
|
||||
"disk": {
|
||||
@@ -262,12 +265,47 @@ assert equal (".#testmachine" | disko eval-flake) {
|
||||
}
|
||||
}
|
||||
|
||||
assert equal ("." | disko eval-flake) {
|
||||
assert equal (disko { mode: "mount", flake: "."}) {
|
||||
success: false,
|
||||
messages: [
|
||||
"Flake-uri . does not contain an attribute.",
|
||||
"Please append an attribute like \"#foo\" to the flake-uri."
|
||||
]
|
||||
{
|
||||
code: ERR_FLAKE_URI_NO_ATTRIBUTE,
|
||||
details: {
|
||||
flakeUri: "."
|
||||
}
|
||||
}
|
||||
],
|
||||
context: "evaluate flake"
|
||||
}
|
||||
|
||||
assert equal (disko { mode: "qwert" }) {
|
||||
success: false,
|
||||
messages: [
|
||||
{
|
||||
code: ERR_INVALID_MODE,
|
||||
details: {
|
||||
mode: "qwert",
|
||||
valid_modes: [
|
||||
"destroy",
|
||||
"format",
|
||||
"mount",
|
||||
"format,mount",
|
||||
"destroy,format,mount"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
context: "validate mode"
|
||||
}
|
||||
|
||||
assert equal (disko { mode: "mount" }) {
|
||||
success: false,
|
||||
messages: [
|
||||
{
|
||||
code: ERR_MISSING_ARGUMENTS
|
||||
}
|
||||
],
|
||||
context: "validate arguments"
|
||||
}
|
||||
|
||||
def main [] {
|
||||
|
||||
@@ -4,29 +4,55 @@ 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 with-context [context: string] {
|
||||
$in | insert context $context
|
||||
}
|
||||
|
||||
def main [
|
||||
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>>>>
|
||||
] {
|
||||
|
||||
if not ($args.mode in (modes)) {
|
||||
return {
|
||||
success: false,
|
||||
messages: [ { code: ERR_INVALID_MODE, details: { mode: $args.mode, valid_modes: (modes) } } ]
|
||||
context: "validate mode"
|
||||
}
|
||||
}
|
||||
|
||||
if not ($args.flake? != null xor $args.disko_file? != null) {
|
||||
return {
|
||||
success: false,
|
||||
messages: [ { code: ERR_MISSING_ARGUMENTS } ],
|
||||
context: "validate arguments"
|
||||
}
|
||||
}
|
||||
|
||||
let eval_result = if $args.disko_file? != null {
|
||||
$args.disko_file | eval-disko-file | with-context "evaluate config"
|
||||
} else {
|
||||
$args.flake | eval-flake | with-context "evaluate flake"
|
||||
}
|
||||
|
||||
# TODO: Continue implementation here
|
||||
$eval_result
|
||||
}
|
||||
|
||||
export 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)) {
|
||||
{
|
||||
success: false,
|
||||
messages: [ { code: ERR_INVALID_MODE, details: { mode: $mode, valid_modes: (modes) } } ]
|
||||
} | exit-on-error "validate mode"
|
||||
run {
|
||||
mode: $mode,
|
||||
disko_file: $disko_file,
|
||||
flake: $flake
|
||||
}
|
||||
|
||||
if not ($flake != null xor $disko_file != null) {
|
||||
{ success: false, messages: [ { code: ERR_MISSING_ARGUMENTS } ] } | exit-on-error "validate arguments"
|
||||
}
|
||||
|
||||
let config = if $disko_file != null {
|
||||
$disko_file | eval-disko-file | exit-on-error "evaluate config"
|
||||
} else {
|
||||
$flake | eval-flake | exit-on-error "evaluate flake"
|
||||
}
|
||||
|
||||
$config | to json | print-info
|
||||
| exit-on-error
|
||||
| to json | print-info
|
||||
}
|
||||
+8
-8
@@ -11,7 +11,7 @@ def value [] { ansi green } # Values that are allowed
|
||||
def reset [] { ansi reset } # Shortcut to reset the color
|
||||
|
||||
def "lookup ERR_INVALID_MODE" [mode: string, valid_modes: list] {
|
||||
let valid_modes = modes
|
||||
let modes_list = $valid_modes
|
||||
| each { |mode| $"(value)($mode)(reset)" }
|
||||
| str join ', '
|
||||
|
||||
@@ -22,7 +22,7 @@ def "lookup ERR_INVALID_MODE" [mode: string, valid_modes: list] {
|
||||
}
|
||||
{
|
||||
type: help
|
||||
msg: $"Valid modes are:\n($valid_modes)"
|
||||
msg: $"Valid modes are:\n($modes_list)"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -141,18 +141,18 @@ def print-message []: record -> nothing {
|
||||
$in | lookup-message | each { render-message }
|
||||
}
|
||||
|
||||
export def exit-on-error [context: string]: [
|
||||
record<success: bool, value: any> -> any
|
||||
record<success: bool, messages: list<record<code: string>>> -> nothing
|
||||
record<success: bool, messages: list<record<code: string, details: record<any>>>> -> nothing
|
||||
export def exit-on-error []: [
|
||||
record<success: bool, context: string, value: any> -> any
|
||||
record<success: bool, context: string, messages: list<record<code: string>>, context: string> -> nothing
|
||||
record<success: bool, context: string, messages: list<record<code: string, details: record<any>>>> -> nothing
|
||||
] {
|
||||
let result = $in
|
||||
if $result.success {
|
||||
log debug $"Success: ($context)"
|
||||
log debug $"Success: ($result.context)"
|
||||
log debug $"Return value: ($result.value)"
|
||||
return $result.value
|
||||
} else {
|
||||
log debug $"Failure: ($context)"
|
||||
log debug $"Failure: ($result.context)"
|
||||
}
|
||||
|
||||
for message in $in.messages {
|
||||
|
||||
Reference in new issue
Block a user