From ecf0b53684914466df93664440d9cc2edcaad8d6 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 58e18334d9..8beb300371 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 @@ -220,8 +224,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)