[docs]classCliContextObj:def__init__(self,ctx:click.Context,logger:logging.Logger,global_opts:GlobalCommandLineOptions,)->None:self._runtime_ctx:RuntimeContext|None=Noneself.ctx=ctxself.logger=loggerself.global_opts=global_opts@propertydefruntime_ctx(self)->RuntimeContext:""" Lazy load the runtime context. This is done to avoid configuration loading when the command is not run. This is useful for commands like `--help` and `--version` """ifself._runtime_ctxisNone:self._runtime_ctx=self._init_runtime_ctx()returnself._runtime_ctxdef_init_runtime_ctx(self)->RuntimeContext:config_path=Path(self.global_opts.config_file)conf_file_exists=config_path.exists()was_conf_file_user_provided=bool(self.ctx.get_parameter_source("config_file")notin(ParameterSource.DEFAULT,ParameterSource.DEFAULT_MAP,))try:ifwas_conf_file_user_providedandnotconf_file_exists:raiseFileNotFoundError(f"File {self.global_opts.config_file} does not exist")config_obj=({}ifnotconf_file_existselseload_raw_config_file(config_path))ifnotconfig_obj:self.logger.info("configuration empty, falling back to default configuration")raw_config=RawConfig.model_validate(config_obj)runtime=RuntimeContext.from_raw_config(raw_config,global_cli_options=self.global_opts,)exceptNotAReleaseBranchasexc:rprint(f"[bold {'red'ifself.global_opts.strictelse'orange1'}]{exc!s}")# If not strict, exit 0 so other processes can continue. For example, in# multibranch CI it might be desirable to run a non-release branch's pipeline# without specifying conditional execution of PSR based on branch nameself.ctx.exit(2ifself.global_opts.strictelse0)exceptFileNotFoundErrorasexc:click.echo(str(exc),err=True)self.ctx.exit(2)except(ValidationError,InvalidConfiguration,InvalidGitRepositoryError,)asexc:click.echo(str(exc),err=True)self.ctx.exit(1)# This allows us to mask secrets in the logging# by applying it to all the configured handlersforhandlerinlogging.getLogger().handlers:handler.addFilter(runtime.masker)returnruntime