aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/bokftui-bw60
1 files changed, 38 insertions, 22 deletions
diff --git a/scripts/bokftui-bw b/scripts/bokftui-bw
index 1fdc4fe..7742909 100755
--- a/scripts/bokftui-bw
+++ b/scripts/bokftui-bw
@@ -2,12 +2,14 @@
# Start bokftui with a credential fetched from a Bitwarden/Vaultwarden item.
# Runs entirely as the invoking user — no root, no sudo.
#
-# bokftui-bw [item]
+# bokftui-bw [item-or-username]
#
-# The item is chosen from BOKF_BW_ITEM, the first argument, or — when several
-# items match "bokf" — an interactive picker. Per item:
+# The item is chosen from BOKF_BW_ITEM, the first argument (matched against
+# the item name or the login user name), or — when several items match —
+# an interactive picker. Candidates without an argument match
+# BOKF_BW_MATCH (default "bokf") the same way.
#
-# BOKF_BW_FIELD custom field that holds the secret (default: the password)
+# BOKF_BW_FIELD custom field that holds the secret (default: login.password)
# BOKF_BW_KIND password (default) exports BOKFD_PASSWORD;
# token exports BOKFD_TOKEN
#
@@ -25,40 +27,39 @@ field="${BOKF_BW_FIELD:-}"
kind="${BOKF_BW_KIND:-password}"
BOKFTUI="${BOKFTUI:-bokftui}"
+# $1: newline-separated "id<TAB>label" rows; prints the chosen id.
choose_item() {
- # $1: newline-separated item names
- names="$1"
- n=$(printf '%s\n' "$names" | grep -c .)
+ rows="$1"
+ n=$(printf '%s\n' "$rows" | grep -c . || true)
if [ "$n" -eq 0 ]; then
echo "bokftui-bw: no matching item" >&2
exit 1
fi
if [ "$n" -eq 1 ]; then
- printf '%s\n' "$names"
+ printf '%s\n' "$rows" | cut -f1
return
fi
if [ ! -t 0 ]; then
echo "bokftui-bw: several items match; pass one as an argument" >&2
- printf '%s\n' "$names" >&2
+ printf '%s\n' "$rows" | cut -f2- >&2
exit 1
fi
echo "Välj konto:" >&2
- i=1
- printf '%s\n' "$names" | while IFS= read -r name; do
- printf ' %d) %s\n' "$i" "$name" >&2
- i=$((i + 1))
- done
+ printf '%s\n' "$rows" | awk -F'\t' '{printf " %d) %s\n", NR, $2}' >&2
printf 'Nummer: ' >&2
read -r ans
- printf '%s\n' "$names" | sed -n "${ans}p"
+ printf '%s\n' "$rows" | awk -F'\t' -v n="$ans" 'NR == n {print $1}'
}
if command -v rbw >/dev/null 2>&1; then
if ! rbw unlocked >/dev/null 2>&1; then
rbw unlock
fi
+ rbw sync >/dev/null 2>&1 || true
if [ -z "$item" ]; then
- item=$(choose_item "$(rbw list 2>/dev/null | grep -i 'bokf' || true)")
+ item=$(choose_item "$(rbw list 2>/dev/null |
+ grep -i "${BOKF_BW_MATCH:-bokf}" |
+ awk '{printf "%s\t%s\n", $0, $0}' || true)")
fi
if [ -n "$field" ]; then
secret=$(rbw get --field "$field" "$item")
@@ -79,15 +80,30 @@ elif command -v bw >/dev/null 2>&1; then
export BW_SESSION
printf '%s\n' "$BW_SESSION" > "$cache"
fi
+ bw sync >/dev/null 2>&1 || true
+ items=$(bw list items 2>/dev/null || echo '[]')
if [ -z "$item" ]; then
- item=$(choose_item "$(bw list items --search bokf 2>/dev/null |
- jq -r '.[]? | .name' || true)")
+ rows=$(printf '%s' "$items" | jq -r --arg m "${BOKF_BW_MATCH:-bokf}" '
+ .[] | select((.name | test($m; "i")) or
+ ((.login.username // "") | test($m; "i")))
+ | "\(.id)\t\(.name) (\(.login.username // "-"))"')
+ else
+ rows=$(printf '%s' "$items" | jq -r --arg q "$item" '
+ ([.[] | select(.id == $q or .name == $q)] as $exact
+ | if ($exact | length) > 0 then $exact
+ else [.[] | select((.name | test($q; "i")) or
+ ((.login.username // "") | test($q; "i")))]
+ end)
+ | .[] | "\(.id)\t\(.name) (\(.login.username // "-"))"')
fi
+ id=$(choose_item "$rows")
if [ -n "$field" ]; then
- secret=$(bw get item "$item" | jq -r --arg f "$field" \
- '.fields[]? | select(.name == $f) | .value' | head -n 1)
+ secret=$(printf '%s' "$items" | jq -r --arg id "$id" --arg f "$field" \
+ '.[] | select(.id == $id) | .fields[]? |
+ select(.name == $f) | .value' | head -n 1)
else
- secret=$(bw get password "$item")
+ secret=$(printf '%s' "$items" | jq -r --arg id "$id" \
+ '.[] | select(.id == $id) | .login.password')
fi
else
echo "bokftui-bw: install rbw or the Bitwarden CLI (bw)" >&2
@@ -95,7 +111,7 @@ else
fi
if [ -z "$secret" ] || [ "$secret" = "null" ]; then
- echo "bokftui-bw: no secret named '$field' in item '$item'" >&2
+ echo "bokftui-bw: item has no secret to use" >&2
exit 1
fi