Skip to content

Commit b790a55

Browse files
committed
added dotenv
1 parent 4c6e707 commit b790a55

File tree

1 file changed

+69
-1
lines changed
  • services/storage/src/simcore_service_storage

1 file changed

+69
-1
lines changed

services/storage/src/simcore_service_storage/cli.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import logging
2+
import os
23

34
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.s3 import S3Settings
7+
from settings_library.utils_cli import (
8+
create_settings_command,
9+
create_version_callback,
10+
print_as_envfile,
11+
)
512

613
from ._meta import PROJECT_NAME, __version__
714
from .core.settings import ApplicationSettings
@@ -27,3 +34,64 @@ def run():
2734
f"$ uvicorn {PROJECT_NAME}.main:the_app",
2835
fg=typer.colors.BLUE,
2936
)
37+
38+
39+
@main.command()
40+
def echo_dotenv(ctx: typer.Context, *, minimal: bool = True) -> None:
41+
"""Generates and displays a valid environment variables file (also known as dot-envfile)
42+
43+
Usage:
44+
$ simcore-service echo-dotenv > .env
45+
$ cat .env
46+
$ set -o allexport; source .env; set +o allexport
47+
"""
48+
assert ctx # nosec
49+
50+
# NOTE: we normally DO NOT USE `os.environ` to capture env vars but this is a special case
51+
# The idea here is to have a command that can generate a **valid** `.env` file that can be used
52+
# to initialized the app. For that reason we fill required fields of the `ApplicationSettings` with
53+
# "fake" but valid values (e.g. generating a password or adding tags as `replace-with-api-key).
54+
# Nonetheless, if the caller of this CLI has already some **valid** env vars in the environment we want to use them ...
55+
# and that is why we use `os.environ`.
56+
57+
settings = ApplicationSettings.create_from_envs(
58+
STORAGE_POSTGRES=os.environ.get(
59+
"STORAGE_POSTGRES",
60+
PostgresSettings.create_from_envs(
61+
POSTGRES_HOST=os.environ.get(
62+
"POSTGRES_HOST", "replace-with-postgres-host"
63+
),
64+
POSTGRES_USER=os.environ.get(
65+
"POSTGRES_USER", "replace-with-postgres-user"
66+
),
67+
POSTGRES_DB=os.environ.get("POSTGRES_DB", "replace-with-postgres-db"),
68+
POSTGRES_PASSWORD=os.environ.get(
69+
"POSTGRES_PASSWORD", "replace-with-postgres-password"
70+
),
71+
),
72+
),
73+
STORAGE_S3=os.environ.get( # nosec
74+
"STORAGE_S3",
75+
S3Settings.create_from_envs(
76+
S3_BUCKET_NAME=os.environ.get("S3_BUCKET", "replace-with-s3-bucket"),
77+
S3_ACCESS_KEY=os.environ.get(
78+
"S3_ACCESS_KEY", "replace-with-s3-access-key"
79+
),
80+
S3_SECRET_KEY=os.environ.get(
81+
"S3_SECRET_KEY", "replace-with-s3-secret-key"
82+
),
83+
S3_ENDPOINT=os.environ.get(
84+
"S3_ENDPOINT", "https://s3.replace-with-s3-endpoint"
85+
),
86+
S3_REGION=os.environ.get("S3_REGION", "replace-with-s3-region"),
87+
),
88+
),
89+
)
90+
91+
print_as_envfile(
92+
settings,
93+
compact=False,
94+
verbose=True,
95+
show_secrets=True,
96+
exclude_unset=minimal,
97+
)

0 commit comments

Comments
 (0)