Skip to main content

Config Module

This module provides a priority-based configuration loading system and reusable validation hooks for the root script.

Module Overview

  • Purpose: Provide a priority-based configuration loading system and reusable validation hooks for the root script.
  • Primary modules:
    • src/lib/config/loader.sh: configuration file parsing and prioritized loading
    • src/lib/config/validator.sh: required environment variable validation (non-fatal; returns status)

Configuration Priority System

Configuration values may come from:

  1. Environment variables (highest precedence)
  2. Home config: ~/.bearflare/.config
  3. Project config: .env (project root)

The system is implemented by loading config files in order, but only assigning variables that are not already set.

Dependencies

  • src/lib/logging/logger.sh: log(), log_with_timing()
  • src/lib/utils/timing.sh: now_ns()

Public API

load_config_file(file_path)

Parse a config file and export values, but only for keys not already set in the environment. File format: KEY=VALUE or KEY="VALUE" (double quotes are stripped). Blank lines and # comments are ignored.

load_all_configs()

Load all supported configuration files using the priority system. Files: ~/.bearflare/.config, .env. May export environment variables and prints "Configuration loaded from …" info to stdout when applicable.

validate_required_env_vars()

Validate that required Cloudflare env vars exist without exiting. Checks: CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_API_TOKEN, CLOUDFLARE_D1_DATABASE_ID. Returns non-zero when any are missing (caller decides whether to exit).

In bearflare.sh:

  1. Source modules (core/timing/logging/config).
  2. Initialize logging (init_logging) early.
  3. Call load_all_configs to populate values.
  4. Call validate_required_env_vars and handle its return:
    • If non-zero: print usage and exit with code 1.