11#! /usr/bin/env bash
2+ #
3+ # common.sh
4+ #
5+ # Purpose:
6+ # Provides shared utility functions for all scripts in the repository.
7+ #
8+ # Usage:
9+ # source "$(dirname "${BASH_SOURCE[0]}")/common.sh"
10+ #
11+ # Required tools/dependencies:
12+ # - bash
13+ #
14+ # Expected output:
15+ # None (defines functions for other scripts).
16+ #
17+ # Exit behavior:
18+ # The utility functions may exit the script with code 1 if requirements are not met.
219
320set -euo pipefail
421
22+ # The root directory of the repository, calculated relative to this script's location.
523REPO_ROOT=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) /.." && pwd) "
624
25+ # Function: require_command
26+ # Purpose: Checks if a command exists in the PATH.
27+ # Inputs:
28+ # $1 - The name of the command to check.
29+ # Outputs:
30+ # Prints an error message to stderr if the command is missing.
31+ # Exit behavior:
32+ # Exits with code 1 if the command is not found.
733require_command () {
834 local command_name=" $1 "
935 if ! command -v " ${command_name} " > /dev/null 2>&1 ; then
@@ -12,6 +38,14 @@ require_command() {
1238 fi
1339}
1440
41+ # Function: require_env_var
42+ # Purpose: Checks if an environment variable is set and non-empty.
43+ # Inputs:
44+ # $1 - The name of the environment variable to check.
45+ # Outputs:
46+ # Prints an error message to stderr if the variable is missing.
47+ # Exit behavior:
48+ # Exits with code 1 if the variable is not set or empty.
1549require_env_var () {
1650 local variable_name=" $1 "
1751 if [[ -z " ${! variable_name:- } " ]]; then
@@ -20,6 +54,14 @@ require_env_var() {
2054 fi
2155}
2256
57+ # Function: require_solace_env_vars
58+ # Purpose: Validates that all required Solace Cloud environment variables are set.
59+ # Inputs:
60+ # $1 - (Optional) Name of the caller script for logging purposes.
61+ # Outputs:
62+ # Prints error messages to stderr listing the missing variables.
63+ # Exit behavior:
64+ # Exits with code 1 if any required Solace variable is missing.
2365require_solace_env_vars () {
2466 local caller_name=" ${1:- this script} "
2567 local required_vars=(
@@ -43,6 +85,14 @@ require_solace_env_vars() {
4385 done
4486}
4587
88+ # Function: enter_module
89+ # Purpose: Changes the current working directory to a specific module.
90+ # Inputs:
91+ # $1 - The path to the module (absolute or relative to REPO_ROOT).
92+ # Outputs:
93+ # None.
94+ # Exit behavior:
95+ # Returns or exits if 'cd' fails (due to 'set -e').
4696enter_module () {
4797 local module_path=" $1 "
4898 if [[ " ${module_path} " = /* ]]; then
0 commit comments