|
| 1 | +import os |
1 | 2 | import cmd |
2 | 3 | import pprint |
3 | 4 | import argparse |
4 | 5 | import operator |
| 6 | +import subprocess |
5 | 7 | from inspect import cleandoc |
6 | 8 | from pathlib import Path |
7 | 9 |
|
8 | 10 | import yaml |
| 11 | +from rich.console import Console |
9 | 12 |
|
10 | 13 | from firewheel.config import Config |
11 | 14 | from firewheel.lib.log import Log |
@@ -64,6 +67,64 @@ def define_reset_parser(self) -> argparse.ArgumentParser: |
64 | 67 | ) |
65 | 68 | return parser |
66 | 69 |
|
| 70 | + def define_edit_parser(self) -> argparse.ArgumentParser: |
| 71 | + """Create an :py:class:`argparse.ArgumentParser` for :ref:`command_config_edit`. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + argparse.ArgumentParser: The parser needed for :ref:`command_config_edit`. |
| 75 | + """ |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description=str( |
| 78 | + "Edit the FIREWHEEL configuration with a text editor. " |
| 79 | + "The user must set either the VISUAL or EDITOR environment variable " |
| 80 | + "or use the provided flag to override these environment variables." |
| 81 | + ), |
| 82 | + prog="firewheel config edit", |
| 83 | + add_help=False, |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + "-e", |
| 87 | + "--editor", |
| 88 | + required=False, |
| 89 | + default="", |
| 90 | + help="Use the specified text editor.", |
| 91 | + ) |
| 92 | + return parser |
| 93 | + |
| 94 | + def do_edit(self, args: str = "") -> None: |
| 95 | + """ |
| 96 | + Edit the FIREWHEEL config with the default text editor as determined by |
| 97 | + the ``VISUAL`` or ``EDITOR`` environment variables. |
| 98 | + If no editor is found an error message is output. |
| 99 | +
|
| 100 | + Args: |
| 101 | + args (str): A string of arguments passed in by the user. |
| 102 | + """ |
| 103 | + # Create a Console object for colored output |
| 104 | + console = Console() |
| 105 | + |
| 106 | + # Get the parser for the reset command |
| 107 | + parser = self.define_edit_parser() |
| 108 | + cmd_args = self._handle_parsing(parser, args) |
| 109 | + |
| 110 | + # Check for VISUAL, then EDITOR |
| 111 | + editor = cmd_args.editor or os.environ.get("VISUAL") or os.environ.get("EDITOR") |
| 112 | + |
| 113 | + if not editor: |
| 114 | + self.help_edit() |
| 115 | + return |
| 116 | + |
| 117 | + # Attempt to open the file with the chosen editor |
| 118 | + try: |
| 119 | + subprocess.run([editor, Config(writable=True).config_path], check=True) |
| 120 | + except (FileNotFoundError, subprocess.CalledProcessError): |
| 121 | + console.print( |
| 122 | + f"Error: Failed to open FIREWHEEL configuration with '{editor}'.\n", |
| 123 | + style="bold red", |
| 124 | + ) |
| 125 | + self.help_edit() |
| 126 | + return |
| 127 | + |
67 | 128 | def do_reset(self, args: str = "") -> None: |
68 | 129 | """ |
69 | 130 | Reset the FIREWHEEL configuration to the defaults. |
@@ -262,6 +323,18 @@ def get_docs(self) -> str: |
262 | 323 | command_string += "\n\n" |
263 | 324 | return command_string |
264 | 325 |
|
| 326 | + def _help_edit(self) -> str: |
| 327 | + """Help message for the :py:meth:`do_edit` sub-command. |
| 328 | +
|
| 329 | + Returns: |
| 330 | + str: The help message. |
| 331 | + """ |
| 332 | + return self.define_edit_parser().format_help() |
| 333 | + |
| 334 | + def help_edit(self) -> None: |
| 335 | + """Print help for the :py:meth:`do_edit` sub-command.""" |
| 336 | + print(self._help_edit()) |
| 337 | + |
265 | 338 | def _help_reset(self) -> str: |
266 | 339 | """Help message for the :py:meth:`do_reset` sub-command. |
267 | 340 |
|
|
0 commit comments