1
0
Fork 0
repo2html/hashcache

69 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
#
# Hash cache storage and retrieval operations
#
# Copyright (C) 2012 Mike Gerwitz
#
# This file is part of repo2html.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
# intended to be configured from a configuration file; lowercase to help
# mitigate global env var conflicts
cachefile="${hashcache:-.hashcache}"
# 'cause it rhymes and 'cause it's useful
_cache()
{
hash="$1"
value="$2"
echo "$hash $value" >>"$cachefile"
}
# retrieves the value associated with the given hash
_retrieve()
{
hash="$1"
# if multiple identical hashes exist, then the last one takes precedence
# (ideally, such a thing should never happen, but it's best to be prepared)
grep "^$hash " "$cachefile" \
| tail -n1 \
| cut -d' ' -f2-
}
# clears the cache file
_wipe()
{
>"$cachefile"
}
# the hash is required, but the value to set it to is optional
hash="${1?No hash provided}"
set="$2"
# poor man's argument parsing (we don't need no stinkin' options [yet]); "clear"
# is not a valid hash currently, though I suppose a sadistic repository could
# allow such, and we're supposed to be repo-agnostic
if [ "$hash" == 'clear' ]; then
_wipe
elif [ "$set" ]; then
_cache "$hash" "$set"
else
_retrieve "$hash"
fi