Skip to content

Commit 0462612

Browse files
committed
Administrative toolkit functions that don't need jq work without jq
1 parent 0ffcc3b commit 0462612

File tree

3 files changed

+69
-25
lines changed

3 files changed

+69
-25
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ Database engine used to store user profiles, sessions, and event logs
452452

453453
- [Area and shape distortions in open-source discrete global grid systems](https://www.tandfonline.com/doi/pdf/10.1080/20964471.2022.2094926) [pdf]
454454
- [H3 as a gridding system](https://www.linkedin.com/pulse/can-h3-substitute-gridding-datasutram)
455+
- [Bit format of an H3 hexagon/pentagon index](https://xxxuduo.github.io/2020/05/23/Uber-H3-Index.html)
456+
- [H3 grid visualization](https://observablehq.com/@fil/h3-oddities)
455457

456458
### Technical articles
457459

functions/get-location/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ exports.handler = async function(event, context) {
7878
ST_DWithin(geo, ${pg.types.point({lat, lon})}, ${r})
7979
`
8080

81-
for (let i = 0; i < locations.length; i++ )
81+
for (let i = 0; i < locations.length; i++)
8282
normalizeLocation(locations[i])
8383

8484
let authorIds = []
8585

8686
// Create a list of unique author ids from the set of locations
87-
for (let i = 0; i < locations.length; i++ ) {
87+
for (let i = 0; i < locations.length; i++) {
8888
let author = locations[i].created_by
8989

9090
if (authorIds.indexOf(author) === -1)

manage

+65-23
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ else
1111
grep="grep"
1212
fi
1313

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
1722
echo "Install $1: $2"
1823
exit 1
1924
fi
2025
}
2126

27+
jq_required() {
28+
required_command jq https://stedolan.github.io/jq/
29+
}
30+
2231
# Return samconfig configuration parameter
2332
get_config() {
2433
if [ -f samconfig.toml ]; then
@@ -71,7 +80,7 @@ if [ $nargs -eq 0 ]; then
7180
echo -e "\e[33m$(echo "$i" | awk -F '# args:' '{print $2}')\e[0m"
7281
done
7382

74-
if ! command -v jq &>/dev/null; then
83+
if ! command_exists jq; then
7584
echo
7685
echo "jq not found, please install it: https://stedolan.github.io/jq/download/"
7786
fi
@@ -103,16 +112,21 @@ build_env_image_base_name () {
103112
echo "lambda-$1-build-environment"
104113
}
105114

115+
# Format and colorize JSON output with jq (if it's available)
116+
format_json=$(command_exists jq && echo jq || echo cat)
117+
106118
case $1 in
107119
"list-functions" )
108-
$aws lambda list-functions --output json | jq
120+
$aws lambda list-functions --output json | $format_json
109121
;;
110122

111123
"list-apis" )
112-
$aws apigatewayv2 get-apis --output json | jq
124+
$aws apigatewayv2 get-apis --output json | $format_json
113125
;;
114126

115127
"list-api-ids" )
128+
jq_required
129+
116130
$0 list-apis | jq -r ".Items[].ApiId"
117131
;;
118132

@@ -122,6 +136,8 @@ case $1 in
122136
;;
123137

124138
"list-api-stage-names" ) # args: [api-id]
139+
jq_required
140+
125141
api=${2:-$($0 list-api-ids | head -1)}
126142
$0 list-api-stages "$api" | jq -r '.Items[] | .StageName'
127143
;;
@@ -142,14 +158,18 @@ case $1 in
142158
;;
143159

144160
"list-rds-dbs" )
145-
$aws rds describe-db-instances --output json | jq
161+
$aws rds describe-db-instances --output json | $format_json
146162
;;
147163

148164
"list-rds-db-ids" )
165+
jq_required
166+
149167
$0 list-rds-dbs | jq -r "try(.DBInstances[].DBInstanceIdentifier)"
150168
;;
151169

152170
"list-rds-db-endpoints" )
171+
jq_required
172+
153173
$0 list-rds-dbs | jq -r "try(.DBInstances[].Endpoint)"
154174
;;
155175

@@ -164,19 +184,26 @@ case $1 in
164184
;;
165185

166186
"list-stacks" )
187+
jq_required
188+
167189
$aws cloudformation describe-stacks --output json | jq -r '.Stacks[].StackName'
168190
;;
169191

170192
"list-stack-params" ) # args: [stack-name]
193+
jq_required
194+
171195
$aws cloudformation describe-stacks --stack-name "${2:-$(get_config stack_name)}" | jq -r 'try(.Stacks[0].Parameters[]) | "\(.ParameterKey)=\(.ParameterValue)"'
172196
;;
173197

174198
"list-stack-outputs" ) # args: [stack-name]
199+
jq_required
200+
175201
$aws cloudformation describe-stacks --stack-name "${2:-$(get_config stack_name)}" | jq -r 'try(.Stacks[0].Outputs)'
176202
;;
177203

178204
"function-run" ) # args: func-name [...params]
179205
args 1
206+
jq_required
180207

181208
name=$2
182209
shift 2
@@ -204,6 +231,7 @@ case $1 in
204231

205232
"function-logs" ) # args: func-name
206233
args 1
234+
jq_required
207235

208236
logstreams=$($aws logs describe-log-streams --log-group-name "/aws/lambda/$2" --query 'logStreams[*].logStreamName' | jq -r '.[-1]')
209237

@@ -218,7 +246,8 @@ case $1 in
218246
;;
219247

220248
"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
222251

223252
set -e
224253

@@ -360,7 +389,8 @@ case $1 in
360389
;;
361390

362391
"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
364394

365395
image_base_name=$(build_env_image_base_name cpp)
366396

@@ -415,7 +445,7 @@ case $1 in
415445
"build-cpp-function" ) # args: func-name [arch] [build = debug|release]
416446
args 1
417447

418-
check_installed docker https://docs.docker.com/get-docker/
448+
required_command docker https://docs.docker.com/get-docker/
419449

420450
set -e
421451

@@ -510,6 +540,8 @@ case $1 in
510540
;;
511541

512542
"rds-db-status" ) # args: [instance-id]
543+
jq_required
544+
513545
if [ -n "$2" ]; then
514546
id="--db-instance-identifier $2"
515547
fi
@@ -520,6 +552,8 @@ case $1 in
520552
;;
521553

522554
"rds-db-hibernate" ) # args: [instance-id]
555+
jq_required
556+
523557
set -eo pipefail
524558

525559
status="$($0 rds-db-action stop-db-instance "$2" | jq -r '.DBInstance.DBInstanceStatus')"
@@ -536,6 +570,8 @@ case $1 in
536570
;;
537571

538572
"rds-db-wake" ) # args: [instance-id]
573+
jq_required
574+
539575
set -eo pipefail
540576

541577
status="$($0 rds-db-action start-db-instance "$2" | jq -r '.DBInstance.DBInstanceStatus')"
@@ -575,14 +611,15 @@ case $1 in
575611

576612
echo "Invoking query function with parameters:"
577613
echo
578-
echo "$json" | jq
614+
echo "$json" | $format_json
579615
echo
580616

581617
$0 function-run db-run --payload "$json"
582618
;;
583619

584620
"rds-db-run-file" ) # args: query-file
585621
args 1
622+
jq_required
586623

587624
echo "Invoking query function with parameters:"
588625
echo
@@ -593,7 +630,8 @@ case $1 in
593630
;;
594631

595632
"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
597635

598636
# Database name (defined as a stack parameter)
599637
dbname=$(get_stack_param DBName)
@@ -628,6 +666,8 @@ case $1 in
628666
;;
629667

630668
"rds-db-get-security-group" )
669+
jq_required
670+
631671
$aws rds describe-db-instances | jq -r ".DBInstances[].VpcSecurityGroups[].VpcSecurityGroupId"
632672
;;
633673

@@ -648,13 +688,13 @@ case $1 in
648688
"dynamo-table-info" ) # args: table = sessions|users|event-log
649689
args 1
650690

651-
$aws dynamodb describe-table --table-name "$2" | jq
691+
$aws dynamodb describe-table --table-name "$2" | $format_json
652692
;;
653693

654694
"s3-list-object-tags" ) # args: bucket-name object-key
655695
args 2
656696

657-
$aws s3api get-object-tagging --bucket "$2" --key "$3" | jq
697+
$aws s3api get-object-tagging --bucket "$2" --key "$3" | $format_json
658698
;;
659699

660700
"s3-delete-all" ) # args: bucket-name
@@ -664,6 +704,8 @@ case $1 in
664704
;;
665705

666706
"event-log" ) # args: [utc-date]
707+
jq_required
708+
667709
# Convert today or given UTC date to epoch time
668710
date=$(date --date="${2:-$(date --utc +"%Y-%m-%d")}" --utc +%s)
669711

@@ -710,26 +752,26 @@ case $1 in
710752
;;
711753

712754
"check-service-quotas" ) # args: [service]
713-
service=$2
755+
if [ -z "$2" ]; then
756+
jq_required
714757

715-
if [ -z "$service" ]; then
716-
# List services with service quotas
758+
# List services for which service quota information can be queried
717759
$aws service-quotas list-services | jq -r ".Services[].ServiceCode"
718760
else
719-
$aws service-quotas list-service-quotas --service-code "$service" | jq
761+
$aws service-quotas list-service-quotas --service-code "$2" | $format_json
720762
fi
721763
;;
722764

723765
"whoami" )
724-
$aws iam get-user | jq
766+
$aws iam get-user | $format_json
725767
;;
726768

727769
"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
730772

731773
# Lint js code
732-
find {functions,lib}/ -name '*.js' -print0 | xargs -0 jshint
774+
find {functions,layers}/ -name '*.js' -print0 | xargs -0 jshint
733775

734776
# Lint bash code
735777
shellcheck "$0"
@@ -742,7 +784,7 @@ case $1 in
742784
;;
743785

744786
"frontend-prepare" ) # args: [git-ref]
745-
check_installed pnpm https://pnpm.io
787+
required_command pnpm https://pnpm.io
746788

747789
if [ -d publish ]; then
748790
echo "It looks like you have previously used this command. Please delete the publish/ directory and re-run this."

0 commit comments

Comments
 (0)