Merge branch 'ssbanerje/master'
This commit is contained in:
commit
fb18c9b112
3 changed files with 51 additions and 3 deletions
|
@ -42,6 +42,8 @@ def add_options(parser):
|
||||||
help='disable color output')
|
help='disable color output')
|
||||||
parser.add_argument('--version', action='store_true',
|
parser.add_argument('--version', action='store_true',
|
||||||
help='show program\'s version number and exit')
|
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):
|
def read_config(config_file):
|
||||||
reader = ConfigReader(config_file)
|
reader = ConfigReader(config_file)
|
||||||
|
@ -107,7 +109,8 @@ def main():
|
||||||
# default to directory of config file
|
# default to directory of config file
|
||||||
base_directory = os.path.dirname(os.path.abspath(options.config_file))
|
base_directory = os.path.dirname(os.path.abspath(options.config_file))
|
||||||
os.chdir(base_directory)
|
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)
|
success = dispatcher.dispatch(tasks)
|
||||||
if success:
|
if success:
|
||||||
log.info('\n==> All tasks executed successfully')
|
log.info('\n==> All tasks executed successfully')
|
||||||
|
|
|
@ -5,12 +5,14 @@ from .messenger import Messenger
|
||||||
from .context import Context
|
from .context import Context
|
||||||
|
|
||||||
class Dispatcher(object):
|
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._log = Messenger()
|
||||||
self._setup_context(base_directory, options)
|
self._setup_context(base_directory, options)
|
||||||
self._load_plugins()
|
self._load_plugins()
|
||||||
self._only = only
|
self._only = only
|
||||||
self._skip = skip
|
self._skip = skip
|
||||||
|
self._exit = exit_on_failure
|
||||||
|
|
||||||
def _setup_context(self, base_directory, options):
|
def _setup_context(self, base_directory, options):
|
||||||
path = os.path.abspath(
|
path = os.path.abspath(
|
||||||
|
@ -36,16 +38,27 @@ class Dispatcher(object):
|
||||||
for plugin in self._plugins:
|
for plugin in self._plugins:
|
||||||
if plugin.can_handle(action):
|
if plugin.can_handle(action):
|
||||||
try:
|
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
|
handled = True
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
self._log.error(
|
self._log.error(
|
||||||
'An error was encountered while executing action %s' %
|
'An error was encountered while executing action %s' %
|
||||||
action)
|
action)
|
||||||
self._log.debug(err)
|
self._log.debug(err)
|
||||||
|
if self._exit:
|
||||||
|
# There was an execption exit
|
||||||
|
return False
|
||||||
if not handled:
|
if not handled:
|
||||||
success = False
|
success = False
|
||||||
self._log.error('Action %s not handled' % action)
|
self._log.error('Action %s not handled' % action)
|
||||||
|
if self._exit:
|
||||||
|
# Invalid action exit
|
||||||
|
return False
|
||||||
return success
|
return success
|
||||||
|
|
||||||
def _load_plugins(self):
|
def _load_plugins(self):
|
||||||
|
|
32
test/tests/exit-on-failure.bash
Normal file
32
test/tests/exit-on-failure.bash
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
test_description='test exit on failure'
|
||||||
|
. '../test-lib.bash'
|
||||||
|
|
||||||
|
test_expect_success 'setup' '
|
||||||
|
echo "apple" > ${DOTFILES}/f1 &&
|
||||||
|
echo "orange" > ${DOTFILES}/f2 &&
|
||||||
|
echo "pineapple" > ${DOTFILES}/f3
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_failure 'run_case1' '
|
||||||
|
run_dotbot -x <<EOF
|
||||||
|
- shell:
|
||||||
|
- "this_is_not_a_command"
|
||||||
|
- link:
|
||||||
|
~/f1:
|
||||||
|
EOF
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_failure 'run_case2' '
|
||||||
|
run_dotbot -x <<EOF
|
||||||
|
- link:
|
||||||
|
~/f2:
|
||||||
|
- shell:
|
||||||
|
- "this_is_not_a_command"
|
||||||
|
- link:
|
||||||
|
~/f3:
|
||||||
|
EOF
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'test' '
|
||||||
|
[[ ! -f ~/f1 ]] && [[ -f ~/f2 ]] && [[ ! -f ~/f3 ]]
|
||||||
|
'
|
Loading…
Reference in a new issue