diff --git a/tutor/commands/config.py b/tutor/commands/config.py index f26e018010..75021330fa 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -260,8 +260,20 @@ def patches_show(context: Context, name: str) -> None: @click.command(name="edit", help="Edit config.yml of the current environment") +@click.option( + "-e", + "--editor", + default="", + help="Editor to open the config.yaml file.", +) +@click.option( + "-s", + "--save", + is_flag=True, + help="Update the tutor environment after editing. Equivalent to `tutor config save`.", +) @click.pass_obj -def edit(context: Context) -> None: +def edit(context: Context, editor: str, save: bool) -> None: config_file = tutor_config.config_path(context.root) if not os.path.isfile(config_file): @@ -270,8 +282,9 @@ def edit(context: Context) -> None: ) open_cmd = None - - if which("open"): # MacOS & linux distributions that ship `open`. eg., Ubuntu + if editor: + open_cmd = [editor, config_file] + elif 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] @@ -279,15 +292,26 @@ def edit(context: Context) -> None: # 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] + + utils.execute(*open_cmd) + + if save: + config = tutor_config.load_full(context.root) + env.save(context.root, config) else: - raise exceptions.TutorError( - f"Failed to find utility to launch an editor. Edit {config_file} with the editor of your choice." + click.echo( + click.style( + "Run 'tutor config save' to update the tutor environment with the latest config.", + fg="yellow", + ) + ) + click.echo( + click.style( + "💡 You can automatically update the environment after editing by setting the flag -s.\n" + "e.g., `tutor config edit -s`", + fg="blue", + ) ) - - utils.execute(open_cmd) - click.echo( - "Remember to run 'tutor config save' after editing to apply the changes to your environment." - ) config_command.add_command(save)