|
1 | 1 | import logging |
| 2 | +import os |
2 | 3 |
|
3 | 4 | import typer |
4 | | -from settings_library.utils_cli import create_settings_command, create_version_callback |
| 5 | +from settings_library.postgres import PostgresSettings |
| 6 | +from settings_library.rabbit import RabbitSettings |
| 7 | +from settings_library.utils_cli import ( |
| 8 | + create_settings_command, |
| 9 | + create_version_callback, |
| 10 | + print_as_envfile, |
| 11 | +) |
5 | 12 |
|
6 | 13 | from ._meta import APP_NAME, __version__ |
7 | 14 | from .core.settings import ApplicationSettings |
8 | 15 |
|
9 | 16 | log = logging.getLogger(__name__) |
10 | 17 |
|
11 | | -main = typer.Typer(name=APP_NAME) |
| 18 | +main = typer.Typer( |
| 19 | + name=APP_NAME, |
| 20 | + pretty_exceptions_enable=False, |
| 21 | + pretty_exceptions_show_locals=False, |
| 22 | +) |
12 | 23 |
|
13 | 24 | main.command()(create_settings_command(settings_cls=ApplicationSettings, logger=log)) |
14 | 25 | main.callback()(create_version_callback(__version__)) |
| 26 | + |
| 27 | + |
| 28 | +@main.command() |
| 29 | +def echo_dotenv(ctx: typer.Context, *, minimal: bool = True) -> None: |
| 30 | + """Generates and displays a valid environment variables file (also known as dot-envfile) |
| 31 | +
|
| 32 | + Usage: |
| 33 | + $ simcore-service echo-dotenv > .env |
| 34 | + $ cat .env |
| 35 | + $ set -o allexport; source .env; set +o allexport |
| 36 | + """ |
| 37 | + assert ctx # nosec |
| 38 | + |
| 39 | + # NOTE: we normally DO NOT USE `os.environ` to capture env vars but this is a special case |
| 40 | + # The idea here is to have a command that can generate a **valid** `.env` file that can be used |
| 41 | + # to initialized the app. For that reason we fill required fields of the `ApplicationSettings` with |
| 42 | + # "fake" but valid values (e.g. generating a password or adding tags as `replace-with-api-key). |
| 43 | + # Nonetheless, if the caller of this CLI has already some **valid** env vars in the environment we want to use them ... |
| 44 | + # and that is why we use `os.environ`. |
| 45 | + |
| 46 | + settings = ApplicationSettings.create_from_envs( |
| 47 | + SC_BOOT_MODE="default", |
| 48 | + NOTIFICATIONS_POSTGRES=os.environ.get( |
| 49 | + "NOTIFICATIONS_POSTGRES", |
| 50 | + PostgresSettings.create_from_envs( |
| 51 | + POSTGRES_HOST=os.environ.get( |
| 52 | + "POSTGRES_HOST", "replace-with-postgres-host" |
| 53 | + ), |
| 54 | + POSTGRES_USER=os.environ.get( |
| 55 | + "POSTGRES_USER", "replace-with-postgres-user" |
| 56 | + ), |
| 57 | + POSTGRES_DB=os.environ.get("POSTGRES_DB", "replace-with-postgres-db"), |
| 58 | + POSTGRES_PASSWORD=os.environ.get( |
| 59 | + "POSTGRES_PASSWORD", "replace-with-postgres-password" |
| 60 | + ), |
| 61 | + ), |
| 62 | + ), |
| 63 | + NOTIFICATIONS_RABBITMQ=os.environ.get( |
| 64 | + "NOTIFICATIONS_RABBITMQ", |
| 65 | + RabbitSettings.create_from_envs( |
| 66 | + RABBIT_HOST=os.environ.get("RABBIT_HOST", "replace-with-rabbit-host"), |
| 67 | + RABBIT_SECURE=os.environ.get("RABBIT_SECURE", "True"), |
| 68 | + RABBIT_USER=os.environ.get("RABBIT_USER", "replace-with-rabbit-user"), |
| 69 | + RABBIT_PASSWORD=os.environ.get( |
| 70 | + "RABBIT_PASSWORD", "replace-with-rabbit-password" |
| 71 | + ), |
| 72 | + ), |
| 73 | + ), |
| 74 | + ) |
| 75 | + |
| 76 | + print_as_envfile( |
| 77 | + settings, |
| 78 | + compact=False, |
| 79 | + verbose=True, |
| 80 | + show_secrets=True, |
| 81 | + exclude_unset=minimal, |
| 82 | + ) |
0 commit comments