Initial test CLI implementation

master
Tom Wilson 6 years ago
parent 510744e3f0
commit d22d3ad8a7

@ -42,6 +42,8 @@ def cli(ctx, default_config_path, local_operation, only_default_layer):
in the current working directory beginning with "shepherd" and in the current working directory beginning with "shepherd" and
ending with ".toml" will be used. ending with ".toml" will be used.
""" """
ctx.ensure_object(SimpleNamespace)
version_text = pkg_resources.get_distribution("shepherd") version_text = pkg_resources.get_distribution("shepherd")
log.info(F"Shepherd Agent [{version_text}]") log.info(F"Shepherd Agent [{version_text}]")
@ -56,8 +58,9 @@ def cli(ctx, default_config_path, local_operation, only_default_layer):
with open(default_config_path, 'r+') as f: with open(default_config_path, 'r+') as f:
content = f.read() content = f.read()
if "Compiled Shepherd config" in content: if "Compiled Shepherd config" in content:
log.warn("Default config file looks like it is full compiled config file" log.warning("Default config file looks like it is full compiled config"
" generated by Shepherd and picked up due to accidental name match") " file generated by Shepherd and picked up due to accidental"
" name match")
else: else:
log.error("No default config file provided, and no 'shepherd*.toml' could be" log.error("No default config file provided, and no 'shepherd*.toml' could be"
" found in the current directory") " found in the current directory")
@ -79,6 +82,11 @@ def cli(ctx, default_config_path, local_operation, only_default_layer):
del plugin_configs["shepherd"] del plugin_configs["shepherd"]
core_config = confman.get_config_bundle("shepherd") core_config = confman.get_config_bundle("shepherd")
if ctx.invoked_subcommand == "test":
ctx.obj.plugin_configs = plugin_configs
ctx.obj.core_config = core_config
return
# control.init_control(core_config, plugin_configs) # control.init_control(core_config, plugin_configs)
scheduler.init_scheduler(core_config) scheduler.init_scheduler(core_config)
@ -98,12 +106,37 @@ def cli(ctx, default_config_path, local_operation, only_default_layer):
@cli.command() @cli.command()
@click.option('-d', '--plugin-dir', type=click.Path(), @click.argument('plugin_name', required=False)
help="Override the configured directory to search for plugin modules, in addition to" @click.argument('interface_function', required=False)
" built in Shepherd plugins and the global import path." @click.pass_context
" Supplying the option empty will use the current directory.") def test(ctx, plugin_name, interface_function):
def test(): if not plugin_name:
print("test!") click.echo("")
click.echo(click.style("===", fg='white', bold=True) +
click.style('Shepherd Test', fg='blue', bold=True) +
click.style("===", fg='white', bold=True))
click.secho(" Plugins loaded:", fg='green')
if len(ctx.obj.plugin_configs) == 0:
click.echo("---none---")
for plugin_name, config in ctx.obj.plugin_configs.items():
click.secho(plugin_name, bold=True)
click.secho("\n Loaded core config:", fg='green')
pprint(ctx.obj.core_config)
click.secho("\n Loaded plugin configs:", fg='green')
if len(ctx.obj.plugin_configs) == 0:
click.echo("---none---")
for plugin_name, config in ctx.obj.plugin_configs.items():
click.secho(plugin_name, bold=True)
pprint(config)
click.echo("")
log.info("Initialising plugins...")
plugin.init_plugins(ctx.obj.plugin_configs, ctx.obj.core_config)
return
@cli.command() @cli.command()
@ -197,8 +230,8 @@ def compile_config(confman, default_config_path, layers_disabled):
log.info(F"Loaded default config layer from {default_config_path}") log.info(F"Loaded default config layer from {default_config_path}")
except Exception as e: except Exception as e:
if isinstance(e, InvalidConfigError): if isinstance(e, InvalidConfigError):
log.error( log.error(F"Failed to load default config from {default_config_path}."
F"Failed to load default config from {default_config_path}. {chr(10).join(e.args)}") F" {chr(10).join(e.args)}")
else: else:
log.error(F"Failed to load default config from {default_config_path}", exc_info=True) log.error(F"Failed to load default config from {default_config_path}", exc_info=True)
sys.exit(1) sys.exit(1)
@ -216,7 +249,7 @@ def compile_config(confman, default_config_path, layers_disabled):
confman.save_fallback() confman.save_fallback()
if not core_conf["plugin_dir"]: if not core_conf["plugin_dir"]:
log.warn("Custom plugin path is empty, won't load custom plugins") log.warning("Custom plugin path is empty, won't load custom plugins")
# ====Custom Local Config Layer==== # ====Custom Local Config Layer====
# If this fails, maintain default config but continue on to Control layer # If this fails, maintain default config but continue on to Control layer
@ -295,9 +328,9 @@ def core_confspec():
("custom_config_path", StringSpec(optional=True, helptext="Path to custom config" ("custom_config_path", StringSpec(optional=True, helptext="Path to custom config"
" layer TOML file.")), " layer TOML file.")),
("compiled_config_path", StringSpec(default="compiled-config.toml", optional=True, ("compiled_config_path", StringSpec(default="compiled-config.toml", optional=True,
helptext="Path to custom file Shepherd will generate" helptext="Path to custom file Shepherd will generate"
" to show compiled config that was used and any" " to show compiled config that was used and any"
" errors in validation.")) " errors in validation."))
]) ])
confspec.add_spec("control_server", StringSpec()) confspec.add_spec("control_server", StringSpec())
@ -317,16 +350,16 @@ def resolve_core_conf_paths(core_conf, relative_dir):
os.chdir(relative_dir) os.chdir(relative_dir)
core_conf["root_dir"] = str(Path(core_conf["root_dir"]).expanduser().resolve()) core_conf["root_dir"] = str(Path(core_conf["root_dir"]).expanduser().resolve())
try: try:
os.chdir(core_conf["root_dir"]) os.chdir(core_conf["root_dir"])
except FileNotFoundError: except FileNotFoundError:
raise FileNotFoundError(F"Shepherd root operating directory '{core_conf['root_dir']}'" raise FileNotFoundError(F"Shepherd root operating directory '{core_conf['root_dir']}'"
F" does not exist") F" does not exist")
if core_conf["plugin_dir"]: if core_conf["plugin_dir"]:
core_conf["plugin_dir"] = str(Path(core_conf["plugin_dir"]).expanduser().resolve()) core_conf["plugin_dir"] = str(Path(core_conf["plugin_dir"]).expanduser().resolve())
if core_conf["custom_config_path"]: if core_conf["custom_config_path"]:
core_conf["custom_config_path"] = str( core_conf["custom_config_path"] = str(
Path(core_conf["custom_config_path"]).expanduser().resolve()) Path(core_conf["custom_config_path"]).expanduser().resolve())
if core_conf["compiled_config_path"]: if core_conf["compiled_config_path"]:
core_conf["compiled_config_path"] = str( core_conf["compiled_config_path"] = str(
Path(core_conf["compiled_config_path"]).expanduser().resolve()) Path(core_conf["compiled_config_path"]).expanduser().resolve())

Loading…
Cancel
Save