-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfigure.py
87 lines (62 loc) · 2.28 KB
/
configure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import typing as t
import secrets
import click
from string import Template
from app.utils import filesystem
APP_CONFIG_FILE = "instance/config.py"
INSTANCE_CONFIG_TEXT = """
SECRET_KEY = "$secret_key"
SQLALCHEMY_DATABASE_URI = "$sqlalchemy_database_uri"
"""
DOT_ENV_TEXT = """
FLASK_DEBUG=1
APP_CONFIG=$flask_env
FLASK_RUN_PORT=$flask_port
"""
def ask_for_database_uri() -> t.Any:
click.echo(click.style("> Let's add database support:", fg="green", bold=True))
click.echo("""
Format: postgresql://<user>:<password>@localhost:5432/<db>\n
Or leave it empty, if you don't need database support
""")
return click.prompt("Database URI", default="")
def create_instance_config() -> str:
secret_key: str = secrets.token_hex()
sqlalchemy_database_uri: t.Any = ask_for_database_uri()
variables: dict = {
"secret_key": secret_key,
"sqlalchemy_database_uri": sqlalchemy_database_uri
}
filesystem.set_file(
APP_CONFIG_FILE,
Template(INSTANCE_CONFIG_TEXT).substitute(variables)
)
click.echo(click.style("[x] %s created." % APP_CONFIG_FILE, fg="green", bold=True))
return sqlalchemy_database_uri
def create_dot_env() -> None:
flask_env: t.Any = click.prompt("APP_CONFIG", default="development")
if flask_env != "production":
flask_port: str = click.prompt("FLASK_RUN_PORT", default="5000")
filesystem.set_file(".env", Template(DOT_ENV_TEXT).substitute({
"flask_env": flask_env,
"flask_port": flask_port
}))
click.echo(click.style("[x] .env created.", fg="green", bold=True))
else:
click.echo(click.style("[] .env ignored.", fg="green", bold=True))
@click.command()
def init_config() -> None:
click.echo(click.style("*** Starting initial configuration ***", fg="green", bold=True))
# instance/config.py
if filesystem.has_file(APP_CONFIG_FILE):
click.echo("Looks like you already have %s" % APP_CONFIG_FILE)
else:
create_instance_config()
# .env
if filesystem.has_file(".env"):
click.echo("Looks like you already have .env")
else:
create_dot_env()
click.echo(click.style("All done. Now you can use 'flask run'", fg="green", bold=True))
if __name__ == '__main__':
init_config()