Null-DRM-Official/apps/pkg/modcfg/modcfg.go
404errordeveloper f9c28be3f1
All checks were successful
release / build-and-upload (push) Successful in 6m8s
modules-release / build-and-upload (push) Successful in 4m47s
v0.1.1: external modules, publish YAML, proxy reset, binary-first README
- Host discovers/builds modules under modules/ (bbc, rte, tg4)
- module-publish.yaml tracked; local module.yaml gitignored
- drm --modules-dir / DRM_MODULES_DIR
- drm proxy reset; Magisk reinject reliability
- modules-v* Forgejo release workflow + build scripts
- BBC full Explore channel map including S4C
- README quickstart based on release binaries
2026-10-08 23:16:14 +02:00

124 lines
3.4 KiB
Go

// Package modcfg loads an app module's values from modules/<name>/,
// with environment-variable overrides.
//
// Load order:
// 1. module-publish.yaml (tracked universal template) if present
// 2. module.yaml (local / release copy; may add secrets) overlays on top
//
// Local module.yaml is gitignored and never published. Release zips ship
// module-publish.yaml renamed to module.yaml.
package modcfg
import (
"fmt"
"os"
"path/filepath"
"strings"
"drmdecryption/modhost"
"drmdecryption/repo"
"gopkg.in/yaml.v3"
)
// Dir returns the directory holding a module's values.
// Prefers the active modules dir (see modhost / --modules-dir); falls back to
// apps/modules/<name>/ for older checkouts.
func Dir(name string) string {
preferred := filepath.Join(modhost.ModulesDir(""), name)
if st, err := os.Stat(preferred); err == nil && st.IsDir() {
return preferred
}
if modhost.ModulesDirOverride() != "" {
return preferred
}
root := repo.Root()
if _, err := os.Stat(filepath.Join(root, "modules")); err == nil {
return filepath.Join(root, "modules", name)
}
return filepath.Join(root, "apps", "modules", name)
}
// Path returns the preferred values file path (module.yaml).
func Path(name string) string {
return filepath.Join(Dir(name), "module.yaml")
}
// PublishPath is the tracked universal template.
func PublishPath(name string) string {
return filepath.Join(Dir(name), "module-publish.yaml")
}
// Result reports where a module's values came from, for `drm modules`.
type Result struct {
Path string
Loaded bool
}
// Load reads module-publish.yaml then overlays module.yaml into dst.
// Missing files are not an error — the module runs on Go defaults plus flags/env.
func Load(name string, dst any) (Result, error) {
dir := Dir(name)
publish := filepath.Join(dir, "module-publish.yaml")
local := filepath.Join(dir, "module.yaml")
res := Result{Path: local}
loadedAny := false
if b, err := os.ReadFile(publish); err == nil {
if err := yaml.Unmarshal(b, dst); err != nil {
return res, fmt.Errorf("%s: %w", publish, err)
}
loadedAny = true
res.Path = publish
} else if !os.IsNotExist(err) {
return res, err
}
if b, err := os.ReadFile(local); err == nil {
if err := yaml.Unmarshal(b, dst); err != nil {
return res, fmt.Errorf("%s: %w", local, err)
}
loadedAny = true
res.Path = local
} else if !os.IsNotExist(err) {
return res, err
}
res.Loaded = loadedAny
return res, nil
}
// EnvKey is the environment variable a field override is read from:
// TG4_POLICY_KEY for module "tg4", field "policy_key".
func EnvKey(module, field string) string {
clean := func(s string) string {
s = strings.ToUpper(s)
s = strings.ReplaceAll(s, "-", "_")
s = strings.ReplaceAll(s, ".", "_")
return s
}
return clean(module) + "_" + clean(field)
}
// Env returns the environment override for a field, or "".
func Env(module, field string) string {
return strings.TrimSpace(os.Getenv(EnvKey(module, field)))
}
// Override picks the first non-empty of: flag value, environment, current value.
func Override(module, field, flagVal, current string) string {
if strings.TrimSpace(flagVal) != "" {
return flagVal
}
if v := Env(module, field); v != "" {
return v
}
return current
}
// Resolve makes a module-relative path absolute against the repo root.
func Resolve(p string) string {
if p == "" || filepath.IsAbs(p) {
return p
}
return filepath.Join(repo.Root(), p)
}