File size: 5,958 Bytes
979853c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | # Prompt helpers for interactive setup.
CLEAR_INPUT_SENTINEL="__LIGHTRAG_CLEAR__"
_truncate_for_display() {
local value="$1"
local max=50
if [[ ${#value} -gt $max ]]; then
printf '%s' "${value:0:$max}..."
else
printf '%s' "$value"
fi
}
mask_sensitive_input() {
local prompt="$1"
local value
read -r -p "$prompt" value
printf '%s' "$value"
}
prompt_secret_with_default() {
local prompt="$1"
local default="${2:-}"
local value
if [[ -n "$default" ]]; then
local display_default
display_default="$(_truncate_for_display "$default")"
read -r -p "$prompt [${display_default}]: " value
else
read -r -p "$prompt" value
fi
if [[ -z "$value" ]]; then
value="$default"
fi
printf '%s' "$value"
}
prompt_clearable_with_default() {
local prompt="$1"
local default="${2:-}"
local value
local prompt_text="$prompt"
if [[ -n "$default" ]]; then
prompt_text="$prompt (Enter to keep, type 'clear' to remove)"
else
prompt_text="$prompt (type 'clear' to remove)"
fi
value="$(prompt_with_default "$prompt_text" "$default")"
if [[ "${value,,}" == "clear" ]]; then
printf '%s' "$CLEAR_INPUT_SENTINEL"
return 0
fi
printf '%s' "$value"
}
prompt_clearable_secret_with_default() {
local prompt="$1"
local default="${2:-}"
local value
local prompt_text="$prompt"
if [[ -n "$default" ]]; then
prompt_text="$prompt (Enter to keep, type 'clear' to remove)"
else
prompt_text="$prompt (type 'clear' to remove)"
fi
value="$(prompt_secret_with_default "$prompt_text" "$default")"
if [[ "${value,,}" == "clear" ]]; then
printf '%s' "$CLEAR_INPUT_SENTINEL"
return 0
fi
printf '%s' "$value"
}
prompt_with_default() {
local prompt="$1"
local default="$2"
local value
if [[ -n "$default" ]]; then
read -r -p "$prompt [$default]: " value
else
read -r -p "$prompt: " value
fi
if [[ -z "$value" ]]; then
value="$default"
fi
printf '%s' "$value"
}
style_prompt_text() {
local prompt="$1"
if [[ -n "${COLOR_YELLOW:-}" && "$prompt" == *Docker* ]]; then
prompt="${prompt//Docker/${COLOR_YELLOW}Docker${COLOR_RESET}}"
fi
printf '%s' "$prompt"
}
confirm_default_no() {
local prompt="$1"
local response
local styled_prompt
styled_prompt="$(style_prompt_text "$prompt")"
while true; do
read -r -n 1 -p "$styled_prompt [y/N]: " response
echo
case "$response" in
y|Y) return 0 ;;
n|N|"") return 1 ;;
esac
done
}
confirm_default_yes() {
local prompt="$1"
local response
local styled_prompt
styled_prompt="$(style_prompt_text "$prompt")"
while true; do
read -r -n 1 -p "$styled_prompt [Y/n]: " response
echo
case "$response" in
y|Y|"") return 0 ;;
n|N) return 1 ;;
esac
done
}
confirm_required_yes_no() {
local prompt="$1"
local response
local styled_prompt
styled_prompt="$(style_prompt_text "$prompt")"
while true; do
printf '%b' "$styled_prompt [yes/no]: " >&2
read -r response
case "${response,,}" in
yes) return 0 ;;
no) return 1 ;;
*)
echo "Please type 'yes' or 'no'." >&2
;;
esac
done
}
prompt_until_valid() {
local prompt="$1"
local default="$2"
local validator="$3"
shift 3
local value
while true; do
value="$(prompt_with_default "$prompt" "$default")"
if "$validator" "$value" "$@"; then
printf '%s' "$value"
return 0
fi
echo "Invalid value. Please try again."
done
}
prompt_secret_until_valid() {
local prompt="$1"
local validator="$2"
shift 2
local value
while true; do
value="$(mask_sensitive_input "$prompt")"
if "$validator" "$value" "$@"; then
printf '%s' "$value"
return 0
fi
echo "Invalid value. Please try again."
done
}
prompt_secret_until_valid_with_default() {
local prompt="$1"
local default="$2"
local validator="$3"
shift 3
local value
while true; do
value="$(prompt_secret_with_default "$prompt" "$default")"
if "$validator" "$value" "$@"; then
printf '%s' "$value"
return 0
fi
echo "Invalid value. Please try again."
done
}
prompt_required_secret() {
local prompt="$1"
local value
while true; do
value="$(mask_sensitive_input "$prompt")"
if [[ -n "$value" ]]; then
printf '%s' "$value"
return 0
fi
echo "Value cannot be empty. Please try again."
done
}
prompt_choice() {
local prompt="$1"
local default="$2"
shift 2
local options=("$@")
local choice
local index=1
local default_index=""
local count="${#options[@]}"
for option in "${options[@]}"; do
if [[ "$option" == "$default" ]]; then
default_index="$index"
fi
index=$((index + 1))
done
while true; do
printf '%s\n' "${COLOR_BLUE}${prompt}${COLOR_RESET} options:" >&2
index=1
for option in "${options[@]}"; do
if [[ "$index" == "$default_index" ]]; then
printf ' %s) %s%s%s\n' \
"${COLOR_GREEN}${index}${COLOR_RESET}" \
"${COLOR_YELLOW}" \
"$option" \
"${COLOR_RESET}" >&2
else
printf ' %s) %s\n' "${COLOR_GREEN}${index}${COLOR_RESET}" "$option" >&2
fi
index=$((index + 1))
done
if [[ -n "$default_index" ]]; then
printf 'Enter number (default: %s): ' "$default_index" >&2
else
printf 'Enter number: ' >&2
fi
if ((count <= 9)); then
read -r -n 1 choice
printf '\n' >&2
else
read -r choice
fi
if [[ -z "$choice" ]]; then
if [[ -n "$default_index" ]]; then
printf '%s' "${options[default_index-1]}"
return 0
fi
elif [[ "$choice" =~ ^[0-9]+$ ]] && ((choice >= 1 && choice <= count)); then
printf '%s' "${options[choice-1]}"
return 0
fi
printf '%s\n' "${COLOR_YELLOW}Invalid selection.${COLOR_RESET} Please enter a number between 1 and ${count}." >&2
done
}
|