From 6e5d054bcdedf97018252405e437368bf3dd7971 Mon Sep 17 00:00:00 2001 From: Arunmozhi Date: Wed, 24 Jul 2024 16:54:56 +1000 Subject: [PATCH] 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 6c927d2c67..3d32d443cf 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 from tutor import interactive as interactive_config @@ -230,8 +234,32 @@ def patches_list(context: Context) -> None: renderer.print_patches_locations() +@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) config_command.add_command(patches_command) +config_command.add_command(edit)