1
0
Fork 0
mirror of synced 2024-12-22 14:11:07 -05:00

Merge branch 'etkeys/feature/expose-cli-options-for-plugins'

This commit is contained in:
Anish Athalye 2021-02-25 07:57:19 -05:00
commit 5f1e33ed67
4 changed files with 51 additions and 9 deletions

View file

@ -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')

View file

@ -1,14 +1,16 @@
import copy
import os
from argparse import Namespace
class Context(object):
'''
Contextual data and information for plugins.
'''
def __init__(self, base_directory):
def __init__(self, base_directory, options=Namespace()):
self._base_directory = base_directory
self._defaults = {}
self._options = options
pass
def set_base_directory(self, base_directory):
@ -25,3 +27,6 @@ class Context(object):
def defaults(self):
return copy.deepcopy(self._defaults)
def options(self):
return copy.deepcopy(self._options)

View file

@ -1,22 +1,23 @@
import os
from argparse import Namespace
from .plugin import Plugin
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=Namespace()):
self._log = Messenger()
self._setup_context(base_directory)
self._setup_context(base_directory, options)
self._load_plugins()
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

View file

@ -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 <<EOF
import dotbot
import os.path
@ -17,12 +17,48 @@ class Test(dotbot.Plugin):
EOF
'
test_expect_success 'run' '
test_expect_success 'run 1' '
run_dotbot --plugin ${DOTFILES}/test.py <<EOF
- test: ~
EOF
'
test_expect_success 'test' '
test_expect_success 'test 1' '
grep "it works" ~/flag
'
test_expect_success 'setup 2' '
rm ${DOTFILES}/test.py;
cat > ${DOTFILES}/test.py <<EOF
import dotbot
import os.path
class Test(dotbot.Plugin):
def can_handle(self, directive):
return directive == "test"
def handle(self, directive, data):
self._log.debug("Attempting to get options from Context")
options = self._context.options()
if len(options.plugins) != 1:
self._log.debug("Context.options.plugins length is %i, expected 1" % len(options.plugins))
return False
if not options.plugins[0].endswith("test.py"):
self._log.debug("Context.options.plugins[0] is %s, expected end with test.py" % options.plugins[0])
return False
with open(os.path.expanduser("~/flag"), "w") as f:
f.write("it works")
return True
EOF
'
test_expect_success 'run 2' '
run_dotbot --plugin ${DOTFILES}/test.py <<EOF
- test: ~
EOF
'
test_expect_success 'test 2' '
grep "it works" ~/flag
'