From af7d62b66f5366b2b320e1b818a3408fc0a0cccb Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Wed, 24 Jul 2024 16:54:56 +1000 Subject: [PATCH 1/3] feat: adds `tutor config edit` Quickly launch the default an editor for editing the config.yml file. --- ...15534_arunmozhi_add_config_edit_command.md | 1 + tutor/commands/config.py | 34 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 changelog.d/20241114_115534_arunmozhi_add_config_edit_command.md diff --git a/changelog.d/20241114_115534_arunmozhi_add_config_edit_command.md b/changelog.d/20241114_115534_arunmozhi_add_config_edit_command.md new file mode 100644 index 0000000000..2dd8735829 --- /dev/null +++ b/changelog.d/20241114_115534_arunmozhi_add_config_edit_command.md @@ -0,0 +1 @@ +- [Feature] Adds `tutor config edit`. This opens the active tutor environment's config.yaml in an editor for manual editing. (by @tecoholic) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index 068b8702a6..60388b4500 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -1,15 +1,19 @@ from __future__ import annotations import json +import os.path +import subprocess import typing as t import click import click.shell_completion +from shutil import which + from tutor import config as tutor_config from tutor import env, exceptions, fmt, hooks from tutor import interactive as interactive_config -from tutor import serialize +from tutor import serialize, utils from tutor.commands.context import Context from tutor.commands.params import ConfigLoaderParam from tutor.types import Config, ConfigValue @@ -255,9 +259,37 @@ def patches_show(context: Context, name: str) -> None: print(rendered) +@click.command(name="edit", help="Edit config.yml of the current environment") +@click.pass_obj +def edit(context: Context, editor: str, save: bool) -> None: + config_file = tutor_config.config_path(context.root) + + if not os.path.isfile(config_file): + raise exceptions.TutorError( + f"Missing config file at {config_file}. Have you run 'tutor local launch' yet?" + ) + + open_cmd = [] + if which("open"): # MacOS & linux distributions that ship `open`. eg., Ubuntu + open_cmd = ["open", config_file] + elif which("xdg-open"): # Linux + open_cmd = ["xdg-open", config_file] + elif which("start"): # Windows + # Calling "start" on a regular file opens it with the default editor. + # The second argument "" just means "don't give the window a custom title". + open_cmd = ["start", '""', config_file] + else: + raise exceptions.TutorError( + f"Failed to find utility to launch an editor. Edit {config_file} with the editor of your choice." + ) + + utils.execute(*open_cmd) + + config_command.add_command(save) config_command.add_command(printroot) config_command.add_command(printvalue) patches_command.add_command(patches_list) patches_command.add_command(patches_show) config_command.add_command(patches_command) +config_command.add_command(edit) From b85d680ac105e083c8b498420b325c5611ff192d Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Fri, 6 Dec 2024 19:21:52 +1100 Subject: [PATCH 2/3] fix: remove params leftover during refactor Co-authored-by: Kyle McCormick --- tutor/commands/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index 60388b4500..5085669c17 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -261,7 +261,7 @@ def patches_show(context: Context, name: str) -> None: @click.command(name="edit", help="Edit config.yml of the current environment") @click.pass_obj -def edit(context: Context, editor: str, save: bool) -> None: +def edit(context: Context) -> None: config_file = tutor_config.config_path(context.root) if not os.path.isfile(config_file): From 748b339b5278d868bca7a0f85a797a70a589765e Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Fri, 6 Dec 2024 19:23:26 +1100 Subject: [PATCH 3/3] fix: remove unused import --- tutor/commands/config.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index 5085669c17..2b843c3095 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -2,7 +2,6 @@ import json import os.path -import subprocess import typing as t import click