refactor: extract resolve_joinable_party from join and knock
cmd_join and cmd_knock both resolved an optional name argument to a live party via find_party_by_name/pick_live_party with identical error handling. Share it in one helper.
This commit is contained in:
parent
c789f4985b
commit
9544f605a6
1 changed files with 22 additions and 23 deletions
45
party
45
party
|
|
@ -635,6 +635,26 @@ pick_live_party() {
|
||||||
eval "roster_read \"\$$pick\""
|
eval "roster_read \"\$$pick\""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Resolve the party a join or knock targets: the named party if given,
|
||||||
|
# else the single live party (or a numbered prompt via pick_live_party).
|
||||||
|
# Sets RR_* on success; exits 1 with the appropriate message otherwise.
|
||||||
|
resolve_joinable_party() {
|
||||||
|
_rj_name="$1"
|
||||||
|
if [ -n "$_rj_name" ]; then
|
||||||
|
# Capture rc explicitly: under set -e a bare `find_party_by_name`
|
||||||
|
# returning 1 or 2 would exit before the case ran, swallowing the
|
||||||
|
# error message.
|
||||||
|
find_party_by_name "$_rj_name" && rc=0 || rc=$?
|
||||||
|
case $rc in
|
||||||
|
0) ;;
|
||||||
|
1) echo "party: '$_rj_name' not found." >&2; exit 1 ;;
|
||||||
|
2) exit 1 ;; # find_party_by_name already printed the ambiguity hint
|
||||||
|
esac
|
||||||
|
else
|
||||||
|
pick_live_party || exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Subcommand dispatch
|
# Subcommand dispatch
|
||||||
# ===================
|
# ===================
|
||||||
|
|
||||||
|
|
@ -1189,19 +1209,7 @@ EOF
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "$name" ]; then
|
resolve_joinable_party "$name"
|
||||||
# Capture rc explicitly: under set -e a bare `find_party_by_name`
|
|
||||||
# returning 1 or 2 would exit before the case ran, swallowing the
|
|
||||||
# error message.
|
|
||||||
find_party_by_name "$name" && rc=0 || rc=$?
|
|
||||||
case $rc in
|
|
||||||
0) ;;
|
|
||||||
1) echo "party: '$name' not found." >&2; exit 1 ;;
|
|
||||||
2) exit 1 ;; # find_party_by_name already printed the ambiguity hint
|
|
||||||
esac
|
|
||||||
else
|
|
||||||
pick_live_party || exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Group gate. Check membership against the party's *recorded* group
|
# Group gate. Check membership against the party's *recorded* group
|
||||||
# (RR_GROUP), the host may have hosted under --group <other>. The
|
# (RR_GROUP), the host may have hosted under --group <other>. The
|
||||||
|
|
@ -1355,16 +1363,7 @@ EOF
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -n "$name" ]; then
|
resolve_joinable_party "$name"
|
||||||
find_party_by_name "$name" && rc=0 || rc=$?
|
|
||||||
case $rc in
|
|
||||||
0) ;;
|
|
||||||
1) echo "party: '$name' not found." >&2; exit 1 ;;
|
|
||||||
2) exit 1 ;; # find_party_by_name printed the ambiguity hint
|
|
||||||
esac
|
|
||||||
else
|
|
||||||
pick_live_party || exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$RR_HOST_USER" = "$USER" ]; then
|
if [ "$RR_HOST_USER" = "$USER" ]; then
|
||||||
echo "party knock: '$RR_PARTY_NAME' is your own party." >&2
|
echo "party knock: '$RR_PARTY_NAME' is your own party." >&2
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue