Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Feature/cache op results #2

Merged
merged 2 commits into from
Feb 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions scripts/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ op_get_session() {
}

get_op_items() {
local cache_file="/tmp/tmux-op-items"

if [ -e $cache_file ]; then
echo "$(cat $cache_file)"
else
fetch_items
fi
}

fetch_items() {
# The structure (we need) looks like the following:
# [
# {
Expand Down Expand Up @@ -84,6 +93,25 @@ get_op_items() {
| jq "$JQ_FILTER" --raw-output
}

cache_items() {
local items=$1
local cache_file="/tmp/tmux-op-items"

if ! [ -e $cache_file ]; then
echo "$items" > $cache_file
else
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 6h ago
if [[ $seconds_since_last_update < 21600 ]]; then
display_message "ok"
rm $cache_file
fi
fi
}

get_op_item_password() {
local -r ITEM_UUID="$1"

Expand Down Expand Up @@ -153,6 +181,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
Expand Down