diff --git a/README.md b/README.md index ba58f2f..4b1f72d 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ This plugin relies on the following: In any tmux mode: - `prefix + u` - list login items in a bottom pane. +- `prefix + C` - clear the cache of tmux-1password. ## Install @@ -79,7 +80,7 @@ sessions expires automatically after 30 minutes of inactivity. Customize this plugin by setting these options in your `.tmux.conf` file. Make sure to reload the environment afterwards. -#### Changing the default key-binding for this plugin +#### Changing the default key-binding for running this plugin ``` set -g @1password-key 'x' @@ -87,6 +88,14 @@ set -g @1password-key 'x' Default: `'u'` +#### Changing the default key-binding for clearing the cache of this plugin + +``` +set -g @1password-clear-cache-key 'X' +``` + +Default: 'C' + #### Setting the signin subdomain ``` @@ -115,31 +124,6 @@ set -g @1password-copy-to-clipboard 'on' Default: `'off'` -#### Customize URL Filtering - -By default, all of the items will be shown. If complete customization of url filtering is required, -a `jq` filter can be provided to filter and map items. - -Items come in the following format from which the filter operates: - -```json -[ - { - "uuid": "some-long-uuid", - "overview": { - "URLs": [ - { "u": "sudolikeaboss://local" } - ], - "title": "Some item", - "tags": ["some_tag"] - } - } -] -``` - - -Default: `''` - #### Examples ##### Filtering by tags diff --git a/plugin.tmux b/plugin.tmux index ceffdec..5d84a5e 100755 --- a/plugin.tmux +++ b/plugin.tmux @@ -28,9 +28,12 @@ main() { done local -r opt_key="$(get_tmux_option "@1password-key" "u")" + local -r clear_key="$(get_tmux_option "@1password-clear-cache-key" "C")" tmux bind-key "$opt_key" \ run "tmux split-window -l 10 \"$CURRENT_DIR/scripts/main.sh '#{pane_id}'\"" + + tmux bind-key "$clear_key" run "$CURRENT_DIR/scripts/main.sh clear-cache" } main "$@" diff --git a/scripts/main.sh b/scripts/main.sh index ddeae64..a007222 100755 --- a/scripts/main.sh +++ b/scripts/main.sh @@ -10,7 +10,9 @@ source "./spinner.sh" # ------------------------------------------------------------------------------ -declare -r TMP_TOKEN_FILE="$HOME/.op_tmux_token_tmp" +declare -r TMP_TOKEN_FILE="/tmp/tmux-op-token" +declare -r CACHE_FILE="/tmp/tmux-op-items" +declare -r CACHE_TTL=1800 # 30 minutes, since we cannot fetch passwords with invalid session token declare -r OPT_SUBDOMAIN="$(get_tmux_option "@1password-subdomain" "my")" declare -r OPT_VAULT="$(get_tmux_option "@1password-vault" "")" @@ -45,7 +47,29 @@ op_get_session() { } get_op_items() { + clear_old_cache + if [[ -e $CACHE_FILE ]]; then + echo "$(cat $CACHE_FILE)" + else + fetch_items + fi +} + +clear_old_cache() { + if [[ -e $CACHE_FILE ]]; then + local last_update="$(stat -c %Y $CACHE_FILE)" + local now="$(date +%s)" + local seconds_since_last_update="$(($now-$last_update))" + + # Remove cache file if last cache was from 30 minutes ago + if [[ $seconds_since_last_update > $CACHE_TTL ]]; then + rm $CACHE_FILE + fi + fi +} + +fetch_items() { # The structure (we need) looks like the following: # [ # { @@ -78,6 +102,14 @@ get_op_items() { | jq "$JQ_FILTER" --raw-output } +cache_items() { + local items=$1 + + if ! [[ -e $CACHE_FILE ]]; then + echo "$items" > $CACHE_FILE + fi +} + get_op_item_password() { local -r ITEM_UUID="$1" @@ -120,7 +152,13 @@ get_op_item_password() { # ------------------------------------------------------------------------------ -main() { +clear_cache() { + rm $CACHE_FILE + + display_message "Cache cleared" +} + +prompt_op() { local -r ACTIVE_PANE="$1" local items @@ -147,6 +185,7 @@ main() { spinner_stop fi + cache_items "$items" selected_item_name="$(echo "$items" | awk -F ',' '{ print $1 }' | fzf --no-multi)" if [[ -n "$selected_item_name" ]]; then @@ -164,11 +203,20 @@ main() { # Clear clipboard clear_clipboard 30 else - # Use `send-keys` tmux send-keys -t "$ACTIVE_PANE" "$selected_item_password" fi fi } +main() { + local -r command=$@ + + if [[ $command == "clear-cache" ]]; then + clear_cache + else + prompt_op $@ + fi +} + main "$@"