bwzy/bwz

51 lines
1.9 KiB
Text
Raw Normal View History

2024-12-25 22:10:12 -07:00
#!/bin/bash
# Trap EXIT signal to clean up
cleanup() {
echo "Cleaning up temporary directories..."
rm -rf "$TMP_DIR"
}
#trap cleanup EXIT
# Create temporary directories in tmpfs
# Static filename is used to allow for re-use between invocations
2024-12-25 22:10:12 -07:00
TMP_DIR="/dev/shm/bwz-cache"
[[ ! -d $TMP_DIR ]] && mkdir "$TMP_DIR"
[[ ! -d $TMP_DIR ]] && echo "failed to create tmp dir" && exit 1
# TODO: also check for size of file in case it's empty?
2024-12-25 22:10:12 -07:00
if [[ ! -f "${TMP_DIR}/items" ]] || [[ ! -f "${TMP_DIR}/items" ]]; then
BW_SESSION=$(bw unlock --raw)
export BW_SESSION
echo "$BW_SESSION" > "${TMP_DIR}/session"
gum spin --title 'fetching items ...' -s dot bw list items > "${TMP_DIR}/items"
gum spin --title 'fetching folder list ...' -s dot bw list folders > "${TMP_DIR}/folders"
fi
BW_SESSION=$(<${TMP_DIR}/session)
export BW_SESSION
items="${TMP_DIR}/items"
folders="${TMP_DIR}/folders"
TAB=" "
folder_sed=$(jq -r '.[] | [ "s@" , .id , "@" , .name , "@;" ] | join("")' < "$folders")
# define the logic here for use in fzf call
jq_select_id="jq -r '.[] | select(.id == \"{1}\")"
copy_user="$jq_select_id | .login.username' <$items | cli-copy"
copy_pass="$jq_select_id | .login.password' <$items | cli-copy"
copy_totp="$jq_select_id | .login.totp' <$items | sed 's/.*secret=//; s/&.*//' | oathtool -b --totp - | cli-copy)"
preview_item="$jq_select_id' < $items | json2yaml | bat --color=always -p -l yaml"
2024-12-25 22:24:18 -07:00
# TODO: make the jq below better. define in variable
2024-12-25 22:10:12 -07:00
jq -r '.[] | [ .id, "{{ Color \"10\" \"0\" \""+ .name +"\" }}", .folderId ] | join("'"$TAB"'")' <"$items" \
| sed -e "$folder_sed" \
| gum format -t template \
| fzf --ansi -i --delimiter "$TAB" --with-nth 2,3 \
2024-12-25 22:24:18 -07:00
--bind "alt-u:execute($copy_user)+change-preview-label(user copied)" \
--bind "alt-p:execute($copy_pass)+change-preview-label(pass copied)" \
--bind "alt-t:execute($copy_totp)+change-preview-label(totp copied)" \
--preview "echo \"{1}\"; $preview_item"
2024-12-25 22:10:12 -07:00