93 lines
2.3 KiB
Plaintext
93 lines
2.3 KiB
Plaintext
![]() |
# Enhanced validation utilities for provisioning tool
|
||
|
|
||
|
export def validate-required [
|
||
|
value: any
|
||
|
name: string
|
||
|
context?: string
|
||
|
]: bool {
|
||
|
if ($value | is-empty) {
|
||
|
print $"π Required parameter '($name)' is missing or empty"
|
||
|
if ($context | is-not-empty) {
|
||
|
print $"Context: ($context)"
|
||
|
}
|
||
|
print $"π‘ Please provide a value for '($name)'"
|
||
|
return false
|
||
|
}
|
||
|
true
|
||
|
}
|
||
|
|
||
|
export def validate-path [
|
||
|
path: string
|
||
|
context?: string
|
||
|
--must-exist
|
||
|
]: bool {
|
||
|
if ($path | is-empty) {
|
||
|
print "π Path parameter is empty"
|
||
|
if ($context | is-not-empty) {
|
||
|
print $"Context: ($context)"
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if $must_exist and not ($path | path exists) {
|
||
|
print $"π Path '($path)' does not exist"
|
||
|
if ($context | is-not-empty) {
|
||
|
print $"Context: ($context)"
|
||
|
}
|
||
|
print "π‘ Check if the path exists and you have proper permissions"
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
true
|
||
|
}
|
||
|
|
||
|
export def validate-command [
|
||
|
command: string
|
||
|
context?: string
|
||
|
]: bool {
|
||
|
let cmd_exists = (^bash -c $"type -P ($command)" | complete)
|
||
|
if $cmd_exists.exit_code != 0 {
|
||
|
print $"π Command '($command)' not found in PATH"
|
||
|
if ($context | is-not-empty) {
|
||
|
print $"Context: ($context)"
|
||
|
}
|
||
|
print $"π‘ Install '($command)' or add it to your PATH"
|
||
|
return false
|
||
|
}
|
||
|
true
|
||
|
}
|
||
|
|
||
|
export def safe-execute [
|
||
|
command: closure
|
||
|
context: string
|
||
|
--fallback: closure
|
||
|
]: any {
|
||
|
try {
|
||
|
do $command
|
||
|
} catch {|err|
|
||
|
print $"β οΈ Warning: Error in ($context): ($err.msg)"
|
||
|
if $fallback != null {
|
||
|
print "π Executing fallback..."
|
||
|
do $fallback
|
||
|
} else {
|
||
|
print $"π Execution failed in ($context)"
|
||
|
print $"Error: ($err.msg)"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export def validate-settings [
|
||
|
settings: record
|
||
|
required_fields: list
|
||
|
]: bool {
|
||
|
let missing_fields = ($required_fields | where {|field|
|
||
|
($settings | get -o $field | is-empty)
|
||
|
})
|
||
|
|
||
|
if ($missing_fields | length) > 0 {
|
||
|
print "π Missing required settings fields:"
|
||
|
$missing_fields | each {|field| print $" - ($field)"}
|
||
|
return false
|
||
|
}
|
||
|
true
|
||
|
}
|