AtlatestRepositoryfjo
1
;;; (fjo config) - Configuration management.2
;;;3
;;; Reads Forgejo credentials and instance URL from environment variables.4
;;; Call (fjo-load-env!) at startup to load .env-fjo from the working directory.6
(define-library (fjo config)7
(import (sigil core)8
(sigil string)9
(sigil process)10
(sigil dotenv))11
(export fjo-token12
fjo-base-url13
fjo-woodpecker-token14
fjo-woodpecker-url15
fjo-load-env!)16
(begin18
;;; Load .env-fjo from the working directory if present.19
(define (fjo-load-env!)20
(: -> void?)21
(load-env-file! ".env-fjo"))23
;;; Get the Forgejo API token.24
;;;25
;;; Raises an error if FORGEJO_TOKEN is not set.26
(define (fjo-token)27
(: -> string?)28
(let ((val (getenv "FORGEJO_TOKEN")))29
(if (and val (not (string=? val "")))30
val31
(error "FORGEJO_TOKEN environment variable must be set"))))33
;;; Get the Forgejo instance base URL.34
;;;35
;;; Defaults to https://codeberg.org if FORGEJO_URL is not set.36
(define (fjo-base-url)37
(: -> string?)38
(or (getenv "FORGEJO_URL") "https://codeberg.org"))40
;;; Get the Woodpecker CI API token.41
;;;42
;;; Uses WOODPECKER_TOKEN if set, otherwise falls back to FORGEJO_TOKEN.43
(define (fjo-woodpecker-token)44
(: -> string?)45
(let ((val (getenv "WOODPECKER_TOKEN")))46
(if (and val (not (string=? val "")))47
val48
(fjo-token))))50
;;; Get the Woodpecker CI base URL.51
;;;52
;;; Uses WOODPECKER_URL if set, otherwise derives from FORGEJO_URL53
;;; by prepending "ci." to the hostname.54
(define (fjo-woodpecker-url)55
(: -> string?)56
(let ((val (getenv "WOODPECKER_URL")))57
(if (and val (not (string=? val "")))58
val59
(let ((forgejo-url (fjo-base-url)))60
(cond61
((string-contains? forgejo-url "codeberg.org")62
"https://ci.codeberg.org")63
(else64
(let ((proto-end (string-find forgejo-url "://")))65
(if proto-end66
(string-append (substring forgejo-url 0 (+ proto-end 3))67
"ci."68
(substring forgejo-url (+ proto-end 3)69
(string-length forgejo-url)))70
(string-append "https://ci." forgejo-url)))))))))72
))