Open dcreemer opened 2 years ago
Thanks. Updating...
At the moment does not look like you can interactively enter the password, it always prompts for it so it might be a pain in the ass
In v2 you don't really need to cache anything anymore as that's built in and with 1Password v8 you can tie the auth to biometrics, so you can almost get away with just this:
list items:
/usr/local/bin/op --cache item list --format=json
and to retrieve an item:
/usr/local/bin/op --cache item get --fields password <itemID>
No caching, no GPG required. You do have to do some filtering on the item list to return just the ID. in JSON land it's .id not very hard :)
Here is a 1password v2 CLI shell script that uses POSIX SHELL.
For iterm integration:
in 1Password, tag everything 'iterm' if you want it to show up in the list. (or update the script below OP_TAG)
in iTerm coprocess keybinding: Run Coporcess
/usr/local/bin/1p choose
you must have a posix shell, jq and the choose-gui:
brew install jq choose-gui
should do the trick.
#!/bin/sh
#
# Copyright 2022 <zie@birl.org>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# DOCS:
# Use op command line tool
# SETUP:
# for iTERM setup a coprocess to run op & choose gui
#
# OP release notes: https://app-updates.agilebits.com/product_history/CLI2
#
if [ -z "$OP_TAG" ]; then
OP_TAG="iterm"
fi
OP_BIN="/usr/local/bin/op"
JQ_BIN="/usr/local/bin/jq"
# for macOS instead of fzf use: https://github.com/sdegutis/choose
CHOOSE_BIN="/usr/local/bin/choose"
CACHE_DIR="${HOME}/.config/1p"
if [ ! -f "$CHOOSE_BIN" ]; then
CHOOSE_BIN='fzf'
fi
usage() {
echo "usage: 1p <cmd>"
echo ' Use one password CLI to choose passwords for terminals'
echo ' specially designed for iterm2 and macOS by default'
echo
echo ' 1p list'
echo ' list all keys in 1P_TAG'
echo ' 1p choose'
echo ' getpw ITEM from 1P_TAG using FZF or other chooser.'
echo ' adds a special item named 'clear' which will clear'
echo ' the cache and let you choose again.'
echo ' 1p get <item name>'
echo ' getpw ITEM from 1P_TAG'
echo ' 1p clear'
echo ' clear the list cache'
echo
echo 'requires:'
echo ' op cli(with biometrics): https://developer.1password.com/docs/cli/'
echo ' jq: https://stedolan.github.io/jq/'
echo ' on MacOS:'
echo ' choose: https://github.com/sdegutis/choose'
echo ' otherwise:'
echo ' fzf: https://github.com/junegunn/fzf'
exit 1
}
list_raw() {
if [ -f "${CACHE_DIR}/list" ]; then
cat "${CACHE_DIR}/list"
else
$OP_BIN --cache item list --tags "${OP_TAG}" --format=json > "${CACHE_DIR}/list"
cat "${CACHE_DIR}/list"
fi
}
list() {
list_raw | $JQ_BIN --raw-output --sort-keys .[].title
}
clear() {
rm "${CACHE_DIR}/list"
}
get() {
# how to get the ID, but not really needed.
ITEM=$2
ID=$(list_raw | $JQ_BIN --raw-output ".[] | select(.title==\"${ITEM}\").id")
$OP_BIN --cache item get --fields password "${ID}"
}
choose() {
ITEMS=$(list)$'\n'clear
ITEM=$(echo "$ITEMS" | ${CHOOSE_BIN})
if [ "$ITEM" = "clear" ]; then
clear
ITEM=$(list | ${CHOOSE_BIN})
fi
ID=$(list_raw | $JQ_BIN --raw-output ".[] | select(.title==\"${ITEM}\").id")
$OP_BIN --cache item get --fields password "${ID}"
}
#######################################################################
case "$1" in
list) list;;
list_raw) list_raw;;
clear) clear;;
clean) clear;;
get) get "$@";;
choose) choose "$@";;
*) usage;;
esac
1password-cli v2 is not compatible with v1, and both will be supported for a while. I have upgraded
1pass
to warn about v2.0, but we still need to add code to support v2.