Skip to content

Commit

Permalink
cli: add sopel-config edit subcommand
Browse files Browse the repository at this point in the history
Opens the specified config file in the user's `$EDITOR`, obeying any
environment variables like `$SOPEL_CONFIG_DIR`, as a convenience.
  • Loading branch information
dgw committed Feb 25, 2024
1 parent 973a489 commit 0007364
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions sopel/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import argparse
import os
import subprocess

from . import utils

Expand Down Expand Up @@ -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 <section> <key>
get_parser = subparsers.add_parser(
'get',
Expand Down Expand Up @@ -150,6 +158,28 @@ def handle_init(options):
return 0 # successful operation


def handle_edit(options):

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
"""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 ``<section> <key>``.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0007364

Please sign in to comment.