Skip to content

Commit

Permalink
Implement --purge to clear a cached item
Browse files Browse the repository at this point in the history
  • Loading branch information
semanticart committed Feb 5, 2020
1 parent ae8b56f commit 13dfde1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
17 changes: 14 additions & 3 deletions cache
Expand Up @@ -35,6 +35,10 @@ do
usage usage
exit 0 exit 0
;; ;;
--purge)
purge=0
shift # drop the key
;;
--stale-while-revalidate) --stale-while-revalidate)
stale_while_revalidate="$2" stale_while_revalidate="$2"
shift # drop the key shift # drop the key
Expand Down Expand Up @@ -67,14 +71,21 @@ if [ -z "$cache_key" ]; then
exit 64 exit 64
fi fi


cache_dir=${CACHE_DIR:-$TMPDIR}
cache_file="$cache_dir$cache_key"

if [ -n "$purge" ]; then
if [ -f "$cache_file" ]; then
rm "$cache_file"
fi
exit 0
fi

if [ -z "$1" ] && [ -z "$check_only" ]; then if [ -z "$1" ] && [ -z "$check_only" ]; then
echo "Error: You must provide a command" echo "Error: You must provide a command"
exit 64 exit 64
fi fi


cache_dir=${CACHE_DIR:-$TMPDIR}
cache_file="$cache_dir$cache_key"

fresh () { fresh () {
# if the $cache_file doesn't exist, it can't be fresh # if the $cache_file doesn't exist, it can't be fresh
if [ ! -f "$cache_file" ]; then if [ ! -f "$cache_file" ]; then
Expand Down
22 changes: 22 additions & 0 deletions test/cache.bats
Expand Up @@ -237,3 +237,25 @@ wait_for_second_to_pass() {
[ "$status" -eq 1 ] [ "$status" -eq 1 ]
[ "$output" = "" ] [ "$output" = "" ]
} }

@test "--purge exits with 0 if the content is not yet cached" {
[ ! -f "$CACHE_DIR$TEST_KEY" ]

run ./cache --purge $TEST_KEY
[ "$status" -eq 0 ]
[ "$output" = "" ]

[ ! -f "$CACHE_DIR$TEST_KEY" ]
}

@test "--purge exits with 0 and removes the file if the content is cached" {
touch "$CACHE_DIR$TEST_KEY"

[ -f "$CACHE_DIR$TEST_KEY" ]

run ./cache --purge $TEST_KEY
[ "$status" -eq 0 ]
[ "$output" = "" ]

[ ! -f "$CACHE_DIR$TEST_KEY" ]
}

0 comments on commit 13dfde1

Please sign in to comment.