158 lines
4.7 KiB
Plaintext
158 lines
4.7 KiB
Plaintext
|
|
# Utility Command Handlers
|
||
|
|
# Handles: ssh, sed, sops, cache, providers, nu, list, qr
|
||
|
|
|
||
|
|
use ../flags.nu *
|
||
|
|
use ../../lib_provisioning *
|
||
|
|
use ../../servers/ssh.nu *
|
||
|
|
use ../../servers/utils.nu *
|
||
|
|
|
||
|
|
# Helper to run module commands
|
||
|
|
def run_module [
|
||
|
|
args: string
|
||
|
|
module: string
|
||
|
|
option?: string
|
||
|
|
--exec
|
||
|
|
] {
|
||
|
|
let use_debug = if ($env.PROVISIONING_DEBUG? | default false) { "-x" } else { "" }
|
||
|
|
|
||
|
|
if $exec {
|
||
|
|
exec $"($env.PROVISIONING_NAME)" $use_debug -mod $module ($option | default "") $args
|
||
|
|
} else {
|
||
|
|
^$"($env.PROVISIONING_NAME)" $use_debug -mod $module ($option | default "") $args
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Main utility command dispatcher
|
||
|
|
export def handle_utility_command [
|
||
|
|
command: string
|
||
|
|
ops: string
|
||
|
|
flags: record
|
||
|
|
] {
|
||
|
|
match $command {
|
||
|
|
"ssh" => { handle_ssh $flags }
|
||
|
|
"sed" | "sops" => { handle_sops_edit $command $ops $flags }
|
||
|
|
"cache" => { handle_cache $flags }
|
||
|
|
"providers" => { handle_providers }
|
||
|
|
"nu" => { handle_nu $ops $flags }
|
||
|
|
"list" | "l" | "ls" => { handle_list $ops $flags }
|
||
|
|
"qr" => { handle_qr }
|
||
|
|
"nuinfo" => { handle_nuinfo }
|
||
|
|
"plugin" | "plugins" => { handle_plugins }
|
||
|
|
_ => {
|
||
|
|
print $"❌ Unknown utility command: ($command)"
|
||
|
|
exit 1
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# SSH command handler
|
||
|
|
def handle_ssh [flags: record] {
|
||
|
|
let curr_settings = (find_get_settings --infra $flags.infra --settings $flags.settings $flags.include_notuse)
|
||
|
|
rm -rf $curr_settings.wk_path
|
||
|
|
server_ssh $curr_settings "" "pub" false
|
||
|
|
}
|
||
|
|
|
||
|
|
# SOPS edit command handler
|
||
|
|
def handle_sops_edit [task: string, ops: string, flags: record] {
|
||
|
|
let pos = if $task == "sed" { 0 } else { 1 }
|
||
|
|
let ops_parts = ($ops | split row " ")
|
||
|
|
let target_file = ($ops_parts | get -o $pos | default "")
|
||
|
|
|
||
|
|
if ($target_file | is-empty) {
|
||
|
|
throw-error $"🛑 No file found" $"for (_ansi yellow_bold)sops(_ansi reset) edit"
|
||
|
|
exit -1
|
||
|
|
}
|
||
|
|
|
||
|
|
let target_full_path = if not ($target_file | path exists) {
|
||
|
|
let infra_path = (get_infra $flags.infra)
|
||
|
|
let candidate = ($infra_path | path join $target_file)
|
||
|
|
if ($candidate | path exists) {
|
||
|
|
$candidate
|
||
|
|
} else {
|
||
|
|
throw-error $"🛑 No file (_ansi green_italic)($target_file)(_ansi reset) found" $"for (_ansi yellow_bold)sops(_ansi reset) edit"
|
||
|
|
exit -1
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$target_file
|
||
|
|
}
|
||
|
|
|
||
|
|
# Setup SOPS environment if needed
|
||
|
|
if ($env.PROVISIONING_SOPS? | is-empty) {
|
||
|
|
let curr_settings = (find_get_settings --infra $flags.infra --settings $flags.settings $flags.include_notuse)
|
||
|
|
rm -rf $curr_settings.wk_path
|
||
|
|
$env.CURRENT_INFRA_PATH = ($curr_settings.infra_path | path join $curr_settings.infra)
|
||
|
|
use ../../sops_env.nu
|
||
|
|
}
|
||
|
|
|
||
|
|
if $task == "sed" {
|
||
|
|
on_sops "sed" $target_full_path
|
||
|
|
} else {
|
||
|
|
on_sops $task $target_full_path ($ops_parts | skip 1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Cache command handler
|
||
|
|
def handle_cache [flags: record] {
|
||
|
|
let str_infra = if ($flags.infra | is-not-empty) { $"--infra ($flags.infra) " } else { "" }
|
||
|
|
let str_outfile = if ($flags.outfile | is-not-empty) { $"--outfile ($flags.outfile) " } else { "" }
|
||
|
|
let str_out = if ($flags.output_format | is-not-empty) { $"--out ($flags.output_format) " } else { "" }
|
||
|
|
run_module $"($str_infra) ($str_out) ($str_outfile)" "server" "cache"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Providers list command handler
|
||
|
|
def handle_providers [] {
|
||
|
|
_print $"(_ansi green)PROVIDERS(_ansi reset) list: \n"
|
||
|
|
_print (providers_list "selection" | to json) "json" "result" "table"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Nu shell command handler
|
||
|
|
def handle_nu [ops: string, flags: record] {
|
||
|
|
let run_ops = if ($ops | str trim | str starts-with "-") {
|
||
|
|
""
|
||
|
|
} else {
|
||
|
|
($ops | split row " " | get -o 0 | default "")
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($flags.infra | is-not-empty) and ($env.PROVISIONING_INFRA_PATH | path join $flags.infra | path exists) {
|
||
|
|
cd ($env.PROVISIONING_INFRA_PATH | path join $flags.infra)
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($flags.output_format | is-empty) {
|
||
|
|
if ($run_ops | is-empty) {
|
||
|
|
print (
|
||
|
|
$"\nTo exit (_ansi purple_bold)NuShell(_ansi reset) session, with (_ansi default_dimmed)lib_provisioning(_ansi reset) loaded, " +
|
||
|
|
$"use (_ansi green_bold)exit(_ansi reset) or (_ansi green_bold)[CTRL-D](_ansi reset)"
|
||
|
|
)
|
||
|
|
^nu -i -e $"use lib_provisioning * ; use env.nu * ; show_titles;"
|
||
|
|
} else {
|
||
|
|
^nu -c $"($run_ops)"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# List command handler
|
||
|
|
def handle_list [ops: string, flags: record] {
|
||
|
|
let target_list = if ($ops | is-not-empty) {
|
||
|
|
($ops | split row " " | get -o 0 | default "")
|
||
|
|
} else { "" }
|
||
|
|
|
||
|
|
let list_ops = ($ops | str replace $"($target_list) " "" | str trim)
|
||
|
|
on_list $target_list ($flags.onsel | default "") $list_ops
|
||
|
|
}
|
||
|
|
|
||
|
|
# QR code command handler
|
||
|
|
def handle_qr [] {
|
||
|
|
make_qr
|
||
|
|
}
|
||
|
|
|
||
|
|
# Nu info command handler
|
||
|
|
def handle_nuinfo [] {
|
||
|
|
print $"\n (_ansi yellow)Nu shell info(_ansi reset)"
|
||
|
|
print (version)
|
||
|
|
}
|
||
|
|
|
||
|
|
# Plugins command handler
|
||
|
|
def handle_plugins [] {
|
||
|
|
print $"\n (_ansi yellow)Nu shell Plugins(_ansi reset)"
|
||
|
|
^nu -c "plugin list"
|
||
|
|
}
|