Skip to content

Commit

Permalink
feat: adds the -e and -s flags to the edit command
Browse files Browse the repository at this point in the history
  • Loading branch information
tecoholic committed Nov 14, 2024
1 parent c164596 commit 83478e8
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions tutor/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -270,24 +282,36 @@ 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]
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]

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)
Expand Down

0 comments on commit 83478e8

Please sign in to comment.