From ae994cc7c348a1c59027ac2073756b6b36f504de Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Wed, 24 Jul 2024 16:54:56 +1000 Subject: [PATCH 1/5] feat: adds `tutor config edit` Quickly launch the default YAML editor for editing the config.yml file. --- tutor/commands/config.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index 068b8702a6..0fa7725d1c 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -1,11 +1,15 @@ 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 @@ -255,9 +259,33 @@ 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) -> None: + config_file = os.path.join(context.root, "config.yml") + + open_cmd = None + + if which("open"): # MacOS + open_cmd = ["open", config_file] + elif which("xdg-open"): # Linux + open_cmd = ["xdg-open", config_file] + elif which("start"): # Windows + open_cmd = ["start", '""', config_file] + else: + click.echo( + "Cannot find a way to open the editor automatically. Kindly open the file manually: " + + config_file + ) + return + + subprocess.call(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 2b0021126d14b87ca46f05f18064126fcfc3c456 Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Mon, 12 Aug 2024 11:05:41 +1000 Subject: [PATCH 2/5] refactor: improve error handling --- tutor/commands/config.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index 0fa7725d1c..01d86eae64 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -262,22 +262,21 @@ 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) -> None: - config_file = os.path.join(context.root, "config.yml") + 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}") open_cmd = None - if which("open"): # MacOS + 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 open_cmd = ["start", '""', config_file] else: - click.echo( - "Cannot find a way to open the editor automatically. Kindly open the file manually: " - + config_file - ) - return + raise exceptions.TutorError(f"Failed to find utility to launch an editor.") subprocess.call(open_cmd) From ed59d221abd2ab993b1f3bcf195d70579a5172a1 Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Sun, 10 Nov 2024 16:14:45 +1100 Subject: [PATCH 3/5] docs: add comment to explain windows command Co-authored-by: Kyle McCormick --- tutor/commands/config.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index 01d86eae64..9fd8e3cd44 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -274,6 +274,8 @@ def edit(context: Context) -> None: 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.") From 6f6eaa29a8d2fa7dd12760da9fb6f4cc9ac8c8ac Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Sun, 10 Nov 2024 16:15:37 +1100 Subject: [PATCH 4/5] refactor: improve missing config exception error message 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 9fd8e3cd44..0017ae3b21 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -265,7 +265,7 @@ def edit(context: Context) -> 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}") + raise exceptions.TutorError(f"Missing config file at {config_file}. Have you run 'tutor local launch' yet?") open_cmd = None From 00565bbf6b28c6f0acc44a56a3e3163528fcafea Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Sun, 10 Nov 2024 16:33:54 +1100 Subject: [PATCH 5/5] refactor: add message at the end of command --- tutor/commands/config.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index 0017ae3b21..3fd4e68337 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -265,7 +265,9 @@ def edit(context: Context) -> 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?") + raise exceptions.TutorError( + f"Missing config file at {config_file}. Have you run 'tutor local launch' yet?" + ) open_cmd = None @@ -278,9 +280,14 @@ def edit(context: Context) -> None: # 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.") + raise exceptions.TutorError( + f"Failed to find utility to launch an editor. Edit {config_file} with the editor of your choice." + ) subprocess.call(open_cmd) + click.echo( + "Remember to run 'tutor config save' after editing, to apply the changes to your environment." + ) config_command.add_command(save)