Created
June 2, 2024 19:10
-
-
Save gabssnake/846140a330b880dc4bdaa52b10340ed8 to your computer and use it in GitHub Desktop.
Nifty unobstrusive log and requirements functions for tiny Taskfile scripts
This file contains 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
#!/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