Skip to content

Commit 1db424c

Browse files
authored
Merge branch 'develop' into main
2 parents 3a44c07 + 76b2b65 commit 1db424c

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

aperturedb/cli/configure.py

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def default(self, o):
2828
app = typer.Typer()
2929

3030

31-
def _config_file_path(as_global: bool):
31+
def _config_file_path(as_global: bool) -> Path:
3232
config_path: Path = Path(os.path.join(
3333
os.getcwd(),
3434
f".{APP_NAME}",
@@ -39,6 +39,11 @@ def _config_file_path(as_global: bool):
3939
return config_path
4040

4141

42+
def _write_config(config_path: Path, config: dict):
43+
with open(config_path.as_posix(), "w") as config_file:
44+
config_file.write(json.dumps(config, indent=2, cls=ObjEncoder))
45+
46+
4247
def has_environment_configuration():
4348
for known_variable in ["APERTUREDB_KEY", "APERTUREDB_JSON"]:
4449
if (data := os.environ.get(known_variable)) is not None and data != "":
@@ -237,8 +242,7 @@ def check_for_overwrite(name):
237242
else:
238243
configs["active"] = ac
239244

240-
with open(config_path.as_posix(), "w") as config_file:
241-
config_file.write(json.dumps(configs, indent=2, cls=ObjEncoder))
245+
_write_config(config_path, configs)
242246

243247

244248
@app.command()
@@ -266,8 +270,61 @@ def activate(
266270
check_configured(as_global=False) or \
267271
check_configured(as_global=True, show_error=True)
268272

269-
with open(config_path.as_posix(), "w") as config_file:
270-
config_file.write(json.dumps(configs, indent=2, cls=ObjEncoder))
273+
_write_config(config_path, configs)
274+
275+
276+
@app.command()
277+
def remove(
278+
name: Annotated[Optional[str], typer.Argument(
279+
help="Name of this configuration to remove")],
280+
remove_if_active: Annotated[bool, typer.Option(
281+
help="If true; if active, remove and assign other configuration to be active; If false refuse to delete if active")] = False,
282+
new_active: Annotated[str, typer.Option(
283+
help="If deleting active, use name as new active")] = None,
284+
as_global: Annotated[bool, typer.Option(
285+
help="Project level vs global level")] = True):
286+
"""
287+
Remove a configuration from a Configuriation file
288+
"""
289+
config_path = _config_file_path(as_global)
290+
configs = {}
291+
config_level = "global" if as_global else "project"
292+
try:
293+
configs, ac = get_configurations(config_path.as_posix())
294+
except FileNotFoundError as e:
295+
console.log(
296+
f"No configuration available at {config_level} level.")
297+
raise typer.Exit(code=2)
298+
except json.JSONDecodeError:
299+
console.log(
300+
f"Configuration file at {config_level} level was malformed.")
301+
raise typer.Exit(code=2)
302+
303+
if not name in configs:
304+
console.log(
305+
f"Configuration file at {config_level} level does not have a config with the name {name}.")
306+
raise typer.Exit(code=2)
307+
308+
change_active = False
309+
if name == ac:
310+
change_active = True
311+
if new_active is None and not remove_if_active:
312+
console.log(
313+
f"Configuration {name} is active and no options for removing active were supplied.")
314+
raise typer.Exit(code=2)
315+
if new_active is not None and not new_active in configs:
316+
console.log(
317+
f"Configuration {new_active} does not exist in Configuration file at {config_level} and cannot be set active")
318+
raise typer.Exit(code=2)
319+
320+
del configs[name]
321+
if change_active:
322+
if new_active:
323+
ac = new_active
324+
else:
325+
ac = next(iter(configs))
326+
configs["active"] = ac
327+
_write_config(config_path, configs)
271328

272329

273330
@app.command()
@@ -309,9 +366,7 @@ def get_key(name: Annotated[str, typer.Argument(
309366

310367
user_key = keys.generate_user_key(conn, key_user)
311368
configs[name].add_user_key(key_user, user_key)
312-
with open(config_path.as_posix(), "w") as config_file:
313-
config_file.write(json.dumps(
314-
configs, indent=2, cls=ObjEncoder))
369+
_write_config(config_path, configs)
315370
except FileNotFoundError:
316371
check_configured(as_global=False) or \
317372
check_configured(as_global=True, show_error=True)

0 commit comments

Comments
 (0)