72 lines
1.5 KiB
Bash
72 lines
1.5 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
### Usage ###
|
|
# talos context [optional-nodename] command
|
|
# Example:
|
|
# talos home dashboard
|
|
# talos home dashboard
|
|
# talos home talos-nuc-1 dashboard
|
|
|
|
# If wrong context or missing this script will output which contexts are available
|
|
|
|
CONFIG_FILE="$HOME/.talos/config"
|
|
|
|
# Check yq
|
|
if ! command -v yq &>/dev/null; then
|
|
echo "Error: yq (Go version v4+) not found."
|
|
exit 1
|
|
fi
|
|
|
|
# Read all contexts
|
|
mapfile -t CONTEXTS < <(yq e '.contexts | keys | .[]' "$CONFIG_FILE")
|
|
|
|
# Helper: check if value exists in array
|
|
function contains() {
|
|
local seeking=$1; shift
|
|
for element; do
|
|
[[ "$element" == "$seeking" ]] && return 0
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# Require context
|
|
if [ -z "$1" ]; then
|
|
echo "Error: Context is required."
|
|
echo "Usage: talos <context> [node] <command>"
|
|
exit 1
|
|
fi
|
|
|
|
# Validate context
|
|
if ! contains "$1" "${CONTEXTS[@]}"; then
|
|
echo "Error: Invalid context '$1'"
|
|
echo "Available contexts:"
|
|
printf ' - %s\n' "${CONTEXTS[@]}"
|
|
exit 1
|
|
fi
|
|
|
|
CONTEXT="$1"
|
|
shift
|
|
|
|
# Load nodes for context
|
|
mapfile -t NODES < <(yq e ".contexts.${CONTEXT}.nodes[]" "$CONFIG_FILE")
|
|
|
|
NODE=""
|
|
|
|
# Check if next arg is a node
|
|
if [ -n "$1" ]; then
|
|
for n in "${NODES[@]}"; do
|
|
if [[ "$n" == "$1" || "${n%%.*}" == "$1" ]]; then
|
|
NODE="$n"
|
|
shift
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# Execute
|
|
if [ -n "$NODE" ]; then
|
|
talosctl --context "$CONTEXT" -n "$NODE" -e "$NODE" "$@"
|
|
else
|
|
talosctl --context "$CONTEXT" "$@"
|
|
fi
|