Skip to content

Instantly share code, notes, and snippets.

@geoffeg
Last active July 10, 2019 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffeg/566d43ecaae6222fefadd0a440635947 to your computer and use it in GitHub Desktop.
Save geoffeg/566d43ecaae6222fefadd0a440635947 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
declare -r TMP_TOKEN_FILE="$HOME/.op_tmux_token_tmp"
declare -r OPT_SUBDOMAIN="my"
declare -r OPT_VAULT="personal"
op_login() {
op signin "$OPT_SUBDOMAIN" --output=raw > "$TMP_TOKEN_FILE"
clear
}
op_get_session() {
cat "$TMP_TOKEN_FILE" 2> /dev/null
}
get_op_items() {
# The structure (we need) looks like the following:
# [
# {
# "uuid": "some-long-uuid",
# "overview": {
# "URLs": [
# { "u": "sudolikeaboss://local" }
# ],
# "title": "Some item"
# }
# }
# ]
local -r JQ_FILTER="
map([ .overview.title, .uuid ]
| join(\",\"))
| .[]
"
op list items --vault="$OPT_VAULT" --session="$(op_get_session)" 2> /dev/null \
| jq "$JQ_FILTER" --raw-output
}
get_op_item_password() {
local -r ITEM_UUID="$1"
# There are two different kind of items that
# we support: login items and passwords.
#
# * Login items:
# {
# "details": {
# "fields": [
# {
# "designation": "password",
# "value": "supersecret"
# }
# ]
# }
# }
#
# * Password:
# {
# "details": {
# "password": "supersecret"
# }
# }
local -r JQ_FILTER="
.details
| if .password and .password != \"\" then
.password
else
.fields[]
| select (.designation == \"password\")
| .value
end
"
op get item "$ITEM_UUID" --session="$(op_get_session)" \
| jq "$JQ_FILTER" --raw-output
}
# ------------------------------------------------------------------------------
main() {
local items
local selected_item_name
local selected_item_uuid
local selected_item_password
items="$(get_op_items)"
if [[ -z "$items" ]]; then
# Needs to login
op_login
if [[ -z "$(op_get_session)" ]]; then
echo "1Password CLI signin has failed"
return 0
fi
items="$(get_op_items)"
fi
selected_item_name="$(echo "$items" | awk -F ',' '{ print $1 }' | fzf --no-multi)"
if [[ -n "$selected_item_name" ]]; then
selected_item_uuid="$(echo "$items" | egrep "^$selected_item_name," | awk -F ',' '{ print $2 }')"
selected_item_password="$(get_op_item_password "$selected_item_uuid")"
echo "$selected_item_password"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment