Skip to content

Commit

Permalink
feat: clean up env directory before save
Browse files Browse the repository at this point in the history
This will add a `-c` `--clean` flag to the save command and ensure that the env directory is deleted if it exists.

Close overhangio#967
  • Loading branch information
CodeWithEmad committed Jun 29, 2024
1 parent dce1138 commit 8218202
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/20240629_120748_codewithemad_cleanup_flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- 💥[Feature] Enhance your workflow with the new `-c` or `--clean` option for the `tutor config save` command! This feature allows you to clean your environment before each save, ensuring that all files and directories within the `env/` folder are deleted, providing you with a fresh environment each time. (by @CodeWithEmad)
5 changes: 5 additions & 0 deletions tests/commands/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def test_config_save(self) -> None:
self.assertFalse(result.exception)
self.assertEqual(0, result.exit_code)

def test_config_save_cleanup_env_dir(self) -> None:
result = self.invoke(["config", "save", "-c"])
self.assertFalse(result.exception)
self.assertEqual(0, result.exit_code)

def test_config_save_interactive(self) -> None:
result = self.invoke(["config", "save", "-i"])
self.assertFalse(result.exception)
Expand Down
10 changes: 10 additions & 0 deletions tutor/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ def _candidate_config_items(self) -> t.Iterable[tuple[str, ConfigValue]]:
@click.option(
"-e", "--env-only", "env_only", is_flag=True, help="Skip updating config.yml"
)
@click.option(
"-c",
"--clean",
"clean_env",
is_flag=True,
help="Remove everything in the env directory before save",
)
@click.pass_obj
def save(
context: Context,
Expand All @@ -145,10 +152,13 @@ def save(
remove_vars: list[tuple[str, t.Any]],
unset_vars: list[str],
env_only: bool,
clean_env: bool,
) -> None:
config = tutor_config.load_minimal(context.root)
if interactive:
interactive_config.ask_questions(config)
if clean_env:
env.delete_env_dir(context.root)
if set_vars:
for key, value in set_vars:
config[key] = env.render_unknown(config, value)
Expand Down
13 changes: 13 additions & 0 deletions tutor/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,19 @@ def root_dir(root: str) -> str:
return os.path.abspath(root)


def delete_env_dir(root: str) -> None:
env_path = base_dir(root)

if os.path.exists(env_path):
try:
shutil.rmtree(env_path)
fmt.echo_alert("All files in the env directory are deleted.")
except PermissionError:
raise exceptions.TutorError(
"Could not delete the env directory. Permission Denied."
)


@hooks.Actions.PLUGIN_UNLOADED.add()
def _delete_plugin_templates(plugin: str, root: str, _config: Config) -> None:
"""
Expand Down

0 comments on commit 8218202

Please sign in to comment.