mirror of
https://github.com/nix-community/disko.git
synced 2026-12-04 15:03: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
|
#!/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
|
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,
|
success: true,
|
||||||
value: {
|
value: {
|
||||||
"disk": {
|
"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,
|
success: true,
|
||||||
value: {
|
value: {
|
||||||
"disk": {
|
"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,
|
success: true,
|
||||||
value: {
|
value: {
|
||||||
"disk": {
|
"disk": {
|
||||||
@@ -262,12 +265,47 @@ assert equal (".#testmachine" | disko eval-flake) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert equal ("." | disko eval-flake) {
|
assert equal (disko { mode: "mount", flake: "."}) {
|
||||||
success: false,
|
success: false,
|
||||||
messages: [
|
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 [] {
|
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 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'
|
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
|
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
|
--flake (-f): string # Flake URI to search for the disko configuration
|
||||||
]: nothing -> nothing {
|
]: nothing -> nothing {
|
||||||
|
|
||||||
if not ($mode in (modes)) {
|
run {
|
||||||
{
|
mode: $mode,
|
||||||
success: false,
|
disko_file: $disko_file,
|
||||||
messages: [ { code: ERR_INVALID_MODE, details: { mode: $mode, valid_modes: (modes) } } ]
|
flake: $flake
|
||||||
} | exit-on-error "validate mode"
|
|
||||||
}
|
}
|
||||||
|
| exit-on-error
|
||||||
if not ($flake != null xor $disko_file != null) {
|
| to json | print-info
|
||||||
{ 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
|
|
||||||
}
|
}
|
||||||
+8
-8
@@ -11,7 +11,7 @@ def value [] { ansi green } # Values that are allowed
|
|||||||
def reset [] { ansi reset } # Shortcut to reset the color
|
def reset [] { ansi reset } # Shortcut to reset the color
|
||||||
|
|
||||||
def "lookup ERR_INVALID_MODE" [mode: string, valid_modes: list] {
|
def "lookup ERR_INVALID_MODE" [mode: string, valid_modes: list] {
|
||||||
let valid_modes = modes
|
let modes_list = $valid_modes
|
||||||
| each { |mode| $"(value)($mode)(reset)" }
|
| each { |mode| $"(value)($mode)(reset)" }
|
||||||
| str join ', '
|
| str join ', '
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ def "lookup ERR_INVALID_MODE" [mode: string, valid_modes: list] {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
type: help
|
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 }
|
$in | lookup-message | each { render-message }
|
||||||
}
|
}
|
||||||
|
|
||||||
export def exit-on-error [context: string]: [
|
export def exit-on-error []: [
|
||||||
record<success: bool, value: any> -> any
|
record<success: bool, context: string, value: any> -> any
|
||||||
record<success: bool, messages: list<record<code: string>>> -> nothing
|
record<success: bool, context: string, messages: list<record<code: string>>, context: string> -> nothing
|
||||||
record<success: bool, messages: list<record<code: string, details: record<any>>>> -> nothing
|
record<success: bool, context: string, messages: list<record<code: string, details: record<any>>>> -> nothing
|
||||||
] {
|
] {
|
||||||
let result = $in
|
let result = $in
|
||||||
if $result.success {
|
if $result.success {
|
||||||
log debug $"Success: ($context)"
|
log debug $"Success: ($result.context)"
|
||||||
log debug $"Return value: ($result.value)"
|
log debug $"Return value: ($result.value)"
|
||||||
return $result.value
|
return $result.value
|
||||||
} else {
|
} else {
|
||||||
log debug $"Failure: ($context)"
|
log debug $"Failure: ($result.context)"
|
||||||
}
|
}
|
||||||
|
|
||||||
for message in $in.messages {
|
for message in $in.messages {
|
||||||
|
|||||||
Reference in new issue
Block a user