-
Notifications
You must be signed in to change notification settings - Fork 1
feat: implement flow mode for interactive commands in captain_utils.sh #458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
63517fc
406a319
92cc096
07640bf
cf8218f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,15 +6,117 @@ set -e | |||||||||||||||||||||||||||||
| set -u | ||||||||||||||||||||||||||||||
| set -o pipefail | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # --- Flow mode infrastructure --- | ||||||||||||||||||||||||||||||
| FLOW_MODE=false | ||||||||||||||||||||||||||||||
| FLOW_QUEUE=() | ||||||||||||||||||||||||||||||
| # Use a temp file for FLOW_INDEX so it persists across subshells created by $() | ||||||||||||||||||||||||||||||
| FLOW_INDEX_FILE=$(mktemp) | ||||||||||||||||||||||||||||||
| echo 0 > "$FLOW_INDEX_FILE" | ||||||||||||||||||||||||||||||
| trap 'rm -f "$FLOW_INDEX_FILE"' EXIT | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| _flow_get_index() { cat "$FLOW_INDEX_FILE"; } | ||||||||||||||||||||||||||||||
| _flow_set_index() { echo "$1" > "$FLOW_INDEX_FILE"; } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| while [[ $# -gt 0 ]]; do | ||||||||||||||||||||||||||||||
| case "$1" in | ||||||||||||||||||||||||||||||
| --flow) | ||||||||||||||||||||||||||||||
| if [[ $# -lt 2 ]] || [[ -z "$2" ]]; then | ||||||||||||||||||||||||||||||
| echo "Error: --flow requires a comma-separated queue argument" >&2 | ||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||
| FLOW_MODE=true | ||||||||||||||||||||||||||||||
| IFS=',' read -r -a FLOW_QUEUE <<< "$2" | ||||||||||||||||||||||||||||||
|
hamzabouissi marked this conversation as resolved.
|
||||||||||||||||||||||||||||||
| shift 2 | ||||||||||||||||||||||||||||||
| ;; | ||||||||||||||||||||||||||||||
|
hamzabouissi marked this conversation as resolved.
|
||||||||||||||||||||||||||||||
| *) | ||||||||||||||||||||||||||||||
| echo "Unknown option: $1" >&2 | ||||||||||||||||||||||||||||||
|
hamzabouissi marked this conversation as resolved.
|
||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||
| ;; | ||||||||||||||||||||||||||||||
| esac | ||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Wrapper for gum choose. In flow mode, consumes next queue value as 1-based index. | ||||||||||||||||||||||||||||||
| # In interactive mode, shows indexed options and strips index prefix from result. | ||||||||||||||||||||||||||||||
| flow_choose() { | ||||||||||||||||||||||||||||||
| local options=("$@") | ||||||||||||||||||||||||||||||
| if [ "$FLOW_MODE" = true ]; then | ||||||||||||||||||||||||||||||
| local flow_index=$(_flow_get_index) | ||||||||||||||||||||||||||||||
| if [ "$flow_index" -ge "${#FLOW_QUEUE[@]}" ]; then | ||||||||||||||||||||||||||||||
| echo "Flow complete." >&2 | ||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||
| local idx="${FLOW_QUEUE[$flow_index]}" | ||||||||||||||||||||||||||||||
| _flow_set_index $((flow_index + 1)) | ||||||||||||||||||||||||||||||
| if ! [[ "$idx" =~ ^[0-9]+$ ]] || [ "$idx" -lt 1 ] || [ "$idx" -gt "${#options[@]}" ]; then | ||||||||||||||||||||||||||||||
| echo "Invalid flow index '$idx'. Valid range: 1-${#options[@]} for options: ${options[*]}" >&2 | ||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+52
|
||||||||||||||||||||||||||||||
| exit 1 | |
| fi | |
| local idx="${FLOW_QUEUE[$flow_index]}" | |
| _flow_set_index $((flow_index + 1)) | |
| if ! [[ "$idx" =~ ^[0-9]+$ ]] || [ "$idx" -lt 1 ] || [ "$idx" -gt "${#options[@]}" ]; then | |
| echo "Invalid flow index '$idx'. Valid range: 1-${#options[@]} for options: ${options[*]}" >&2 | |
| exit 1 | |
| return 1 | |
| fi | |
| local idx="${FLOW_QUEUE[$flow_index]}" | |
| _flow_set_index $((flow_index + 1)) | |
| if ! [[ "$idx" =~ ^[0-9]+$ ]] || [ "$idx" -lt 1 ] || [ "$idx" -gt "${#options[@]}" ]; then | |
| echo "Invalid flow index '$idx'. Valid range: 1-${#options[@]} for options: ${options[*]}" >&2 | |
| return 1 |
Copilot
AI
Apr 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flow_confirm exits the script (exit 0) when the flow queue is exhausted. This makes flow_confirm unsafe to use in conditionals (e.g. if ! flow_confirm ...) because it can terminate the whole script mid-function. Prefer returning a status (e.g. return 1/return 0) and let the caller decide what to do when the flow runs out.
| exit 0 | |
| return 1 |
Copilot
AI
Apr 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still relies on eval to run the constructed helm_diff_cmd. Since version/other fields can come from external commands (yq, gh release list, etc.), eval can mis-handle quoting or allow unintended shell interpretation. Prefer building the command as an array and executing it directly (and then piping to flow_pager) to avoid injection/quoting bugs.
Copilot
AI
Apr 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handle_calico_upgrades re-runs helm repo add projectcalico + helm repo update even though run_prerequisite_commands already adds that repo and updates at script startup. This adds latency to every calico run; consider removing the duplicate update here or guarding it (e.g. only add if missing, and avoid an extra update unless needed).
| helm repo add projectcalico https://docs.tigera.io/calico/charts | |
| helm repo update | |
| if ! helm repo list | awk '{print $1}' | grep -Fxq projectcalico; then | |
| helm repo add projectcalico https://docs.tigera.io/calico/charts | |
| helm repo update | |
| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FLOW_INDEX_FILEis created unconditionally at startup even when--flowisn’t used. This adds needless temp-file churn and makes behavior less predictable if the script is sourced. Consider creating the temp file (and installing the trap) only whenFLOW_MODEis enabled.