diff --git a/bin/talos b/bin/talos old mode 100644 new mode 100755 index 63ec759..f8ceccd --- a/bin/talos +++ b/bin/talos @@ -1,27 +1,18 @@ #!/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" +COMMAND="$(basename "$0")" + # 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() { +contains() { local seeking=$1; shift for element; do [[ "$element" == "$seeking" ]] && return 0 @@ -29,10 +20,39 @@ function contains() { return 1 } +print_help() { + cat < [node] + +Description: + Wrapper for talosctl with context and optional node selection. + +Examples: + $COMMAND home dashboard + $COMMAND home talos-nuc-1 dashboard + $COMMAND zl-erm talos-geekom-1 kubeconfig + +Arguments: + context Required. Must match a configured Talos context. + node Optional. Node name or short name. + command Any talosctl command. + +Commands: + help, -h, --help Show this help message +EOF +} + +# Help handling +if [[ "$1" == "help" || "$1" == "-h" || "$1" == "--help" ]]; then + print_help + exit 0 +fi + # Require context if [ -z "$1" ]; then echo "Error: Context is required." - echo "Usage: talos [node] " + print_help exit 1 fi @@ -47,23 +67,30 @@ fi CONTEXT="$1" shift -# Load nodes for context mapfile -t NODES < <(yq e ".contexts.${CONTEXT}.nodes[]" "$CONFIG_FILE") NODE="" +NODE_INPUT="" -# Check if next arg is a node if [ -n "$1" ]; then + NODE_INPUT="$1" + for n in "${NODES[@]}"; do - if [[ "$n" == "$1" || "${n%%.*}" == "$1" ]]; then + if [[ "$n" == "$NODE_INPUT" || "${n%%.*}" == "$NODE_INPUT" ]]; then NODE="$n" shift break fi done + + if [ -n "$NODE_INPUT" ] && [ -z "$NODE" ]; then + echo "Error: Node '$NODE_INPUT' not found in context '$CONTEXT'" + echo "Available nodes:" + printf ' - %s\n' "${NODES[@]}" + exit 1 + fi fi -# Execute if [ -n "$NODE" ]; then talosctl --context "$CONTEXT" -n "$NODE" -e "$NODE" "$@" else