/****************************************************************************** __ ____ __ / / ___ ____ _/ __/_____________ _/ /__ / / / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \ / /___/ __/ /_/ / __(__ ) /__/ /_/ / / __/ /_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/ (C)opyright 2025, Leafscale, LLC - https://www.leafscale.com Project: Zygaena Filename: config.reef Authors: Chris Tusa License: Description: Configuration file loading and access ******************************************************************************/ module core.config import types import io.file import core.str import encoding.toml export // Configuration structure type Config // Load configuration from file (with defaults if not found) fn load(): Config // Access functions for common settings fn get_arch(cfg: Config): string fn get_jobs(cfg: Config): int fn get_confirm(cfg: Config): bool fn get_color(cfg: Config): bool // Path accessors fn get_ports_dir(cfg: Config): string fn get_packages_dir(cfg: Config): string fn get_sources_dir(cfg: Config): string fn get_database_dir(cfg: Config): string fn get_build_dir(cfg: Config): string fn get_index_dir(cfg: Config): string // Legacy (for compatibility) fn get_work_dir(cfg: Config): string fn get_pkg_dir(cfg: Config): string // Root and prefix accessors fn get_root(cfg: Config): string fn get_prefix(cfg: Config): string fn get_sysconfdir(cfg: Config): string // Root-aware path accessors (prepend root to path) fn get_database_dir_rooted(cfg: Config): string fn get_install_prefix(cfg: Config): string // Apply command-line overrides to config fn apply_overrides(cfg: Config, root: string, prefix: string, no_chroot: bool): Config // Build settings fn get_cflags(cfg: Config): string fn get_cxxflags(cfg: Config): string fn get_makeflags(cfg: Config): string fn get_strip(cfg: Config): bool fn get_fallback_to_source(cfg: Config): bool fn get_compress_man(cfg: Config): bool fn get_clean_work(cfg: Config): bool fn get_clean_pkg(cfg: Config): bool fn get_logging(cfg: Config): bool fn get_quiet(cfg: Config): bool // Network settings fn get_timeout(cfg: Config): int fn get_retries(cfg: Config): int fn get_use_proxy(cfg: Config): bool // Sync settings fn get_auto_refresh(cfg: Config): bool // Cache settings fn get_keep_packages(cfg: Config): bool fn get_keep_sources(cfg: Config): bool // Security settings fn get_require_signed_repos(cfg: Config): bool fn get_require_signed_packages(cfg: Config): bool // Install settings fn get_chroot_scripts(cfg: Config): bool end export // Configuration structure type Config = struct // [general] arch: string jobs: int confirm: bool color: bool // [paths] root: string prefix: string sysconfdir: string ports_dir: string packages_dir: string sources_dir: string database_dir: string build_dir: string index_dir: string // Legacy (kept for compatibility) work_dir: string pkg_dir: string // [build] cflags: string cxxflags: string makeflags: string strip: bool compress_man: bool fallback_to_source: bool clean_work: bool clean_pkg: bool logging: bool quiet: bool // [network] timeout: int retries: int use_proxy: bool // [sync] auto_refresh: bool // [cache] keep_packages: bool keep_sources: bool // [security] require_signed_repos: bool require_signed_packages: bool // [install] chroot_scripts: bool end Config // Create a default configuration fn default_config(): Config return Config{ // General arch: "amd64", jobs: 0, // 0 = auto-detect confirm: true, color: true, // Paths (defaults from types) root: "", prefix: "/usr/local", sysconfdir: "/etc", ports_dir: types.get_ports_dir(), packages_dir: types.get_packages_dir(), sources_dir: types.get_sources_dir(), database_dir: types.get_db_dir(), build_dir: types.get_build_dir(), index_dir: types.get_index_dir(), // Legacy (for compatibility) work_dir: types.get_build_dir(), pkg_dir: types.get_build_dir(), // Build cflags: "-O2 -pipe", cxxflags: "-O2 -pipe", makeflags: "", strip: true, compress_man: true, fallback_to_source: true, clean_work: true, clean_pkg: true, logging: true, quiet: false, // Network timeout: 30, retries: 3, use_proxy: false, // Sync auto_refresh: false, // Cache keep_packages: true, keep_sources: true, // Security require_signed_repos: false, require_signed_packages: false, // Install chroot_scripts: true } end default_config // Load configuration from file fn load(): Config mut cfg = default_config() // Check if config file exists if not file.fileExists(types.get_config_file()) return cfg end if // Read config file let content = file.readFile(types.get_config_file()) if str.length(content) == 0 return cfg end if // Parse TOML let keys = toml.toml_alloc_keys() let vals = toml.toml_alloc_values() let entry_count = toml.toml_parse(content, keys, vals) if entry_count == 0 return cfg end if // [general] section if toml.toml_has_key(keys, vals, entry_count, "general.arch") cfg.arch = toml.toml_get(keys, vals, entry_count, "general.arch") end if if toml.toml_has_key(keys, vals, entry_count, "general.jobs") cfg.jobs = toml.toml_get_int(keys, vals, entry_count, "general.jobs") end if if toml.toml_has_key(keys, vals, entry_count, "general.confirm") cfg.confirm = toml.toml_get_bool(keys, vals, entry_count, "general.confirm") end if if toml.toml_has_key(keys, vals, entry_count, "general.color") cfg.color = toml.toml_get_bool(keys, vals, entry_count, "general.color") end if // [paths] section if toml.toml_has_key(keys, vals, entry_count, "paths.root") cfg.root = toml.toml_get(keys, vals, entry_count, "paths.root") end if if toml.toml_has_key(keys, vals, entry_count, "paths.prefix") cfg.prefix = toml.toml_get(keys, vals, entry_count, "paths.prefix") end if if toml.toml_has_key(keys, vals, entry_count, "paths.sysconfdir") cfg.sysconfdir = toml.toml_get(keys, vals, entry_count, "paths.sysconfdir") end if if toml.toml_has_key(keys, vals, entry_count, "paths.ports") cfg.ports_dir = toml.toml_get(keys, vals, entry_count, "paths.ports") end if if toml.toml_has_key(keys, vals, entry_count, "paths.packages") cfg.packages_dir = toml.toml_get(keys, vals, entry_count, "paths.packages") end if if toml.toml_has_key(keys, vals, entry_count, "paths.sources") cfg.sources_dir = toml.toml_get(keys, vals, entry_count, "paths.sources") end if if toml.toml_has_key(keys, vals, entry_count, "paths.database") cfg.database_dir = toml.toml_get(keys, vals, entry_count, "paths.database") end if if toml.toml_has_key(keys, vals, entry_count, "paths.build") cfg.build_dir = toml.toml_get(keys, vals, entry_count, "paths.build") end if if toml.toml_has_key(keys, vals, entry_count, "paths.index") cfg.index_dir = toml.toml_get(keys, vals, entry_count, "paths.index") end if // Legacy path support (maps to build_dir) if toml.toml_has_key(keys, vals, entry_count, "paths.work") cfg.work_dir = toml.toml_get(keys, vals, entry_count, "paths.work") cfg.build_dir = cfg.work_dir // Also set new field end if if toml.toml_has_key(keys, vals, entry_count, "paths.pkg") cfg.pkg_dir = toml.toml_get(keys, vals, entry_count, "paths.pkg") end if // [build] section if toml.toml_has_key(keys, vals, entry_count, "build.cflags") cfg.cflags = toml.toml_get(keys, vals, entry_count, "build.cflags") end if if toml.toml_has_key(keys, vals, entry_count, "build.cxxflags") cfg.cxxflags = toml.toml_get(keys, vals, entry_count, "build.cxxflags") end if if toml.toml_has_key(keys, vals, entry_count, "build.makeflags") cfg.makeflags = toml.toml_get(keys, vals, entry_count, "build.makeflags") end if if toml.toml_has_key(keys, vals, entry_count, "build.strip") cfg.strip = toml.toml_get_bool(keys, vals, entry_count, "build.strip") end if if toml.toml_has_key(keys, vals, entry_count, "build.compress_man") cfg.compress_man = toml.toml_get_bool(keys, vals, entry_count, "build.compress_man") end if if toml.toml_has_key(keys, vals, entry_count, "build.fallback_to_source") cfg.fallback_to_source = toml.toml_get_bool(keys, vals, entry_count, "build.fallback_to_source") end if if toml.toml_has_key(keys, vals, entry_count, "build.clean_work") cfg.clean_work = toml.toml_get_bool(keys, vals, entry_count, "build.clean_work") end if if toml.toml_has_key(keys, vals, entry_count, "build.clean_pkg") cfg.clean_pkg = toml.toml_get_bool(keys, vals, entry_count, "build.clean_pkg") end if if toml.toml_has_key(keys, vals, entry_count, "build.logging") cfg.logging = toml.toml_get_bool(keys, vals, entry_count, "build.logging") end if if toml.toml_has_key(keys, vals, entry_count, "build.quiet") cfg.quiet = toml.toml_get_bool(keys, vals, entry_count, "build.quiet") end if // [network] section if toml.toml_has_key(keys, vals, entry_count, "network.timeout") cfg.timeout = toml.toml_get_int(keys, vals, entry_count, "network.timeout") end if if toml.toml_has_key(keys, vals, entry_count, "network.retries") cfg.retries = toml.toml_get_int(keys, vals, entry_count, "network.retries") end if if toml.toml_has_key(keys, vals, entry_count, "network.use_proxy") cfg.use_proxy = toml.toml_get_bool(keys, vals, entry_count, "network.use_proxy") end if // [sync] section if toml.toml_has_key(keys, vals, entry_count, "sync.auto_refresh") cfg.auto_refresh = toml.toml_get_bool(keys, vals, entry_count, "sync.auto_refresh") end if // [cache] section if toml.toml_has_key(keys, vals, entry_count, "cache.keep_packages") cfg.keep_packages = toml.toml_get_bool(keys, vals, entry_count, "cache.keep_packages") end if if toml.toml_has_key(keys, vals, entry_count, "cache.keep_sources") cfg.keep_sources = toml.toml_get_bool(keys, vals, entry_count, "cache.keep_sources") end if // [security] section if toml.toml_has_key(keys, vals, entry_count, "security.require_signed_repos") cfg.require_signed_repos = toml.toml_get_bool(keys, vals, entry_count, "security.require_signed_repos") end if if toml.toml_has_key(keys, vals, entry_count, "security.require_signed_packages") cfg.require_signed_packages = toml.toml_get_bool(keys, vals, entry_count, "security.require_signed_packages") end if // [install] section if toml.toml_has_key(keys, vals, entry_count, "install.chroot_scripts") cfg.chroot_scripts = toml.toml_get_bool(keys, vals, entry_count, "install.chroot_scripts") end if return cfg end load // Accessor functions fn get_arch(cfg: Config): string return cfg.arch end get_arch fn get_jobs(cfg: Config): int return cfg.jobs end get_jobs fn get_confirm(cfg: Config): bool return cfg.confirm end get_confirm fn get_color(cfg: Config): bool return cfg.color end get_color fn get_ports_dir(cfg: Config): string return cfg.ports_dir end get_ports_dir fn get_packages_dir(cfg: Config): string return cfg.packages_dir end get_packages_dir fn get_sources_dir(cfg: Config): string return cfg.sources_dir end get_sources_dir fn get_database_dir(cfg: Config): string return cfg.database_dir end get_database_dir fn get_build_dir(cfg: Config): string return cfg.build_dir end get_build_dir fn get_index_dir(cfg: Config): string return cfg.index_dir end get_index_dir // Legacy accessors (for compatibility) fn get_work_dir(cfg: Config): string return cfg.build_dir end get_work_dir fn get_pkg_dir(cfg: Config): string return cfg.build_dir end get_pkg_dir fn get_cflags(cfg: Config): string return cfg.cflags end get_cflags fn get_cxxflags(cfg: Config): string return cfg.cxxflags end get_cxxflags fn get_makeflags(cfg: Config): string return cfg.makeflags end get_makeflags fn get_strip(cfg: Config): bool return cfg.strip end get_strip fn get_fallback_to_source(cfg: Config): bool return cfg.fallback_to_source end get_fallback_to_source fn get_compress_man(cfg: Config): bool return cfg.compress_man end get_compress_man fn get_clean_work(cfg: Config): bool return cfg.clean_work end get_clean_work fn get_clean_pkg(cfg: Config): bool return cfg.clean_pkg end get_clean_pkg fn get_logging(cfg: Config): bool return cfg.logging end get_logging fn get_quiet(cfg: Config): bool return cfg.quiet end get_quiet fn get_timeout(cfg: Config): int return cfg.timeout end get_timeout fn get_retries(cfg: Config): int return cfg.retries end get_retries fn get_use_proxy(cfg: Config): bool return cfg.use_proxy end get_use_proxy fn get_auto_refresh(cfg: Config): bool return cfg.auto_refresh end get_auto_refresh fn get_keep_packages(cfg: Config): bool return cfg.keep_packages end get_keep_packages fn get_keep_sources(cfg: Config): bool return cfg.keep_sources end get_keep_sources fn get_require_signed_repos(cfg: Config): bool return cfg.require_signed_repos end get_require_signed_repos fn get_require_signed_packages(cfg: Config): bool return cfg.require_signed_packages end get_require_signed_packages fn get_chroot_scripts(cfg: Config): bool return cfg.chroot_scripts end get_chroot_scripts // Get the alternate root path (empty string means /) fn get_root(cfg: Config): string return cfg.root end get_root // Get the installation prefix (default /usr/local) fn get_prefix(cfg: Config): string return cfg.prefix end get_prefix // Get the system configuration directory (default /etc) fn get_sysconfdir(cfg: Config): string return cfg.sysconfdir end get_sysconfdir // Get database directory with root prepended fn get_database_dir_rooted(cfg: Config): string if str.length(cfg.root) == 0 return cfg.database_dir end if return cfg.root + cfg.database_dir end get_database_dir_rooted // Get the full install prefix (root + prefix) // This is where package files should be installed fn get_install_prefix(cfg: Config): string if str.length(cfg.root) == 0 return cfg.prefix end if return cfg.root + cfg.prefix end get_install_prefix // Apply command-line overrides to config // Call this after loading config to override with --root and --prefix flags fn apply_overrides(cfg: Config, root: string, prefix: string, no_chroot: bool): Config mut result = cfg if str.length(root) > 0 result.root = root end if if str.length(prefix) > 0 result.prefix = prefix end if if no_chroot result.chroot_scripts = false end if return result end apply_overrides end module