Skip to content

Commit 18ac1cf

Browse files
committed
fn: add foreach-pair for name/region cli type ops
1 parent 4832b76 commit 18ac1cf

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

scripts/bash-fns.sh

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# shellcheck disable=SC2148
22
#
3-
# Various bash helper functions live here.
4-
3+
# Various bash helper fns which aren't used enough to move to just recipes.
54

65
# This can be used to simplify ssh sessions, rsync, ex:
76
# ssh -o "$(ssm-proxy-cmd "$REGION")" "$INSTANCE_ID"
@@ -34,3 +33,44 @@ submit() (
3433
echo "Transaction $TX_SIGNED with txid $TXID submitted successfully."
3534
echo
3635
)
36+
37+
foreach-pair() (
38+
set -euo pipefail
39+
40+
[ -n "${DEBUG:-}" ] && set -x
41+
42+
if [ "$#" -ne 3 ]; then
43+
echo "Usage:"
44+
echo " foreach-pair \$STRING_LIST_1 \$STRING_LIST_2 \$STRING_CMD"
45+
echo
46+
echo "Where:"
47+
echo " \$STRING_CMD has \$i and \$j embedded as iters from the two lists"
48+
echo
49+
echo "Example:"
50+
echo " foreach-pair \"\$(just ssh-list name "preview1.*")\" \"\$(just ssh-list region "preview1.*")\" 'just aws-ec2-status \$i \$j'"
51+
exit 1
52+
fi
53+
54+
local l1="$1"
55+
local l2="$2"
56+
local cmd="$3"
57+
58+
read -r -a l1 <<< "$l1"
59+
read -r -a l2 <<< "$l2"
60+
61+
if [[ ${#l1[@]} -ne ${#l2[@]} ]]; then
62+
echo "Error: Lists are not the same length." >&2
63+
exit 1
64+
fi
65+
66+
local length=${#l1[@]}
67+
68+
for ((n = 0; n < length; n++)); do
69+
# shellcheck disable=SC2034
70+
local i="${l1[n]}"
71+
# shellcheck disable=SC2034
72+
local j="${l2[n]}"
73+
74+
eval "$cmd" || true
75+
done
76+
)

0 commit comments

Comments
 (0)