From 23eead307a5af56f73eea649921a0ebeb89defeb Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Mon, 1 Nov 2021 19:37:12 +0000 Subject: [PATCH] adding latest to CLI --- tembo/cli.py | 107 ++++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 49 deletions(-) diff --git a/tembo/cli.py b/tembo/cli.py index fcab713..bf38f2b 100644 --- a/tembo/cli.py +++ b/tembo/cli.py @@ -1,3 +1,5 @@ +import pathlib + import click import tembo @@ -22,9 +24,9 @@ def run(): """ -@click.command(options_metavar="") +@click.command(options_metavar="", name="list") def list_all(): - """List all names for 'tembo new '.""" + """List all scopes defined in the config.yml""" _all_scopes = [user_scope["name"] for user_scope in tembo.CONFIG.scopes] tembo.logger.info( "%s names found in config.yml: '%s'", len(_all_scopes), "', '".join(_all_scopes) @@ -42,25 +44,21 @@ def list_all(): @click.option("--dry-run", is_flag=True, default=False) @click.option("--example", is_flag=True, default=False) def new(scope, inputs, dry_run, example): - """ + r""" Create a new page. - The name of the scope in the Tembo config.yml. + \n + The name of the scope in the config.yml. - Any input tokens needed in the Tembo config.yml. + \n + Any input token values that are defined in the config.yml for this scope. + Accepts multiple inputs separated by a space. Example: tembo new meeting my_presentation """ # get the name from the tembo config.yml - try: - _name_found = scope in [ - user_scope["name"] for user_scope in tembo.CONFIG.scopes - ] - except TypeError as type_error: - # raise error if no scopes are defined - tembo.logger.critical("No scopes found in config.yml - exiting") - raise SystemExit(1) from type_error + _cli_verify_name_exists(scope) # get the scope information from the tembo config.yml config_scope = {} @@ -112,50 +110,61 @@ def new(scope, inputs, dry_run, example): template_filename=config_scope["template_filename"], template_path=tembo.CONFIG.template_path, ) - if _name_found: - try: - scoped_page = pages.ScopedPageCreator(page_creator_options).create_page() - except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error: - tembo.logger.critical(base_path_does_not_exist_error) - raise SystemExit(1) from base_path_does_not_exist_error - except exceptions.TemplateFileNotFoundError as template_file_not_found_error: - tembo.logger.critical(template_file_not_found_error.args[0]) - raise SystemExit(1) from template_file_not_found_error - except exceptions.MismatchedTokenError as mismatched_token_error: - if config_scope["example"] is not None: - tembo.logger.critical( - "Your tembo config.yml/template specifies %s input tokens, you gave %s. Example: %s", - mismatched_token_error.expected, - mismatched_token_error.given, - config_scope["example"], - ) - raise SystemExit(1) from mismatched_token_error + try: + scoped_page = pages.ScopedPageCreator(page_creator_options).create_page() + except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error: + tembo.logger.critical(base_path_does_not_exist_error) + raise SystemExit(1) from base_path_does_not_exist_error + except exceptions.TemplateFileNotFoundError as template_file_not_found_error: + tembo.logger.critical(template_file_not_found_error.args[0]) + raise SystemExit(1) from template_file_not_found_error + except exceptions.MismatchedTokenError as mismatched_token_error: + if config_scope["example"] is not None: tembo.logger.critical( - "Your tembo config.yml/template specifies %s input tokens, you gave %s", + "Your tembo config.yml/template specifies %s input tokens, you gave %s. Example: %s", mismatched_token_error.expected, mismatched_token_error.given, + config_scope["example"], ) raise SystemExit(1) from mismatched_token_error + tembo.logger.critical( + "Your tembo config.yml/template specifies %s input tokens, you gave %s", + mismatched_token_error.expected, + mismatched_token_error.given, + ) + raise SystemExit(1) from mismatched_token_error - if dry_run: - click.echo(cli_message(f"{scoped_page.path} will be created")) - raise SystemExit(0) - - try: - scoped_page.save_to_disk() - raise SystemExit(0) - except exceptions.ScopedPageAlreadyExists as scoped_page_already_exists: - cli_message(f"File {scoped_page_already_exists}") - raise SystemExit(0) from scoped_page_already_exists - if not _name_found and len(tembo.CONFIG.scopes) > 0: - # if the name is missing in the config.yml, raise error - click.echo(cli_message(f"Command {scope} not found in config.yml.")) + if dry_run: + click.echo(cli_message(f"{scoped_page.path} will be created")) raise SystemExit(0) + try: + scoped_page.save_to_disk() + raise SystemExit(0) + except exceptions.ScopedPageAlreadyExists as scoped_page_already_exists: + cli_message(f"File {scoped_page_already_exists}") + raise SystemExit(0) from scoped_page_already_exists + + +def _cli_verify_name_exists(scope: str) -> None: + _name_found = scope in [ + user_scope["name"] for user_scope in tembo.CONFIG.scopes + ] + if _name_found: + return + if len(tembo.CONFIG.scopes) > 0: + # if the name is missing in the config.yml, raise error + cli_message(f"Command {scope} not found in config.yml.") + raise SystemExit(0) # raise error if no config.yml found - tembo.logger.critical( - "No config.yml found in %s - exiting", tembo.CONFIG.config_path - ) + if pathlib.Path(tembo.CONFIG.config_path).exists(): + tembo.logger.critical( + "Config.yml found in %s is empty - exiting", tembo.CONFIG.config_path + ) + else: + tembo.logger.critical( + "No config.yml found in %s - exiting", tembo.CONFIG.config_path + ) raise SystemExit(1) @@ -169,7 +178,7 @@ run.add_command(list_all) if __name__ == "__main__": # new(["meeting", "robs presentation", "meeting on gcp"]) - new(["meeting", "a", "b", "c", "d", "e"]) + new(["meeting", "a", "b", "c", "d"]) # new(["meeting", "robs presentation"]) # pyinstaller