# Script Library Guide ## 1. Introduction The script library system standardises helper functions used across Pulse installers. It reduces duplication, improves testability, and makes it easier to roll out fixes across the installer fleet. Use the shared libraries when: - Multiple scripts need the same functionality (logging, HTTP, systemd, etc.). - You are refactoring legacy scripts to adopt the v2 pattern. - New features require reusable helpers (e.g., additional service management). ## 2. Architecture Overview ``` scripts/ ├── lib/ # Shared library modules │ ├── common.sh # Core utilities │ ├── systemd.sh # Service management │ ├── http.sh # HTTP/API operations │ └── README.md # API documentation ├── tests/ # Test suites │ ├── run.sh # Test runner │ ├── test-*.sh # Smoke tests │ └── integration/ # Integration tests ├── bundle.sh # Bundler tool ├── bundle.manifest # Bundle configuration └── install-*.sh # Installer scripts dist/ # Generated bundled scripts └── install-*.sh # Ready for distribution ``` ### Development & Bundling Workflow ```mermaid flowchart LR Code[Write Code
scripts/lib] Test[Run Tests] Bundle[Bundle
make bundle-scripts] Dist[Distribute
dist/*.sh] Code --> Test Test --> Bundle Bundle --> Dist ``` This workflow emphasizes the library's modular design: develop reusable modules in `scripts/lib`, test thoroughly, bundle for distribution, and validate bundled artifacts before release. ## 3. Using the Library in Your Script ```bash #!/usr/bin/env bash # Example installer using shared libraries LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)/lib" if [[ -f "$LIB_DIR/common.sh" ]]; then source "$LIB_DIR/common.sh" source "$LIB_DIR/systemd.sh" source "$LIB_DIR/http.sh" fi common::init "$@" main() { common::ensure_root --allow-sudo --args "$@" common::log_info "Starting installation..." # ...script logic... } main "$@" ``` ## 4. Common Migration Patterns **Logging** ```bash # Before echo "[INFO] Installing..." # After common::log_info "Installing..." ``` **Privilege Escalation** ```bash # Before if [[ $EUID -ne 0 ]]; then echo "Must run as root"; exit 1 fi # After common::ensure_root --allow-sudo --args "$@" ``` **Downloads** ```bash # Before curl -o file.tar.gz http://example.com/file.tar.gz # After http::download --url http://example.com/file.tar.gz --output file.tar.gz ``` **Systemd Unit Creation** ```bash # Before cat > /etc/systemd/system/my.service <<'EOF' ... systemctl daemon-reload systemctl enable my.service systemctl start my.service # After systemd::create_service /etc/systemd/system/my.service <<'EOF' ... EOF systemd::enable_and_start "my.service" ``` ## 5. Creating New Library Modules Create a new module when functionality is reused across scripts or complex enough to warrant dedicated helpers. Template: ```bash #!/usr/bin/env bash # Module: mymodule.sh mymodule::do_thing() { local arg="$1" # implementation } ``` Add the module to `scripts/lib`, document exported functions in `scripts/lib/README.md`, and update `scripts/bundle.manifest` for any bundles that need it. ## 6. Testing Requirements Every migrated script must include: 1. Smoke test (`scripts/tests/test-