Skip to content

Instantly share code, notes, and snippets.

@StevenRudenko
Created March 24, 2023 16:06
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 StevenRudenko/23d4f882ff95b7e9fab03fe46316240a to your computer and use it in GitHub Desktop.
Save StevenRudenko/23d4f882ff95b7e9fab03fe46316240a to your computer and use it in GitHub Desktop.
1Password upload tool
#!/bin/bash
if ! loc="$(type -p "op")" || [[ -z $loc ]]; then
echo "1Password CLI is not found. Try brew install --cask 1password/tap/1password-cli"
exit 1
fi
if ! loc="$(type -p "jq")" || [[ -z $loc ]]; then
echo "jq is not found. Try brew install jq"
exit 1
fi
ROOT_PATH=$(pwd)
export OP_VAULT='Sync'
export OP_ROOT_TAG='Sync'
print_help() {
echo "Used to upload files from specified folder to 1Password to keep them in safe place."
echo
echo "Syntax: op-upload [--path|--vault|--tag]"
echo "Example: op-upload --vault=Archive --tag=Archive"
echo
echo "Options:"
echo "--path Path to a folder to be uploaded recursively [default: a place of call]."
echo "--vault 1Password vault to be used to store files [default: Sync]."
echo "--tag Root tag that will be used to mark uploaded files [default: Sync]."
echo "-h|--help Print this help."
echo
}
for i in "$@"; do
case $i in
-h | --help)
print_help
exit 0
;;
--path=*)
ROOT_PATH=$(echo $i | sed 's/--path=//')
if [[ "$ROOT_PATH" != /* ]]; then
ROOT_PATH=$(pwd)/$ROOT_PATH
fi
;;
--vault=*)
OP_VAULT=$(echo $i | sed 's/--vault=//')
;;
--tag=*)
OP_ROOT_TAG=$(echo $i | sed 's/--tag=//')
;;
esac
done
export OP_DOCUMENTS_LIST_JSON=$(op document list --vault $OP_VAULT --format=json)
pushd $ROOT_PATH >/dev/null
function processFile() {
path=$1
vault=$OP_VAULT
# op item would have same name as file
filename=$(basename $path)
# creating the tag based on path
tag="$OP_ROOT_TAG/$(dirname $path | cut -c3-)"
# uploading document to 1Password
# check if file exists with same filename and tag
uuids=($(echo $OP_DOCUMENTS_LIST_JSON | jq -r "sort_by(.updated_at | fromdateiso8601) | map(select(.title == \"$filename\")) | [ .[].id ] | @sh" | tr -d \'))
if ((${#uuids[@]} == 0)); then
# no file found with this name
echo "New file. Uploading $filename with $tag tag"
op document create $path --title $filename --vault $OP_VAULT --tags $tag >/dev/null
fi
# look for tags, to upload files with same name but different path
for i in "${uuids[@]}"; do
item=$(op item get $i --format=json)
tags=$(echo $item | jq -r "[ .tags ]")
if [[ $tags == *"$tag"* ]]; then
echo "Skip $filename. File exists"
else
echo "File with same name exist. Tag mismatch. Uploading $filename with $tag tag"
op document create $path --title $filename --vault $OP_VAULT --tags $tag >/dev/null
fi
done
# echo "Uploading $filename with $tag tag"
# op document create $path --title $filename --vault $OP_VAULT --tags $tag >/dev/null
}
export -f processFile
echo "Upload files from $ROOT_PATH to 1Password vault $OP_VAULT"
find . -type f -exec bash -c 'processFile "$0"' {} \;
popd >/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment