Split mkdeck() into two helper functions

This commit is contained in:
Jaidyn Ann 2024-04-25 16:31:37 -05:00
parent 27611131ea
commit 4acf8c2355

View File

@ -19,33 +19,41 @@ shell_sanitize() {
suit_cards() { suit_cards() {
local name="$(shell_sanitize "$1")" local name="$(shell_sanitize "$1")"
local suits="$(eval "echo \$${name}_SUITS")" local suits=n"$(eval "echo \$${name}_SUITS")"
local suit_cards="$(eval "echo \$${name}_SUIT_CARDS")" local suit_cards="$(eval "echo \$${name}_SUIT_CARDS")"
for suit in $suits; do for suit in $suits; do
for suit_card in $suit_cards; do for suit_card in $suit_cards; do
printf "${suit}${suit_card} " printf "${suit}${suit_card} "
done done
done done
eval "echo \$${name}_ADDITIONAL_CARDS" eval "echo \$${name}_ADDITIONAL_CARDS"
} }
mkdeck() { mkdeck_dir() {
local dir="$1" local dir="$1"
local deck_name="$2"
local cards="$(suit_cards "$deck_name")"
if test -z "$cards"; then
echo "No cards found for deck $deck_name. Check your environment variables:"
echo "${deck_name}_SUITS, ${deck_name}_SUIT_CARDS, and ${deck_name}_ADDITIONAL_CARDS."
fi
if test ! -d "$dir"; then if test ! -d "$dir"; then
mkdir "$dir" mkdir "$dir"
fi fi
if test ! -d "$dir"; then if test ! -d "$dir"; then
echo "Failed to create deck directory at $dir." 1>&2 echo "Failed to create deck directory at $dir."
return 2 exit 2
fi
}
mkdeck_cards() {
local dir="$1"
local deck_name="$2"
local cards="$(suit_cards "$deck_name")"
if test -z "$cards"; then
1>&2 echo "No cards found for deck $deck_name. Check your environment variables:"
1>&2 echo "${deck_name}_SUITS, ${deck_name}_SUIT_CARDS, and ${deck_name}_ADDITIONAL_CARDS."
exit 3
fi fi
for card in $cards; do for card in $cards; do
@ -54,4 +62,12 @@ mkdeck() {
} }
mkdeck() {
local dir="$1"
local deck_name="$2"
mkdeck_dir "$dir"
mkdeck_cards "$dir" "$deck_name"
}
mkdeck "$1" "FRENCH" mkdeck "$1" "FRENCH"