11
11
grep=" grep"
12
12
fi
13
13
14
- # Exit if a particular utility is not available
15
- check_installed () {
16
- if ! command -v " $1 " & > /dev/null; then
14
+ # Check if a command exists in the shell session
15
+ command_exists () {
16
+ command -v " $1 " & > /dev/null
17
+ }
18
+
19
+ # Exit if a particular command is not available
20
+ required_command () {
21
+ if ! command_exists " $1 " ; then
17
22
echo " Install $1 : $2 "
18
23
exit 1
19
24
fi
20
25
}
21
26
27
+ jq_required () {
28
+ required_command jq https://stedolan.github.io/jq/
29
+ }
30
+
22
31
# Return samconfig configuration parameter
23
32
get_config () {
24
33
if [ -f samconfig.toml ]; then
@@ -71,7 +80,7 @@ if [ $nargs -eq 0 ]; then
71
80
echo -e " \e[33m$( echo " $i " | awk -F ' # args:' ' {print $2}' ) \e[0m"
72
81
done
73
82
74
- if ! command -v jq & > /dev/null ; then
83
+ if ! command_exists jq ; then
75
84
echo
76
85
echo " jq not found, please install it: https://stedolan.github.io/jq/download/"
77
86
fi
@@ -103,16 +112,21 @@ build_env_image_base_name () {
103
112
echo " lambda-$1 -build-environment"
104
113
}
105
114
115
+ # Format and colorize JSON output with jq (if it's available)
116
+ format_json=$( command_exists jq && echo jq || echo cat)
117
+
106
118
case $1 in
107
119
" list-functions" )
108
- $aws lambda list-functions --output json | jq
120
+ $aws lambda list-functions --output json | $format_json
109
121
;;
110
122
111
123
" list-apis" )
112
- $aws apigatewayv2 get-apis --output json | jq
124
+ $aws apigatewayv2 get-apis --output json | $format_json
113
125
;;
114
126
115
127
" list-api-ids" )
128
+ jq_required
129
+
116
130
$0 list-apis | jq -r " .Items[].ApiId"
117
131
;;
118
132
@@ -122,6 +136,8 @@ case $1 in
122
136
;;
123
137
124
138
" list-api-stage-names" ) # args: [api-id]
139
+ jq_required
140
+
125
141
api=${2:- $($0 list-api-ids | head -1)}
126
142
$0 list-api-stages " $api " | jq -r ' .Items[] | .StageName'
127
143
;;
@@ -142,14 +158,18 @@ case $1 in
142
158
;;
143
159
144
160
" list-rds-dbs" )
145
- $aws rds describe-db-instances --output json | jq
161
+ $aws rds describe-db-instances --output json | $format_json
146
162
;;
147
163
148
164
" list-rds-db-ids" )
165
+ jq_required
166
+
149
167
$0 list-rds-dbs | jq -r " try(.DBInstances[].DBInstanceIdentifier)"
150
168
;;
151
169
152
170
" list-rds-db-endpoints" )
171
+ jq_required
172
+
153
173
$0 list-rds-dbs | jq -r " try(.DBInstances[].Endpoint)"
154
174
;;
155
175
@@ -164,19 +184,26 @@ case $1 in
164
184
;;
165
185
166
186
" list-stacks" )
187
+ jq_required
188
+
167
189
$aws cloudformation describe-stacks --output json | jq -r ' .Stacks[].StackName'
168
190
;;
169
191
170
192
" list-stack-params" ) # args: [stack-name]
193
+ jq_required
194
+
171
195
$aws cloudformation describe-stacks --stack-name " ${2:- $(get_config stack_name)} " | jq -r ' try(.Stacks[0].Parameters[]) | "\(.ParameterKey)=\(.ParameterValue)"'
172
196
;;
173
197
174
198
" list-stack-outputs" ) # args: [stack-name]
199
+ jq_required
200
+
175
201
$aws cloudformation describe-stacks --stack-name " ${2:- $(get_config stack_name)} " | jq -r ' try(.Stacks[0].Outputs)'
176
202
;;
177
203
178
204
" function-run" ) # args: func-name [...params]
179
205
args 1
206
+ jq_required
180
207
181
208
name=$2
182
209
shift 2
@@ -204,6 +231,7 @@ case $1 in
204
231
205
232
" function-logs" ) # args: func-name
206
233
args 1
234
+ jq_required
207
235
208
236
logstreams=$( $aws logs describe-log-streams --log-group-name " /aws/lambda/$2 " --query ' logStreams[*].logStreamName' | jq -r ' .[-1]' )
209
237
@@ -218,7 +246,8 @@ case $1 in
218
246
;;
219
247
220
248
" make-cpp-build-environment" ) # args: [arch = arm|x86]
221
- check_installed docker https://docs.docker.com/get-docker/
249
+ required_command docker https://docs.docker.com/get-docker/
250
+ jq_required
222
251
223
252
set -e
224
253
@@ -360,7 +389,8 @@ case $1 in
360
389
;;
361
390
362
391
" set-default-cpp-build-environment" ) # args: [arch]
363
- check_installed docker https://docs.docker.com/get-docker/
392
+ required_command docker https://docs.docker.com/get-docker/
393
+ jq_required
364
394
365
395
image_base_name=$( build_env_image_base_name cpp)
366
396
@@ -415,7 +445,7 @@ case $1 in
415
445
" build-cpp-function" ) # args: func-name [arch] [build = debug|release]
416
446
args 1
417
447
418
- check_installed docker https://docs.docker.com/get-docker/
448
+ required_command docker https://docs.docker.com/get-docker/
419
449
420
450
set -e
421
451
@@ -510,6 +540,8 @@ case $1 in
510
540
;;
511
541
512
542
" rds-db-status" ) # args: [instance-id]
543
+ jq_required
544
+
513
545
if [ -n " $2 " ]; then
514
546
id=" --db-instance-identifier $2 "
515
547
fi
@@ -520,6 +552,8 @@ case $1 in
520
552
;;
521
553
522
554
" rds-db-hibernate" ) # args: [instance-id]
555
+ jq_required
556
+
523
557
set -eo pipefail
524
558
525
559
status=" $( $0 rds-db-action stop-db-instance " $2 " | jq -r ' .DBInstance.DBInstanceStatus' ) "
@@ -536,6 +570,8 @@ case $1 in
536
570
;;
537
571
538
572
" rds-db-wake" ) # args: [instance-id]
573
+ jq_required
574
+
539
575
set -eo pipefail
540
576
541
577
status=" $( $0 rds-db-action start-db-instance " $2 " | jq -r ' .DBInstance.DBInstanceStatus' ) "
@@ -575,14 +611,15 @@ case $1 in
575
611
576
612
echo " Invoking query function with parameters:"
577
613
echo
578
- echo " $json " | jq
614
+ echo " $json " | $format_json
579
615
echo
580
616
581
617
$0 function-run db-run --payload " $json "
582
618
;;
583
619
584
620
" rds-db-run-file" ) # args: query-file
585
621
args 1
622
+ jq_required
586
623
587
624
echo " Invoking query function with parameters:"
588
625
echo
@@ -593,7 +630,8 @@ case $1 in
593
630
;;
594
631
595
632
" rds-db-connect" ) # args: [role = admin|reader|writer]
596
- check_installed psql https://www.postgresql.org/download/
633
+ required_command psql https://www.postgresql.org/download/
634
+ jq_required
597
635
598
636
# Database name (defined as a stack parameter)
599
637
dbname=$( get_stack_param DBName)
@@ -628,6 +666,8 @@ case $1 in
628
666
;;
629
667
630
668
" rds-db-get-security-group" )
669
+ jq_required
670
+
631
671
$aws rds describe-db-instances | jq -r " .DBInstances[].VpcSecurityGroups[].VpcSecurityGroupId"
632
672
;;
633
673
@@ -648,13 +688,13 @@ case $1 in
648
688
" dynamo-table-info" ) # args: table = sessions|users|event-log
649
689
args 1
650
690
651
- $aws dynamodb describe-table --table-name " $2 " | jq
691
+ $aws dynamodb describe-table --table-name " $2 " | $format_json
652
692
;;
653
693
654
694
" s3-list-object-tags" ) # args: bucket-name object-key
655
695
args 2
656
696
657
- $aws s3api get-object-tagging --bucket " $2 " --key " $3 " | jq
697
+ $aws s3api get-object-tagging --bucket " $2 " --key " $3 " | $format_json
658
698
;;
659
699
660
700
" s3-delete-all" ) # args: bucket-name
@@ -664,6 +704,8 @@ case $1 in
664
704
;;
665
705
666
706
" event-log" ) # args: [utc-date]
707
+ jq_required
708
+
667
709
# Convert today or given UTC date to epoch time
668
710
date=$( date --date=" ${2:- $(date --utc +" %Y-%m-%d" )} " --utc +%s)
669
711
@@ -710,26 +752,26 @@ case $1 in
710
752
;;
711
753
712
754
" check-service-quotas" ) # args: [service]
713
- service=$2
755
+ if [ -z " $2 " ]; then
756
+ jq_required
714
757
715
- if [ -z " $service " ]; then
716
- # List services with service quotas
758
+ # List services for which service quota information can be queried
717
759
$aws service-quotas list-services | jq -r " .Services[].ServiceCode"
718
760
else
719
- $aws service-quotas list-service-quotas --service-code " $service " | jq
761
+ $aws service-quotas list-service-quotas --service-code " $2 " | $format_json
720
762
fi
721
763
;;
722
764
723
765
" whoami" )
724
- $aws iam get-user | jq
766
+ $aws iam get-user | $format_json
725
767
;;
726
768
727
769
" lint" )
728
- check_installed jshint https://jshint.com/install/
729
- check_installed shellcheck https://github.com/koalaman/shellcheck
770
+ required_command jshint https://jshint.com/install/
771
+ required_command shellcheck https://github.com/koalaman/shellcheck
730
772
731
773
# Lint js code
732
- find {functions,lib }/ -name ' *.js' -print0 | xargs -0 jshint
774
+ find {functions,layers }/ -name ' *.js' -print0 | xargs -0 jshint
733
775
734
776
# Lint bash code
735
777
shellcheck " $0 "
@@ -742,7 +784,7 @@ case $1 in
742
784
;;
743
785
744
786
" frontend-prepare" ) # args: [git-ref]
745
- check_installed pnpm https://pnpm.io
787
+ required_command pnpm https://pnpm.io
746
788
747
789
if [ -d publish ]; then
748
790
echo " It looks like you have previously used this command. Please delete the publish/ directory and re-run this."
0 commit comments