""" Resolve deploy-time tokens in deployed files. Values come from config/environments.yml and config/services_registry.yml -- the single source of truth. Nothing here knows what '-DEV' is. render_subs.py --env dev --service tune /path/to/ioc /path/to/systemd render_subs.py --env prod /sls/bd/hla/prod/qt Without --service only the environment tokens are available, so a file that needs a per-service token (ioc_port) fails loudly rather than shipping the placeholder. A path that does not exist is reported and skipped, which is how services without a systemd unit are handled. """ import argparse import sys from pathlib import Path from agebd.subs import SubstitutionError, Substitutions def render(subs, paths): """ Render every path in place. Returns the files that changed. """ changed = [] for path in paths: if not path.exists(): print(f"skipped (does not exist): {path}") continue if path.is_dir(): changed.extend(subs.render_tree(path)) elif subs.render_file(path): changed.append(path) return changed def main(argv=None): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument("--env", required=True, help="Deployment environment, e.g. dev") parser.add_argument( "--service", default=None, help="Service name. Required for files holding per-service tokens.", ) parser.add_argument("paths", nargs="+", type=Path, help="Files or directories to render") args = parser.parse_args(argv) if args.service: subs = Substitutions.for_service(args.env, args.service) else: subs = Substitutions.for_env(args.env) changed = render(subs, args.paths) for path in changed: print(f"rendered: {path}") print(f"{len(changed)} file(s) rendered for env '{args.env}'") return 0 if __name__ == "__main__": try: sys.exit(main()) except SubstitutionError as e: print(f"error: {e}", file=sys.stderr) sys.exit(1)