From 0ba45763d938c26b81029a8493e26864314da7bd Mon Sep 17 00:00:00 2001 From: "E. Keys" Date: Wed, 2 Dec 2020 18:49:36 -0500 Subject: [PATCH] Make parsed cli options available to plugins - `Context` now provides a copy of the parsed cli options so that they can be used by plugins. - Modified `Dispatcher` to pass parsed cli options to `Context` Resolves #249 --- dotbot/cli.py | 2 +- dotbot/context.py | 8 +++++++- dotbot/dispatcher.py | 16 +++++++++------ test/tests/plugin.bash | 45 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/dotbot/cli.py b/dotbot/cli.py index 32db016..1a986f5 100644 --- a/dotbot/cli.py +++ b/dotbot/cli.py @@ -97,7 +97,7 @@ 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) + dispatcher = Dispatcher(base_directory, only=options.only, skip=options.skip, options=options) success = dispatcher.dispatch(tasks) if success: log.info('\n==> All tasks executed successfully') diff --git a/dotbot/context.py b/dotbot/context.py index 8c42d47..9820757 100644 --- a/dotbot/context.py +++ b/dotbot/context.py @@ -6,9 +6,10 @@ class Context(object): Contextual data and information for plugins. ''' - def __init__(self, base_directory): + def __init__(self, base_directory, options): self._base_directory = base_directory self._defaults = {} + self._options = options pass def set_base_directory(self, base_directory): @@ -25,3 +26,8 @@ class Context(object): def defaults(self): return copy.deepcopy(self._defaults) + + def options(self): + if self._options is not None: + return copy.deepcopy(self._options) + return None diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py index 646f6a0..fa38092 100644 --- a/dotbot/dispatcher.py +++ b/dotbot/dispatcher.py @@ -4,19 +4,23 @@ from .messenger import Messenger from .context import Context class Dispatcher(object): - def __init__(self, base_directory, only=None, skip=None): + def __init__(self, base_directory, only=None, skip=None, options=None): self._log = Messenger() - self._setup_context(base_directory) + self._setup_context(base_directory, options) self._load_plugins() - self._only = only - self._skip = skip + if options is not None: + self._only = options.only + self._skip = options.skip + else: + self._only = only + self._skip = skip - def _setup_context(self, base_directory): + def _setup_context(self, base_directory, options): path = os.path.abspath( os.path.expanduser(base_directory)) if not os.path.exists(path): raise DispatchError('Nonexistent base directory') - self._context = Context(path) + self._context = Context(path, options) def dispatch(self, tasks): success = True diff --git a/test/tests/plugin.bash b/test/tests/plugin.bash index bdf0c7f..47e348d 100644 --- a/test/tests/plugin.bash +++ b/test/tests/plugin.bash @@ -1,7 +1,7 @@ test_description='plugin loading works' . '../test-lib.bash' -test_expect_success 'setup' ' +test_expect_success 'setup 1' ' cat > ${DOTFILES}/test.py < ${DOTFILES}/test.py <