Last active
July 30, 2025 23:40
-
-
Save somewhatabstract/be9acf6150880bcf7db82f64d02541f0 to your computer and use it in GitHub Desktop.
BashRC functions for printing some versions out to the console on console open with expirable cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generic caching function for commands with time-based expiry | |
cached_command() { | |
# The command to run. | |
local command="$1" | |
shift | |
# Default values | |
local cadence=3600 # 1 hour default | |
local cache_key="" | |
local cache_name="" | |
local cache_output=false | |
local force_flag="" | |
# Parse remaining arguments for flags | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--cadence=*) | |
cadence="${1#*=}" | |
shift | |
;; | |
--name=*) | |
cmd_name="${1#*=}" | |
shift | |
;; | |
--key=*) | |
cache_key="${1#*=}" | |
shift | |
;; | |
--cache-output) | |
cache_output=true | |
shift | |
;; | |
-f|-u) | |
force_flag="$1" | |
shift | |
;; | |
*) | |
# Unknown option, could be passed through or ignored | |
shift | |
;; | |
esac | |
done | |
# Validate required parameters | |
if [[ -z "$cache_key" ]]; then | |
echo "Error: --key=<cache_key> is required" | |
return 1 | |
fi | |
local cache_file="/tmp/${cache_key}_cache" | |
# Check if we need to refresh the cache | |
if [[ "$force_flag" == "-f" || "$force_flag" == "-u" || ! -f "$cache_file" || $(($(date +%s) - $(stat -f %m "$cache_file"))) -ge $cadence ]]; then | |
if [[ "$cache_output" == true ]]; then | |
# Cache the command output | |
eval "$command" > "$cache_file" | |
cat "$cache_file" | |
else | |
# Just run the command and update timestamp | |
eval "$command" | |
touch "$cache_file" | |
fi | |
else | |
# Calculate cache age and return cached content with age message | |
local cache_age=$(($(date +%s) - $(stat -f %m "$cache_file"))) | |
local age_minutes=$((cache_age / 60)) | |
local age_hours=$((cache_age / 3600)) | |
if [[ "$cache_output" == true ]]; then | |
if [[ $cache_age -lt 60 ]]; then | |
echo "📋${cmd_name} cached content (${cache_age}s old):" | |
elif [[ $cache_age -lt 3600 ]]; then | |
echo "📋${cmd_name} cached content (${age_minutes}m old):" | |
else | |
echo "📋${cmd_name} cached content (${age_hours}h old):" | |
fi | |
cat "$cache_file" | |
else | |
# Show skip message | |
if [[ $cache_age -lt 60 ]]; then | |
echo "⏭️ ${cmd_name} skipped (run ${cache_age}s ago)" | |
elif [[ $cache_age -lt 3600 ]]; then | |
echo "⏭️ ${cmd_name} skipped (run ${age_minutes}m ago)" | |
else | |
echo "⏭️ ${cmd_name} skipped (run ${age_hours}h ago)" | |
fi | |
fi | |
fi | |
} | |
printversions() { | |
cached_command " | |
echo \"bash: \$(bash --version | head -n1 | cut -d \",\" -f2- | cut -d\" \" -f3-)\" | |
echo \" git: \$(git --version | cut -d' ' -f3)\" | |
echo \" gh: \$(gh --version | head -n1 | cut -d' ' -f3 -f4)\" | |
echo \" go: \$(go version | cut -d' ' -f3 | cut -c3-)\" | |
echo \"java: \$(java -version 2>&1 | head -n 1 | awk -F '\\\"' '{print \$2}')\" | |
echo \"node: \$(nvm version | cut -c2-) (latest: \$(nvm version-remote \$(nvm version | cut -c2- | cut -d'.' -f1) | cut -c2-))\" | |
echo \" npm: \$(npm --version)\" | |
echo \" nvm: \$(nvm --version)\" | |
echo \" olc: \$(jq -r '.version' \$KA_DEVROOT/our-lovely-cli/package.json).\$(git -C \$KA_DEVROOT/our-lovely-cli rev-parse HEAD | cut -c1-5)\" | |
echo \"pnpm: \$(pnpm --version)\" | |
echo \"ruby: \$(ruby -v | cut -d' ' -f2)\" | |
echo \"rust: \$(rustc --version | cut -d' ' -f2)\" | |
" --name=printversions --cadence=3600 --key=printversions --cache-output "$@" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment