From 0007364d9200aa572d63ac52b47daa11a94582ec Mon Sep 17 00:00:00 2001 From: dgw Date: Sun, 25 Feb 2024 00:10:01 -0600 Subject: [PATCH] cli: add `sopel-config edit` subcommand Opens the specified config file in the user's `$EDITOR`, obeying any environment variables like `$SOPEL_CONFIG_DIR`, as a convenience. --- sopel/cli/config.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/sopel/cli/config.py b/sopel/cli/config.py index 3e6c5f957..e1441880b 100644 --- a/sopel/cli/config.py +++ b/sopel/cli/config.py @@ -3,6 +3,7 @@ import argparse import os +import subprocess from . import utils @@ -50,6 +51,13 @@ def build_parser(): description='Initialize Sopel configuration file') utils.add_common_arguments(init_parser) + # sopel-config edit + edit_parser = subparsers.add_parser( + 'edit', + help='Open Sopel configuration file in your default text editor', + description='Open Sopel configuration file in your default text editor') + utils.add_common_arguments(edit_parser) + # sopel-config get
get_parser = subparsers.add_parser( 'get', @@ -150,6 +158,28 @@ def handle_init(options): return 0 # successful operation +def handle_edit(options): + """Open the specified config file in the user's ``$EDITOR``. + + :param options: parsed arguments + :type options: :class:`argparse.Namespace` + :return: 0 if everything went fine; + 1 if the specified config file doesn't exist; + 2 if the user doesn't have a valid ``$EDITOR`` + """ + editor = os.environ.get('EDITOR', None) + if editor is None: + print('No $EDITOR found. Please configure your shell.') + return 2 + + config_filename = utils.find_config(options.configdir, options.config) + if not os.path.isfile(config_filename): + print('No config file found at {}'.format(config_filename)) + return 1 + + subprocess.Popen([editor, config_filename]) + + def handle_get(options): """Read the settings to display the value of ``
``. @@ -198,6 +228,8 @@ def main(): return handle_list(options) elif action == 'init': return handle_init(options) + elif action == 'edit': + return handle_edit(options) elif action == 'get': return handle_get(options) except KeyboardInterrupt: