mirror of
https://github.com/nix-community/disko
synced 2024-11-14 16:17:06 +00:00
disko2: Split into modules
This commit is contained in:
parent
94806e4506
commit
62581f38f5
5 changed files with 97 additions and 93 deletions
94
disko.nu
94
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"]}
|
||||
|
||||
|
|
26
lib/errors.nu
Normal file
26
lib/errors.nu
Normal file
|
@ -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)"
|
||||
}
|
68
lib/eval-config.nu
Normal file
68
lib/eval-config.nu
Normal file
|
@ -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 }
|
||||
}
|
2
lib/mod.nu
Normal file
2
lib/mod.nu
Normal file
|
@ -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]
|
Loading…
Reference in a new issue