Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add sopel-config edit subcommand #2595

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 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,29 @@ def handle_init(options):
return 0 # successful operation


def handle_edit(options):
Fixed Show fixed Hide fixed
"""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])
return 0 # successful operation


def handle_get(options):
"""Read the settings to display the value of ``<section> <key>``.

Expand Down Expand Up @@ -198,6 +229,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
Loading