# 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:

```bash
#!/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:

```bash
source "$SCRIPT_DIR/lib/common/colors.sh"
source "$SCRIPT_DIR/lib/common/logging.sh"
```

### Examples

#### Using Logging

```bash
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

```bash
source "$SCRIPT_DIR/lib/init.sh"

color_red "Error message"
color_green "Success message"
color_yellow "Warning message"
```

#### Using Region Codes

```bash
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

```bash
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:

1. **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`)

2. **Replace with library calls**:
   ```bash
   # Old
   RED='\033[0;31m'
   echo -e "${RED}Error${NC}"
   
   # New
   source "$SCRIPT_DIR/lib/init.sh"
   log_error "Error"
   ```

3. **Use standardized functions**:
   - Replace `az account show` checks with `ensure_azure_cli`
   - Replace manual color codes with logging functions
   - Replace duplicate region mappings with `get_region_code()`

## Benefits

1. **Single Source of Truth**: Region codes defined once
2. **Consistency**: All scripts use the same colors, logging, and utilities
3. **Maintainability**: Update once, applies everywhere
4. **Reduced Duplication**: Eliminates repetitive code
5. **Better Error Handling**: Standardized error messages and checks

