101 lines
3.2 KiB
Text
101 lines
3.2 KiB
Text
#!/usr/bin/env nu
|
|
|
|
# Provider Common Utilities - Shared patterns across all providers
|
|
#
|
|
# This module consolidates common operations used by all cloud providers:
|
|
# - Configuration loading
|
|
# - Argument parsing
|
|
# - Server resolution
|
|
# - Help text routing
|
|
# - Command execution with error handling
|
|
|
|
# Load provisioning configuration from infra/settings paths
|
|
#
|
|
# Handles the common pattern of loading settings with various path combinations.
|
|
# Used by all providers in their main entry point.
|
|
export def provider_load_config [infra?: string, settings?: string] {
|
|
if $infra != null {
|
|
if $settings != null {
|
|
(load_settings --infra $infra --settings $settings)
|
|
} else {
|
|
(load_settings --infra $infra)
|
|
}
|
|
} else {
|
|
if $settings != null {
|
|
(load_settings --settings $settings)
|
|
} else {
|
|
(load_settings)
|
|
}
|
|
}
|
|
}
|
|
|
|
# Parse command arguments into task, target, and remaining args
|
|
export def provider_parse_args [args: list<string>] {
|
|
let task = ($args | get -o 0 | default "")
|
|
let target = if ($args | length) > 1 { ($args | get -o 1) } else { "" }
|
|
let cmd_args = if ($args | length) > 1 { ($args | drop nth ..1) } else { [] }
|
|
{task: $task, target: $target, cmd_args: $cmd_args}
|
|
}
|
|
|
|
# Resolve server target from multiple sources
|
|
export def provider_resolve_server [target: string, server?: record, settings?: record, error_exit: bool = false] {
|
|
let resolved = if $server != null {
|
|
$server
|
|
} else if $settings != null {
|
|
($settings.data.servers | where {|it| $it.hostname == $target } | get -o 0)
|
|
} else {
|
|
null
|
|
}
|
|
|
|
if $resolved == null and $error_exit {
|
|
(throw-error "Server not found" $target "provider_resolve_server" --span (metadata $resolved).span)
|
|
}
|
|
|
|
$resolved
|
|
}
|
|
|
|
# Execute command with standard error handling
|
|
export def provider_execute [cmd: string, context: string = "", suppress_error: bool = false] {
|
|
let result = (^$cmd | complete)
|
|
if $result.exit_code != 0 and not $suppress_error {
|
|
if (is-debug-enabled) {
|
|
(throw-error $"Command failed: ($context)" $"Exit code: ($result.exit_code)" "" --span (metadata $result).span)
|
|
} else {
|
|
print $"Error ($context): ($result.exit_code)"
|
|
}
|
|
}
|
|
$result
|
|
}
|
|
|
|
# Check if help was requested in arguments
|
|
export def provider_has_help_request [args: list<string>] {
|
|
($args | find "help" | length) > 0
|
|
}
|
|
|
|
# Format IP type for consistent handling
|
|
export def provider_normalize_ip_type [ip_type: string = "public"] {
|
|
match $ip_type {
|
|
"private" | "prv" | "priv" => "private",
|
|
"public" | "pub" | "ipv4" => "public",
|
|
"ipv6" => "ipv6",
|
|
_ => $ip_type
|
|
}
|
|
}
|
|
|
|
# Standard exit code interpretation
|
|
export def provider_interpret_exit_code [exit_code: int] {
|
|
match $exit_code {
|
|
0 => "success",
|
|
1 => "general error",
|
|
2 => "misuse of shell command",
|
|
127 => "command not found",
|
|
130 => "terminated by signal",
|
|
137 => "killed by signal",
|
|
_ => $"unknown error ($exit_code)"
|
|
}
|
|
}
|
|
|
|
# Check if we should use error_exit semantics
|
|
export def provider_should_error_exit [error_exit: bool = false, debug_mode: bool = false] {
|
|
$error_exit and (not $debug_mode)
|
|
}
|