mirror of
1
0
Fork 0

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
This commit is contained in:
E. Keys 2020-12-02 22:49:17 -05:00 committed by Anish Athalye
parent 5f1e33ed67
commit c35382c06d
3 changed files with 101 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import os, glob import os, glob
import sys import sys
from argparse import ArgumentParser from argparse import ArgumentParser, RawTextHelpFormatter
from .config import ConfigReader, ReadingError from .config import ConfigReader, ReadingError
from .dispatcher import Dispatcher, DispatchError from .dispatcher import Dispatcher, DispatchError
from .messenger import Messenger from .messenger import Messenger
@ -16,8 +16,10 @@ def add_options(parser):
help='suppress almost all output') help='suppress almost all output')
parser.add_argument('-q', '--quiet', action='store_true', parser.add_argument('-q', '--quiet', action='store_true',
help='suppress most output') help='suppress most output')
parser.add_argument('-v', '--verbose', action='store_true', parser.add_argument('-v', '--verbose', action='count', default=0,
help='enable verbose output') help='enable verbose output\n'
'-v: typical verbose\n'
'-vv: also, set shell commands stderr/stdout to true')
parser.add_argument('-d', '--base-directory', parser.add_argument('-d', '--base-directory',
help='execute commands from within BASEDIR', help='execute commands from within BASEDIR',
metavar='BASEDIR') metavar='BASEDIR')
@ -47,7 +49,7 @@ def read_config(config_file):
def main(): def main():
log = Messenger() log = Messenger()
try: try:
parser = ArgumentParser() parser = ArgumentParser(formatter_class=RawTextHelpFormatter)
add_options(parser) add_options(parser)
options = parser.parse_args() options = parser.parse_args()
if options.version: if options.version:
@ -57,7 +59,7 @@ def main():
log.set_level(Level.WARNING) log.set_level(Level.WARNING)
if options.quiet: if options.quiet:
log.set_level(Level.INFO) log.set_level(Level.INFO)
if options.verbose: if options.verbose > 0:
log.set_level(Level.DEBUG) log.set_level(Level.DEBUG)
if options.force_color and options.no_color: if options.force_color and options.no_color:

View File

@ -10,6 +10,7 @@ class Shell(dotbot.Plugin):
''' '''
_directive = 'shell' _directive = 'shell'
_has_shown_override_message = False
def can_handle(self, directive): def can_handle(self, directive):
return directive == self._directive return directive == self._directive
@ -23,6 +24,7 @@ class Shell(dotbot.Plugin):
def _process_commands(self, data): def _process_commands(self, data):
success = True success = True
defaults = self._context.defaults().get('shell', {}) defaults = self._context.defaults().get('shell', {})
options = self._get_option_overrides()
for item in data: for item in data:
stdin = defaults.get('stdin', False) stdin = defaults.get('stdin', False)
stdout = defaults.get('stdout', False) stdout = defaults.get('stdout', False)
@ -47,6 +49,8 @@ class Shell(dotbot.Plugin):
self._log.lowinfo('%s' % msg) self._log.lowinfo('%s' % msg)
else: else:
self._log.lowinfo('%s [%s]' % (msg, cmd)) self._log.lowinfo('%s [%s]' % (msg, cmd))
stdout = options.get('stdout', stdout)
stderr = options.get('stderr', stderr)
ret = dotbot.util.shell_command( ret = dotbot.util.shell_command(
cmd, cmd,
cwd=self._context.base_directory(), cwd=self._context.base_directory(),
@ -62,3 +66,14 @@ class Shell(dotbot.Plugin):
else: else:
self._log.error('Some commands were not successfully executed') self._log.error('Some commands were not successfully executed')
return success 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

View File

@ -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")) <<EOF
- shell:
-
command: echo apple
EOF
'
test_expect_success 'run 2' '
(run_dotbot -vv | (grep "^apple")) <<EOF
- shell:
-
command: echo apple
stdout: false
EOF
'
test_expect_success 'run 3' '
(run_dotbot -vv | (grep "^apple")) <<EOF
- defaults:
shell:
stdout: false
- shell:
- command: echo apple
EOF
'
# Control to make sure stderr redirection is working as expected
test_expect_failure 'run 4' '
(run_dotbot -vv | (grep "^apple")) <<EOF
- shell:
- command: echo apple >&2
EOF
'
test_expect_success 'run 5' '
(run_dotbot -vv 2>&1 | (grep "^apple")) <<EOF
- shell:
- command: echo apple >&2
EOF
'
test_expect_success 'run 6' '
(run_dotbot -vv 2>&1 | (grep "^apple")) <<EOF
- shell:
-
command: echo apple >&2
stdout: false
EOF
'
test_expect_success 'run 7' '
(run_dotbot -vv 2>&1 | (grep "^apple")) <<EOF
- defaults:
shell:
stdout: false
- shell:
- command: echo 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")) <<EOF
- shell:
- command: echo apple
EOF
'
test_expect_failure 'run 9' '
(run_dotbot -v | (grep "^apple")) <<EOF
- shell:
- command: echo apple >&2
EOF
'