Last active
October 19, 2018 22:25
-
-
Save bjuretko/8192229f215a9abe50e5d1285f9394ff to your computer and use it in GitHub Desktop.
Store files as documents on 1password CLI (Bash) / Get Password from OSX Keychain in bash / CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/bash | |
[ $TRACE ] && set -x | |
set -eu | |
if [[ -z "${BASH_VERSINFO[0]}" || ${BASH_VERSINFO[0]} < 4 ]]; then | |
error "You need at least bash version 4. Install with 'brew install bash'" | |
exit 1 | |
fi | |
DEFAULT_VAULT="mydefaultvault" | |
ACCOUNT="myaccount" | |
# 1pw cli helper funcs https://support.1password.com/command-line-getting-started/ | |
install_1pw() { | |
TMPFILE=`mktemp` | |
PWD=`pwd` | |
wget "https://cache.agilebits.com/dist/1P/op/pkg/v0.5.3/op_darwin_amd64_v0.5.3.zip" -O $TMPFILE | |
unzip -d $PWD $TMPFILE | |
rm $TMPFILE | |
gpg --receive-keys 3FEF9748469ADBE15DA7CA80AC2D62742012EA22 | |
gpg --verify op.sig op | |
rm op.sig | |
mv op /usr/local/bin/ | |
op update | |
} | |
1pw_isnotloggedin() { | |
[ op get account ] && return 1 || return 0 | |
} | |
1pw_login() { | |
if [ -z `which op` ]; then | |
echo "1PW CLI seems not to be installed. Hit enter to install or CTRL-C to quit." | |
read | |
1pw_install | |
fi | |
if [ 1pw_isnotloggedin ]; then | |
sessionfile=`mktemp` | |
if op signin $ACCOUNT >$sessionfile 2>>/dev/null; then | |
eval $(cat $sessionfile) | |
rm $sessionfile | |
else | |
read -p "Please enter your 1pw account $ACCOUNT email: " EMAIL | |
eval $(op signin $ACCOUNT.1password.com $EMAIL) | |
fi | |
fi | |
} | |
function 1pw_store_file { | |
1pw_login | |
local filename=$1 | |
local title=${2:-$filename} | |
local tags=${3:-$title} | |
local vault=${4:-$DEFAULT_VAULT} | |
echo "Storing $filename to 1pw-vault $vault ..." | |
op create document "$filename" --vault="$vault" --title="$title" --tags="$tags" | |
} | |
if [ $# -eq 0 ]; then | |
echo "$0 <filename> [title] [tags] [vault]" | |
echo "Store files as documents in 1password" | |
echo "Account: $ACCOUNT" | |
echo "Default Vault: $DEFAULT_VAULT" | |
exit 1 | |
fi | |
FILENAME="$1" | |
TITLE=${2:-$filename} | |
TAGS=${3:-$title} | |
VAULT=${4:-$DEFAULT_VAULT} | |
1pw_store_file "$FILENAME" "$TITLE" "$TAGS" "$VAULT" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getkeychainuser() { | |
echo $(security find-generic-password -s $1 -g 2>&1 | grep "acct" | cut -d \" -f 4) | |
} | |
getkeychainpassword() { | |
echo $(security find-generic-password -s $1 -g 2>&1 | grep "password" | cut -d \" -f 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment