diff --git a/scripts/utils.sh b/scripts/utils.sh new file mode 100644 index 0000000..5035876 --- /dev/null +++ b/scripts/utils.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# Common utility functions for shell scripts + +# Checks if a tool is installed and available in PATH. +# Usage: is_tool_installed +# Returns 0 if found, 1 otherwise. +is_tool_installed() { + command -v "$1" &> /dev/null +} + +# Ensures a tool is installed. Exits with an error if not. +# Usage: ensure_tool_installed [optional_purpose_message] +# Example: ensure_tool_installed curl "to download files" +ensure_tool_installed() { + local tool_name="$1" + local purpose_message="$2" + + if ! is_tool_installed "${tool_name}"; then + echo "Error: Required tool '${tool_name}' could not be found." >&2 + if [ -n "${purpose_message}" ]; then + echo " It is needed ${purpose_message}." >&2 + fi + echo " Please install it first and ensure it is in your PATH." >&2 + exit 1 + fi +} \ No newline at end of file