From 80aec772a9ca863579530bfac7204ace16ef12c8 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Tue, 28 Jan 2020 14:32:03 -0700 Subject: [PATCH] update CLI with supported config file location --- cli/commands.py | 5 +-- cli/util.py | 34 +++++++++++++------ .../commands.yaml.example | 0 .../devices.yaml.example | 0 .../hyperglass.yaml.example | 0 5 files changed, 27 insertions(+), 12 deletions(-) rename {hyperglass/configuration => examples}/commands.yaml.example (100%) rename {hyperglass/configuration => examples}/devices.yaml.example (100%) rename {hyperglass/configuration => examples}/hyperglass.yaml.example (100%) diff --git a/cli/commands.py b/cli/commands.py index b182537..f204c94 100644 --- a/cli/commands.py +++ b/cli/commands.py @@ -84,9 +84,10 @@ def start(build): cls=HelpColorsCommand, help_options_custom_colors=random_colors(), ) -def migrateconfig(): +@click.option("-d", "--directory", required=True, help="Target directory") +def migrateconfig(directory): """Copy example configuration files to usable config files.""" - migrate_config(WORKING_DIR / "hyperglas/configuration/") + migrate_config(Path(directory)) @hg.command( diff --git a/cli/util.py b/cli/util.py index 5e1783b..adeca2c 100644 --- a/cli/util.py +++ b/cli/util.py @@ -1,4 +1,7 @@ """CLI utility functions.""" +# Standard Library Imports +from pathlib import Path + # Third Party Imports import click @@ -14,6 +17,8 @@ from cli.static import VALUE from cli.static import WS from cli.static import E +PROJECT_ROOT = Path(__file__).parent.parent + def async_command(func): """Decororator for to make async functions runable from syncronous code.""" @@ -107,24 +112,33 @@ def migrate_config(config_dir): """Copy example config files and remove .example extensions.""" status("Migrating example config files...") - import os - import glob import shutil - examples = glob.iglob(os.path.join(config_dir, "*.example")) + examples = Path(PROJECT_ROOT / "examples").glob("*.yaml.example") + if not isinstance(config_dir, Path): + config_dir = Path(config_dir) + + if not config_dir.exists(): + error(f"{str(config_dir)} does not exist") + + migrated = 0 for file in examples: - basefile, extension = os.path.splitext(file) + target_file = config_dir / file.with_suffix("").name try: - if os.path.exists(basefile): - info(f"{basefile} already exists") + if target_file.exists(): + info(f"{str(target_file)} already exists") else: - shutil.copyfile(file, basefile) - success(f"Migrated {basefile}") + shutil.copyfile(file, target_file) + migrated += 1 + info(f"Migrated {str(target_file)}") except Exception as e: - error(f"Failed to migrate {basefile}", e) + error(f"Failed to migrate {str(target_file)}", e) - success("Successfully migrated example config files") + if migrated == 0: + info(f"Migrated {migrated} example config files") + elif migrated > 0: + success(f"Successfully migrated {migrated} example config files") def migrate_systemd(source, destination): diff --git a/hyperglass/configuration/commands.yaml.example b/examples/commands.yaml.example similarity index 100% rename from hyperglass/configuration/commands.yaml.example rename to examples/commands.yaml.example diff --git a/hyperglass/configuration/devices.yaml.example b/examples/devices.yaml.example similarity index 100% rename from hyperglass/configuration/devices.yaml.example rename to examples/devices.yaml.example diff --git a/hyperglass/configuration/hyperglass.yaml.example b/examples/hyperglass.yaml.example similarity index 100% rename from hyperglass/configuration/hyperglass.yaml.example rename to examples/hyperglass.yaml.example