Skip to content

Commit 22ad075

Browse files
committed
Support backwards compatability
1 parent f83957a commit 22ad075

1 file changed

Lines changed: 64 additions & 21 deletions

File tree

src/blueapi/cli/cli.py

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,63 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
6666
return wrapper
6767

6868

69-
def _load_config(ctx: click.Context, config: Path | None | tuple[Path, ...]) -> None:
69+
def _default_config(ctx: click.Context) -> None:
70+
ctx.ensure_object(dict)
71+
config_loader = ConfigLoader(ApplicationConfig)
72+
73+
loaded_config: ApplicationConfig = config_loader.load()
74+
75+
set_up_logging(loaded_config.logging)
76+
77+
ctx.obj["config"] = loaded_config
78+
79+
80+
def _load_config(
81+
ctx: click.Context,
82+
config: Path | None | tuple[Path, ...],
83+
) -> None:
84+
ctx.ensure_object(dict)
85+
7086
config_loader = ConfigLoader(ApplicationConfig)
87+
ctx.obj["custom_config"] = False
7188

7289
if config is not None:
90+
ctx.obj["custom_config"] = True
7391
configs = (config,) if isinstance(config, Path) else config
7492
for path in configs:
7593
if path.exists():
7694
config_loader.use_values_from_yaml(path)
7795
else:
7896
raise FileNotFoundError(f"Cannot find file: {path}")
7997

80-
ctx.ensure_object(dict)
8198
loaded_config: ApplicationConfig = config_loader.load()
82-
8399
set_up_logging(loaded_config.logging)
84-
85100
ctx.obj["config"] = loaded_config
86101

87102

88103
@click.group(
89104
invoke_without_command=True, context_settings={"auto_envvar_prefix": "BLUEAPI"}
90105
)
91106
@click.version_option(version=__version__, prog_name="blueapi")
107+
@click.option(
108+
"-c",
109+
"--config",
110+
type=Path,
111+
help="Path to configuration YAML file",
112+
multiple=True,
113+
)
92114
@click.pass_context
93-
def main(ctx: click.Context) -> None:
115+
def main(ctx: click.Context, config: Path | None | tuple[Path, ...]) -> None:
94116
# if no command is supplied, run with the options passed
95117

96118
# Set umask to DLS standard
97119
os.umask(stat.S_IWOTH)
98120

121+
if config == ():
122+
config = None
123+
124+
_load_config(ctx, config)
125+
99126
if ctx.invoked_subcommand is None:
100127
print("Please invoke subcommand!")
101128

@@ -157,14 +184,10 @@ def config_schema(output: Path | None = None, update: bool = False) -> None:
157184

158185

159186
@main.command(name="serve")
160-
@click.option(
161-
"-c", "--config", type=Path, help="Path to configuration YAML file", multiple=True
162-
)
163187
@click.pass_context
164-
def start_application(ctx: click.Context, config: Path | None | tuple[Path, ...]):
188+
def start_application(ctx: click.Context):
165189
"""Run a worker that accepts plans to run"""
166-
_load_config(ctx, config)
167-
loaded_config: ApplicationConfig = ctx.obj["config"]
190+
config: ApplicationConfig = ctx.obj["config"]
168191

169192
"""Only import the service functions when starting the service or generating
170193
the schema, not the controller as a new FastAPI app will be started each time.
@@ -176,7 +199,7 @@ def start_application(ctx: click.Context, config: Path | None | tuple[Path, ...]
176199
observability context.
177200
"""
178201
setup_tracing("BlueAPI", OTLP_EXPORT_ENABLED)
179-
start(loaded_config)
202+
start(config)
180203

181204

182205
@main.command(name="login")
@@ -195,9 +218,16 @@ def login(
195218
"""
196219
Authenticate with the blueapi using the OIDC (OpenID Connect) flow.
197220
"""
198-
config: ApplicationConfig = ConfigLoader(ApplicationConfig).load()
221+
config: ApplicationConfig = obj["config"]
222+
199223
if url is not None:
200-
config.api.url = HttpUrl(url)
224+
if obj["custom_config"] is True:
225+
logging.warning(
226+
"Custom config has been used. This will take precidence "
227+
"over a provided url"
228+
)
229+
else:
230+
config.api.url = HttpUrl(url)
201231
try:
202232
auth: SessionManager = SessionManager.from_cache(config.auth_token_path)
203233
access_token = auth.get_valid_access_token()
@@ -230,9 +260,16 @@ def logout(
230260
"""
231261
Logs out from the OIDC provider and removes the cached access token.
232262
"""
233-
config: ApplicationConfig = ConfigLoader(ApplicationConfig).load()
263+
config: ApplicationConfig = obj["config"]
264+
234265
if url is not None:
235-
config.api.url = HttpUrl(url)
266+
if obj["custom_config"] is True:
267+
logging.warning(
268+
"Custom config has been used. This will take precidence "
269+
"over a provided url"
270+
)
271+
else:
272+
config.api.url = HttpUrl(url)
236273
try:
237274
auth: SessionManager = SessionManager.from_cache(config.auth_token_path)
238275
auth.logout()
@@ -276,15 +313,21 @@ def controller(
276313
ctx.ensure_object(dict)
277314
ctx.obj["fmt"] = OutputFormat(output)
278315

279-
config: ApplicationConfig = ConfigLoader(ApplicationConfig).load()
316+
config: ApplicationConfig = ctx.obj["config"]
280317

281318
if url is not None:
282-
config.api.url = HttpUrl(url)
319+
if ctx.obj["custom_config"] is True:
320+
logging.warning(
321+
"Custom config has been used. This will take precidence "
322+
"over a provided url"
323+
)
324+
else:
325+
config.api.url = HttpUrl(url)
283326

284-
tmp_client = BlueapiClient.from_config(config)
285-
config.stomp = tmp_client.get_stomp_config()
327+
tmp_client = BlueapiClient.from_config(config)
328+
config.stomp = tmp_client.get_stomp_config()
329+
ctx.obj["config"] = config
286330

287-
ctx.obj["config"] = config
288331
set_up_logging(config.logging)
289332
ctx.obj["client"] = BlueapiClient.from_config(config)
290333

0 commit comments

Comments
 (0)