-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaws-pipeline-cli.sh
More file actions
353 lines (334 loc) · 11.9 KB
/
Copy pathaws-pipeline-cli.sh
File metadata and controls
353 lines (334 loc) · 11.9 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env bash
VERSION="v1.0.3" # Update this on each release
github_repo="ddjain/aws-pipeline-cli"
script_path="/usr/local/bin/aws-codepipeline-cli"
# Self-update logic
check_for_update() {
latest=$(curl -fsSL "https://api.github.com/repos/$github_repo/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [[ -z "$latest" ]]; then
return
fi
if [[ "$latest" != "$VERSION" ]]; then
echo "A new version ($latest) is available! You are running $VERSION."
echo "Run 'aws-codepipeline-cli update' to upgrade."
fi
}
self_update() {
latest=$(curl -fsSL "https://api.github.com/repos/$github_repo/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [[ -z "$latest" ]]; then
echo "Could not fetch latest version."
exit 1
fi
echo "Updating to version $latest..."
sudo curl -fsSL "https://raw.githubusercontent.com/$github_repo/refs/tags/$latest/aws-pipeline-cli.sh" -o "$script_path"
sudo chmod +x "$script_path"
echo "Update complete! Now running version $latest."
exit 0
}
# Default values
PROFILE=""
COLUMNS=2
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--profile)
if [[ -n "$2" && "$2" != --* ]]; then
PROFILE="$2"
shift 2
else
echo "Error: --profile requires a value."
echo "Usage: aws-codepipeline-cli --profile <profile> [--columns <columns>]"
exit 1
fi
;;
--columns)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ && "$2" -gt 0 ]]; then
COLUMNS="$2"
shift 2
else
echo "Error: --columns requires a positive integer value."
echo "Usage: aws-codepipeline-cli [--profile <profile>] [--columns <columns>]"
exit 1
fi
;;
--help)
echo "Usage: aws-codepipeline-cli [--profile <profile>] [--columns <columns>]"
echo " --profile <profile> AWS CLI profile to use (optional, default: default profile)"
echo " --columns <columns> Number of columns in the grid (optional, default: 2)"
echo " --help Show this help message and exit"
echo " --version Show version information and exit"
echo " update Update to the latest version and exit"
exit 0
;;
--version)
echo "aws-pipeline-cli version $VERSION"
exit 0
;;
update)
self_update
exit 0
;;
[0-9]*)
COLUMNS="$1"
shift
;;
*)
# If not a recognized flag, treat as profile if not already set
if [[ -z "$PROFILE" ]]; then
PROFILE="$1"
fi
shift
;;
esac
done
# Validate COLUMNS
if ! [[ "$COLUMNS" =~ ^[0-9]+$ ]] || [[ "$COLUMNS" -le 0 ]]; then
COLUMNS=2
fi
check_for_update
# Fetch list of CodePipelines
fetch_pipelines() {
local output
if [[ -n "$PROFILE" ]]; then
output=$(aws codepipeline list-pipelines --profile "$PROFILE" --output text --query 'pipelines[*].name' 2>&1)
else
output=$(aws codepipeline list-pipelines --output text --query 'pipelines[*].name' 2>&1)
fi
if echo "$output" | grep -q "You must specify a region"; then
echo "Error: AWS region not set. Please run 'aws configure' or set the AWS_REGION environment variable."
exit 1
fi
if echo "$output" | grep -q "Unable to locate credentials"; then
echo "Error: AWS credentials not found. Please run 'aws configure' or set the appropriate environment variables."
exit 1
fi
if echo "$output" | grep -q "error"; then
echo "AWS CLI error:"
echo "$output"
exit 1
fi
echo "$output" | tr '\t' '\n'
}
# Options (will be dynamically set)
options=()
while IFS= read -r line; do
options+=("$line")
done < <(fetch_pipelines)
selected=0
# Disable echo and enable raw mode for arrow key reading
enable_raw_mode() {
stty -echo -icanon time 0 min 0
}
disable_raw_mode() {
stty sane
}
# Display menu
draw_menu() {
clear
echo "AWS CodePipelines (profile: $PROFILE)"
echo "Use ↑ ↓ to navigate, press Enter to select, q to quit:"
for i in "${!options[@]}"; do
if [[ $i -eq $selected ]]; then
# Highlight selected option in green
echo -e "> \033[32m${options[$i]}\033[0m"
else
echo " ${options[$i]}"
fi
done
}
get_user_input() {
while true; do
draw_menu
read -rsn1 key
if [[ $key == $'q' ]]; then
selected=-1
break
fi
if [[ $key == $'\x1b' ]]; then
read -rsn2 key2
case "$key2" in
"[A") # Up
((selected--))
[[ $selected -lt 0 ]] && selected=$((${#options[@]} - 1))
;;
"[B") # Down
((selected++))
[[ $selected -ge ${#options[@]} ]] && selected=0
;;
esac
elif [[ $key == "" ]]; then
break
fi
done
}
# Main loop
while true; do
# Fetch pipelines and populate options
options=()
while IFS= read -r line; do
options+=("$line")
done < <(fetch_pipelines)
if [[ ${#options[@]} -eq 0 ]]; then
echo "No pipelines found for profile '$PROFILE'."
exit 1
fi
selected=0
search=""
get_user_input_with_search() {
local key
while true; do
# Filter options based on search
filtered_options=()
for opt in "${options[@]}"; do
if [[ -z "$search" || "$opt" == *$search* ]]; then
filtered_options+=("$opt")
fi
done
# Adjust selected if out of bounds
[[ $selected -ge ${#filtered_options[@]} ]] && selected=0
[[ $selected -lt 0 ]] && selected=0
# Draw menu with search
clear
echo "|---------------------------------------------------------------------|"
echo "| AWS Pipeline CLI |"
echo "|---------------------------------------------------------------------|"
if [[ -n "$PROFILE" ]]; then
echo "AWS CodePipelines (profile: $PROFILE)"
else
echo "AWS CodePipelines (default profile)"
fi
echo "Type to search: $search"
echo "Use ↑ ↓ ← → to navigate, Enter to select, :q to quit, Backspace to delete."
# Print grid with aligned columns
local total=${#filtered_options[@]}
local rows=$(( (total + COLUMNS - 1) / COLUMNS ))
# Calculate max width for each column
local -a col_widths
for ((col=0; col<COLUMNS; col++)); do
col_widths[$col]=0
for ((row=0; row<rows; row++)); do
idx=$((row + col * rows))
if [[ $idx -lt $total ]]; then
len=${#filtered_options[$idx]}
(( len > col_widths[$col] )) && col_widths[$col]=$len
fi
done
done
for ((row=0; row<rows; row++)); do
line=""
for ((col=0; col<COLUMNS; col++)); do
idx=$((row + col * rows))
if [[ $idx -lt $total ]]; then
name="${filtered_options[$idx]}"
padlen=$((col_widths[$col] - ${#name}))
pad=""
for ((p=0; p<padlen; p++)); do pad+=" "; done
if [[ $idx -eq $selected ]]; then
line+=" > \033[32m${name}${pad}\033[0m "
else
line+=" ${name}${pad} "
fi
fi
done
echo -e "$line"
done
read -rsn1 key
# Check for :q typed in search
if [[ "$search" == ":q" ]]; then
selected=-1
break
fi
if [[ $key == $'q' ]]; then
selected=-1
break
elif [[ $key == $'\x1b' ]]; then
read -rsn2 key2
case "$key2" in
"[A") # Up
# Move up a row
rowsz=$(( (total + COLUMNS - 1) / COLUMNS ))
if (( selected - 1 >= 0 )); then
selected=$(( selected - 1 ))
fi
;;
"[B") # Down
# Move down a row
rowsz=$(( (total + COLUMNS - 1) / COLUMNS ))
if (( selected + 1 < total )); then
selected=$(( selected + 1 ))
fi
;;
"[C") # Right
# Move right a column
rowsz=$(( (total + COLUMNS - 1) / COLUMNS ))
if (( selected + rowsz < total )); then
selected=$(( selected + rowsz ))
fi
;;
"[D") # Left
# Move left a column
rowsz=$(( (total + COLUMNS - 1) / COLUMNS ))
if (( selected - rowsz >= 0 )); then
selected=$(( selected - rowsz ))
fi
;;
esac
elif [[ $key == "" ]]; then
break
elif [[ $key == $'\x7f' ]]; then # Backspace
search="${search%?}"
selected=0
else
search+="$key"
selected=0
fi
done
}
enable_raw_mode
get_user_input_with_search
disable_raw_mode
# Use filtered_options and selected
if [[ $selected -lt 0 ]]; then
echo "Exiting..."
break
elif [[ ${#filtered_options[@]} -eq 0 ]]; then
echo "No pipelines match your search."
echo -e "\nPress any key to return to menu..."
read -rsn1
continue
else
PIPELINE_NAME="${filtered_options[$selected]}"
while true; do
read -p "Are you sure you want to run $PIPELINE_NAME? (y/N): " confirm
case "$confirm" in
[yY])
echo "Releasing pipeline: $PIPELINE_NAME..."
# Start pipeline execution
if [[ -n "$PROFILE" ]]; then
output=$(aws codepipeline start-pipeline-execution --name "$PIPELINE_NAME" --profile "$PROFILE" 2>&1)
else
output=$(aws codepipeline start-pipeline-execution --name "$PIPELINE_NAME" 2>&1)
fi
status=$?
if [[ $status -eq 0 ]]; then
execution_id=$(echo "$output" | grep 'pipelineExecutionId' | awk -F'\"' '{print $4}')
echo "Started execution. ID: $execution_id"
else
echo "Failed to start pipeline execution. AWS CLI output:"
echo "$output"
fi
echo -e "\nPress any key to return to menu..."
read -rsn1
break
;;
[nN]|"")
# Go back to menu
break
;;
*)
echo "Please answer y or n."
;;
esac
done
fi
done