1
0
Fork 0
mirror of synced 2024-11-17 05:55:34 -05:00

adding a exit on failure flag

This commit is contained in:
Subho Banerjee 2021-09-12 19:32:25 -05:00
parent 076939da8e
commit 4d2e1537f4
No known key found for this signature in database
GPG key ID: 02AF2E69BA7B1C8B
3 changed files with 38 additions and 3 deletions

View file

@ -42,6 +42,8 @@ def add_options(parser):
help='disable color output')
parser.add_argument('--version', action='store_true',
help='show program\'s version number and exit')
parser.add_argument('-x', '--exit-on-failure', dest='exit_on_failure', action='store_true',
help='exit after first failed directive')
def read_config(config_file):
reader = ConfigReader(config_file)
@ -107,7 +109,8 @@ def main():
# default to directory of config file
base_directory = os.path.dirname(os.path.abspath(options.config_file))
os.chdir(base_directory)
dispatcher = Dispatcher(base_directory, only=options.only, skip=options.skip, options=options)
dispatcher = Dispatcher(base_directory, only=options.only, skip=options.skip,
exit_on_failure=options.exit_on_failure, options=options)
success = dispatcher.dispatch(tasks)
if success:
log.info('\n==> All tasks executed successfully')

View file

@ -5,12 +5,14 @@ from .messenger import Messenger
from .context import Context
class Dispatcher(object):
def __init__(self, base_directory, only=None, skip=None, options=Namespace()):
def __init__(self, base_directory, only=None, skip=None, exit_on_failure=False,
options=Namespace()):
self._log = Messenger()
self._setup_context(base_directory, options)
self._load_plugins()
self._only = only
self._skip = skip
self._exit = exit_on_failure
def _setup_context(self, base_directory, options):
path = os.path.abspath(
@ -36,16 +38,27 @@ class Dispatcher(object):
for plugin in self._plugins:
if plugin.can_handle(action):
try:
success &= plugin.handle(action, task[action])
local_success = plugin.handle(action, task[action])
if not local_success and self._exit:
# The action has failed exit
self._log.error('Action %s failed' % action)
return False
success &= local_success
handled = True
except Exception as err:
self._log.error(
'An error was encountered while executing action %s' %
action)
self._log.debug(err)
if self._exit:
# There was an execption exit
return False
if not handled:
success = False
self._log.error('Action %s not handled' % action)
if self._exit:
# Invalid action exit
return False
return success
def _load_plugins(self):

View file

@ -0,0 +1,19 @@
test_description='test exit on failure'
. '../test-lib.bash'
test_expect_success 'setup' '
echo "apple" > ${DOTFILES}/f
'
test_expect_failure 'run' '
run_dotbot -x <<EOF
- shell:
- "this_is_not_a_command"
- link:
~/f:
EOF
'
test_expect_success 'test' '
[[ ! -f ~/f ]]
'