Skip to content

Commit c049282

Browse files
committed
Improve memo
1 parent b8dde37 commit c049282

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

memo

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
# memo - Memoize command output with cache lifetime
3-
# Usage: memo [--ttl SECONDS] COMMAND [ARGS...]
3+
# Usage: memo [OPTIONS] COMMAND [ARGS...]
44
# Example: memo curl example.com | jq '.data'
55

66
set -euo pipefail
@@ -9,17 +9,51 @@ set -euo pipefail
99
DEFAULT_TTL=3600
1010
CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/memo"
1111

12+
show_help() {
13+
cat <<EOF
14+
memo - Memoize command output with cache lifetime
15+
16+
Usage: memo [OPTIONS] COMMAND [ARGS...]
17+
18+
Options:
19+
--ttl SECONDS Cache lifetime in seconds (default: $DEFAULT_TTL)
20+
-f, --force Force recreation of cache, ignoring existing cached value
21+
-h, --help Show this help message
22+
23+
Examples:
24+
memo curl example.com # Cache curl output for 1 hour
25+
memo --ttl 60 curl example.com # Cache for 60 seconds
26+
memo -f curl example.com # Force refresh, ignore cache
27+
EOF
28+
exit 0
29+
}
30+
1231
# Parse options
1332
TTL="$DEFAULT_TTL"
14-
if [[ "${1:-}" == "--ttl" ]]; then
15-
TTL="$2"
16-
shift 2
17-
fi
33+
FORCE=false
34+
while [[ $# -gt 0 ]]; do
35+
case "$1" in
36+
-h|--help)
37+
show_help
38+
;;
39+
--ttl)
40+
TTL="$2"
41+
shift 2
42+
;;
43+
-f|--force)
44+
FORCE=true
45+
shift
46+
;;
47+
*)
48+
break
49+
;;
50+
esac
51+
done
1852

1953
# Check if command provided
2054
if [[ $# -eq 0 ]]; then
21-
echo "Usage: memo [--ttl SECONDS] COMMAND [ARGS...]" >&2
22-
echo "Example: memo curl example.com" >&2
55+
echo "Usage: memo [OPTIONS] COMMAND [ARGS...]" >&2
56+
echo "Try 'memo --help' for more information." >&2
2357
exit 1
2458
fi
2559

@@ -33,8 +67,8 @@ find "$CACHE_DIR" -type f -mtime +$((DEFAULT_TTL / 86400)) -delete 2>/dev/null |
3367
CACHE_KEY=$(echo -n "$*" | shasum -a 256 | cut -d' ' -f1)
3468
CACHE_FILE="$CACHE_DIR/$CACHE_KEY"
3569

36-
# Check if cache exists and is fresh
37-
if [[ -f "$CACHE_FILE" ]]; then
70+
# Check if cache exists and is fresh (unless force is set)
71+
if [[ "$FORCE" == false && -f "$CACHE_FILE" ]]; then
3872
# Get cache file age in seconds
3973
if [[ "$(uname)" == "Darwin" ]]; then
4074
# macOS

0 commit comments

Comments
 (0)