mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 07:55:45 +00:00
adding latest to CLI
This commit is contained in:
107
tembo/cli.py
107
tembo/cli.py
@@ -1,3 +1,5 @@
|
|||||||
|
import pathlib
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
import tembo
|
import tembo
|
||||||
@@ -22,9 +24,9 @@ def run():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@click.command(options_metavar="<options>")
|
@click.command(options_metavar="<options>", name="list")
|
||||||
def list_all():
|
def list_all():
|
||||||
"""List all names for 'tembo new <name>'."""
|
"""List all scopes defined in the config.yml"""
|
||||||
_all_scopes = [user_scope["name"] for user_scope in tembo.CONFIG.scopes]
|
_all_scopes = [user_scope["name"] for user_scope in tembo.CONFIG.scopes]
|
||||||
tembo.logger.info(
|
tembo.logger.info(
|
||||||
"%s names found in config.yml: '%s'", len(_all_scopes), "', '".join(_all_scopes)
|
"%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("--dry-run", is_flag=True, default=False)
|
||||||
@click.option("--example", is_flag=True, default=False)
|
@click.option("--example", is_flag=True, default=False)
|
||||||
def new(scope, inputs, dry_run, example):
|
def new(scope, inputs, dry_run, example):
|
||||||
"""
|
r"""
|
||||||
Create a new page.
|
Create a new page.
|
||||||
|
|
||||||
<scope> The name of the scope in the Tembo config.yml.
|
<scope>\n
|
||||||
|
The name of the scope in the config.yml.
|
||||||
|
|
||||||
<inputs> Any input tokens needed in the Tembo config.yml.
|
<inputs>\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
|
Example: tembo new meeting my_presentation
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# get the name from the tembo config.yml
|
# get the name from the tembo config.yml
|
||||||
try:
|
_cli_verify_name_exists(scope)
|
||||||
_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
|
|
||||||
|
|
||||||
# get the scope information from the tembo config.yml
|
# get the scope information from the tembo config.yml
|
||||||
config_scope = {}
|
config_scope = {}
|
||||||
@@ -112,50 +110,61 @@ def new(scope, inputs, dry_run, example):
|
|||||||
template_filename=config_scope["template_filename"],
|
template_filename=config_scope["template_filename"],
|
||||||
template_path=tembo.CONFIG.template_path,
|
template_path=tembo.CONFIG.template_path,
|
||||||
)
|
)
|
||||||
if _name_found:
|
try:
|
||||||
try:
|
scoped_page = pages.ScopedPageCreator(page_creator_options).create_page()
|
||||||
scoped_page = pages.ScopedPageCreator(page_creator_options).create_page()
|
except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error:
|
||||||
except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error:
|
tembo.logger.critical(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
|
||||||
raise SystemExit(1) from base_path_does_not_exist_error
|
except exceptions.TemplateFileNotFoundError as template_file_not_found_error:
|
||||||
except exceptions.TemplateFileNotFoundError as template_file_not_found_error:
|
tembo.logger.critical(template_file_not_found_error.args[0])
|
||||||
tembo.logger.critical(template_file_not_found_error.args[0])
|
raise SystemExit(1) from template_file_not_found_error
|
||||||
raise SystemExit(1) from template_file_not_found_error
|
except exceptions.MismatchedTokenError as mismatched_token_error:
|
||||||
except exceptions.MismatchedTokenError as mismatched_token_error:
|
if config_scope["example"] is not None:
|
||||||
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
|
|
||||||
tembo.logger.critical(
|
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.expected,
|
||||||
mismatched_token_error.given,
|
mismatched_token_error.given,
|
||||||
|
config_scope["example"],
|
||||||
)
|
)
|
||||||
raise SystemExit(1) from mismatched_token_error
|
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:
|
if dry_run:
|
||||||
click.echo(cli_message(f"{scoped_page.path} will be created"))
|
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."))
|
|
||||||
raise SystemExit(0)
|
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
|
# raise error if no config.yml found
|
||||||
tembo.logger.critical(
|
if pathlib.Path(tembo.CONFIG.config_path).exists():
|
||||||
"No config.yml found in %s - exiting", tembo.CONFIG.config_path
|
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)
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
@@ -169,7 +178,7 @@ run.add_command(list_all)
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# new(["meeting", "robs presentation", "meeting on gcp"])
|
# new(["meeting", "robs presentation", "meeting on gcp"])
|
||||||
new(["meeting", "a", "b", "c", "d", "e"])
|
new(["meeting", "a", "b", "c", "d"])
|
||||||
# new(["meeting", "robs presentation"])
|
# new(["meeting", "robs presentation"])
|
||||||
|
|
||||||
# pyinstaller
|
# pyinstaller
|
||||||
|
|||||||
Reference in New Issue
Block a user