|
root / src / core / config.reef
config.reef Reef 526 lines 15.5 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/******************************************************************************
               __               ____                __
              / /   ___  ____ _/ __/_____________ _/ /__
             / /   / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \
            / /___/  __/ /_/ / __(__  ) /__/ /_/ / /  __/
           /_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/

   (C)opyright 2025, Leafscale, LLC -  https://www.leafscale.com

   Project: Zygaena
  Filename: config.reef
   Authors: Chris Tusa <chris.tusa@leafscale.com>
   License: <see LICENSE file included with this source code>
Description: Configuration file loading and access

******************************************************************************/

module core.config

import types
import io.file
import core.str
import encoding.toml
import core.result as res

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 = res.unwrap_or(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 = res.unwrap_or(toml.toml_parse(content, keys, vals), 0)

    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