Skip to content

Instantly share code, notes, and snippets.

@gabssnake
Created June 2, 2024 19:10
Show Gist options
  • Save gabssnake/846140a330b880dc4bdaa52b10340ed8 to your computer and use it in GitHub Desktop.
Save gabssnake/846140a330b880dc4bdaa52b10340ed8 to your computer and use it in GitHub Desktop.
Nifty unobstrusive log and requirements functions for tiny Taskfile scripts
#!/bin/bash
function main {
log 'you said:' "$1"
}
# Use stderr to avoid polluting stdout
function log {
printf "%s\n" "$*" >&2;
}
# Convenient way to fail-fast and be helpful
function requires {
for cmd in "$@"; do
if ! command -v "$cmd" &> /dev/null; then
log "You need '$cmd', try 'brew install $cmd' or whatever."
exit
fi
done
}
# Usage:
# $ ./script.sh
# $ ./script.sh main
# $ ./script.sh log coucou
requires "curl" "jq"
"${@:-main}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment