From c35382c06d65cb5f86c0898ce2fdc9e59bf882f2 Mon Sep 17 00:00:00 2001 From: "E. Keys" Date: Wed, 2 Dec 2020 22:49:17 -0500 Subject: [PATCH] Add cli option force shell show stderr/stdout Passing `--verbose` flag two times will now force shell commands to show stderr/stdout output regardless of settings in config file. Resolves #104 --- dotbot/cli.py | 12 ++-- dotbot/plugins/shell.py | 15 +++++ test/tests/shell-cli-override-config.bash | 79 +++++++++++++++++++++++ 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 test/tests/shell-cli-override-config.bash diff --git a/dotbot/cli.py b/dotbot/cli.py index 1a986f5..f9fba73 100644 --- a/dotbot/cli.py +++ b/dotbot/cli.py @@ -1,7 +1,7 @@ import os, glob import sys -from argparse import ArgumentParser +from argparse import ArgumentParser, RawTextHelpFormatter from .config import ConfigReader, ReadingError from .dispatcher import Dispatcher, DispatchError from .messenger import Messenger @@ -16,8 +16,10 @@ def add_options(parser): help='suppress almost all output') parser.add_argument('-q', '--quiet', action='store_true', help='suppress most output') - parser.add_argument('-v', '--verbose', action='store_true', - help='enable verbose output') + parser.add_argument('-v', '--verbose', action='count', default=0, + help='enable verbose output\n' + '-v: typical verbose\n' + '-vv: also, set shell commands stderr/stdout to true') parser.add_argument('-d', '--base-directory', help='execute commands from within BASEDIR', metavar='BASEDIR') @@ -47,7 +49,7 @@ def read_config(config_file): def main(): log = Messenger() try: - parser = ArgumentParser() + parser = ArgumentParser(formatter_class=RawTextHelpFormatter) add_options(parser) options = parser.parse_args() if options.version: @@ -57,7 +59,7 @@ def main(): log.set_level(Level.WARNING) if options.quiet: log.set_level(Level.INFO) - if options.verbose: + if options.verbose > 0: log.set_level(Level.DEBUG) if options.force_color and options.no_color: diff --git a/dotbot/plugins/shell.py b/dotbot/plugins/shell.py index 3092f20..bae8b99 100644 --- a/dotbot/plugins/shell.py +++ b/dotbot/plugins/shell.py @@ -10,6 +10,7 @@ class Shell(dotbot.Plugin): ''' _directive = 'shell' + _has_shown_override_message = False def can_handle(self, directive): return directive == self._directive @@ -23,6 +24,7 @@ class Shell(dotbot.Plugin): def _process_commands(self, data): success = True defaults = self._context.defaults().get('shell', {}) + options = self._get_option_overrides() for item in data: stdin = defaults.get('stdin', False) stdout = defaults.get('stdout', False) @@ -47,6 +49,8 @@ class Shell(dotbot.Plugin): self._log.lowinfo('%s' % msg) else: self._log.lowinfo('%s [%s]' % (msg, cmd)) + stdout = options.get('stdout', stdout) + stderr = options.get('stderr', stderr) ret = dotbot.util.shell_command( cmd, cwd=self._context.base_directory(), @@ -62,3 +66,14 @@ class Shell(dotbot.Plugin): else: self._log.error('Some commands were not successfully executed') return success + + def _get_option_overrides(self): + ret = {} + options = self._context.options() + if options.verbose > 1: + ret['stderr'] = True + ret['stdout'] = True + if not self._has_shown_override_message: + self._log.debug("Shell: Found cli option to force show stderr and stdout.") + self._has_shown_override_message = True + return ret diff --git a/test/tests/shell-cli-override-config.bash b/test/tests/shell-cli-override-config.bash new file mode 100644 index 0000000..908c10d --- /dev/null +++ b/test/tests/shell-cli-override-config.bash @@ -0,0 +1,79 @@ +test_description='cli options can override config file' +. '../test-lib.bash' + +test_expect_success 'run 1' ' +(run_dotbot -vv | (grep "^apple")) <&2 +EOF +' + +test_expect_success 'run 5' ' +(run_dotbot -vv 2>&1 | (grep "^apple")) <&2 +EOF +' + +test_expect_success 'run 6' ' +(run_dotbot -vv 2>&1 | (grep "^apple")) <&2 + stdout: false +EOF +' + +test_expect_success 'run 7' ' +(run_dotbot -vv 2>&1 | (grep "^apple")) <&2 +EOF +' + +# Make sure that we must use verbose level 2 +# This preserves backwards compatability +test_expect_failure 'run 8' ' +(run_dotbot -v | (grep "^apple")) <&2 +EOF +'