#!/bin/bash set -e # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color # ----------------------------------------------------------------------------- # Input Framework # ----------------------------------------------------------------------------- # Ensure we have a tty for input if [ ! -e /dev/tty ]; then echo "Error: Script must be run in an interactive terminal (cannot find /dev/tty)." >&2 exit 1 fi # Function to prompt for user input # Usage: get_input "VARIABLE_NAME" "Prompt Text" "Default Value" "is_secret(true/false)" get_input() { local var_name="$1" local prompt_text="$2" local default_value="$3" local is_secret="$4" local input_val="" local prompt_full="${GREEN}${prompt_text}${NC}" if [ -n "$default_value" ]; then prompt_full+=" ${YELLOW}[$default_value]${NC}" fi prompt_full+=": " while true; do # Print prompt to stderr so it shows up even if stdout is redirected if [ "$is_secret" == "true" ]; then echo -ne "$prompt_full" >&2 read -s input_val < /dev/tty echo "" >&2 # Newline after secret input else echo -ne "$prompt_full" >&2 read input_val < /dev/tty fi # Use default if input is empty if [ -z "$input_val" ] && [ -n "$default_value" ]; then input_val="$default_value" fi # Validation: Require input if no default exists if [ -z "$input_val" ]; then echo -e "${RED}Error: This value is required.${NC}" >&2 else break fi done # Set the variable dynamically in the parent scope printf -v "$var_name" "%s" "$input_val" } # Function to confirm collected inputs # Usage: confirm_inputs "VAR1" "VAR2" "VAR3" ... confirm_inputs() { echo "" >&2 echo -e "${GREEN}=== Configuration Summary ===${NC}" >&2 for var in "$@"; do local val="${!var}" # Mask secrets in summary if needed, or just show length # For now, just printing value. # To improve: pass a list of secret vars to mask them. echo -e "${YELLOW}$var:${NC} $val" >&2 done echo "" >&2 get_input "CONFIRM" "Is this correct? (y/n)" "y" "false" if [[ "${CONFIRM,,}" != "y" ]]; then echo -e "${RED}Aborted by user.${NC}" >&2 exit 1 fi } # ----------------------------------------------------------------------------- # Script Logic # ----------------------------------------------------------------------------- echo "Starting Interactive Setup..." echo "-----------------------------" # 1. Collect Inputs # Example: get_input "HOST_NAME" "Enter Hostname" "$(hostname)" "false" # get_input "ADMIN_PASS" "Enter Admin Password" "" "true" # 2. Confirm # confirm_inputs "HOST_NAME" "ADMIN_PASS" # 3. Execute # echo "Configuring $HOST_NAME..."