Scripts Library
Common libraries for all scripts in the project. This modularizes and consolidates shared functionality.
Structure
lib/
├── common/ # Common utilities
│ ├── colors.sh # Color definitions and functions
│ ├── logging.sh # Logging functions (info, warn, error, etc.)
│ ├── paths.sh # Path definitions (SCRIPT_DIR, PROJECT_ROOT, etc.)
│ └── utils.sh # Utility functions (confirm, require_command, etc.)
├── config/ # Configuration
│ ├── env.sh # Environment variable loading
│ └── regions.sh # Region code mapping (single source of truth)
├── azure/ # Azure-specific functions
│ └── cli.sh # Azure CLI wrapper functions
└── init.sh # Initialize all libraries (main entry point)
Usage
Quick Start
Most scripts should source init.sh to load all libraries:
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/init.sh"
# Now you can use all functions from the libraries
log_info "Starting script"
ensure_azure_cli || exit 1
Individual Libraries
You can also source individual libraries if you only need specific functionality:
source "$SCRIPT_DIR/lib/common/colors.sh"
source "$SCRIPT_DIR/lib/common/logging.sh"
Examples
Using Logging
source "$SCRIPT_DIR/lib/init.sh"
log_info "Processing regions..."
log_warn "This might take a while"
log_error "Failed to connect"
log_success "Completed successfully"
log_section "Deployment Status"
Using Colors
source "$SCRIPT_DIR/lib/init.sh"
color_red "Error message"
color_green "Success message"
color_yellow "Warning message"
Using Region Codes
source "$SCRIPT_DIR/lib/init.sh"
# Get all regions
get_all_regions
# Get region code
CODE=$(get_region_code "westeurope") # Returns "wst"
# Get region name from code
REGION=$(get_region_name "wst") # Returns "westeurope"
Using Azure CLI Functions
source "$SCRIPT_DIR/lib/init.sh"
# Ensure Azure CLI is ready
ensure_azure_cli || exit 1
# Get subscription info
SUB_ID=$(get_current_subscription)
Migration Guide
To migrate existing scripts to use the library:
-
Remove duplicate code:
- Remove color definitions (use
lib/common/colors.sh) - Remove SCRIPT_DIR/PROJECT_ROOT definitions (use
lib/common/paths.sh) - Remove region code mappings (use
lib/config/regions.sh)
- Remove color definitions (use
-
Replace with library calls:
# Old RED='\033[0;31m' echo -e "${RED}Error${NC}" # New source "$SCRIPT_DIR/lib/init.sh" log_error "Error" -
Use standardized functions:
- Replace
az account showchecks withensure_azure_cli - Replace manual color codes with logging functions
- Replace duplicate region mappings with
get_region_code()
- Replace
Benefits
- Single Source of Truth: Region codes defined once
- Consistency: All scripts use the same colors, logging, and utilities
- Maintainability: Update once, applies everywhere
- Reduced Duplication: Eliminates repetitive code
- Better Error Handling: Standardized error messages and checks