microblog.pub/scripts/config_wizard.py

101 lines
2.7 KiB
Python
Raw Normal View History

2022-06-22 18:11:22 +00:00
"""Basic wizard for setting up microblog.pub configuration files."""
import os
import sys
from pathlib import Path
from typing import Any
import bcrypt
import tomli_w
from prompt_toolkit import prompt
2022-07-28 18:28:13 +00:00
from prompt_toolkit.key_binding import KeyBindings
2022-06-22 18:11:22 +00:00
from app.key import generate_key
2022-07-04 18:25:27 +00:00
_ROOT_DIR = Path().parent.resolve()
_KEY_PATH = _ROOT_DIR / "data" / "key.pem"
2022-06-22 18:11:22 +00:00
2022-07-28 18:28:13 +00:00
_kb = KeyBindings()
@_kb.add("c-@")
def _(event):
"""Save multi-line buffer on CTRL + space"""
event.current_buffer.validate_and_handle()
2022-06-22 18:11:22 +00:00
def main() -> None:
theme_file = Path("data/_theme.scss")
if not theme_file.exists():
theme_file.write_text("// override vars for theming here")
2022-06-22 18:11:22 +00:00
print("Welcome to microblog.pub setup wizard\n")
print("Generating key...")
2022-07-04 18:25:27 +00:00
if _KEY_PATH.exists():
2022-06-22 18:11:22 +00:00
yn = ""
while yn not in ["y", "n"]:
yn = prompt(
"WARNING, a key already exists, overwrite it? (y/n): ", default="n"
).lower()
if yn == "y":
2022-07-04 18:25:27 +00:00
generate_key(_KEY_PATH)
2022-06-22 18:11:22 +00:00
else:
2022-07-04 18:25:27 +00:00
generate_key(_KEY_PATH)
2022-06-22 18:11:22 +00:00
2022-07-07 18:37:16 +00:00
config_file = Path("data/profile.toml")
2022-06-22 18:11:22 +00:00
if config_file.exists():
# Spit out the relative path for the "config artifacts"
2022-07-07 18:37:16 +00:00
rconfig_file = "data/profile.toml"
2022-06-22 18:11:22 +00:00
print(
f"Existing setup detected, please delete {rconfig_file} "
"before restarting the wizard"
)
sys.exit(2)
dat: dict[str, Any] = {}
print("Your identity will be @{username}@{domain}")
dat["domain"] = prompt("domain: ")
dat["username"] = prompt("username: ")
dat["admin_password"] = bcrypt.hashpw(
prompt("admin password: ", is_password=True).encode(), bcrypt.gensalt()
).decode()
dat["name"] = prompt("name (e.g. John Doe): ", default=dat["username"])
2022-08-24 18:12:10 +00:00
dat["summary"] = prompt(
(
"summary (short description, in markdown, "
"press [CTRL] + [SPACE] to submit):\n"
),
key_bindings=_kb,
multiline=True,
2022-06-22 18:11:22 +00:00
)
dat["https"] = True
proto = "https"
yn = ""
while yn not in ["y", "n"]:
yn = prompt("will the site be served via https? (y/n): ", default="y").lower()
if yn == "n":
dat["https"] = False
proto = "http"
print("Note that you can put your icon/avatar in the static/ directory")
if icon_url := prompt(
2022-06-22 18:11:22 +00:00
"icon URL: ", default=f'{proto}://{dat["domain"]}/static/nopic.png'
):
dat["icon_url"] = icon_url
2022-06-22 18:11:22 +00:00
dat["secret"] = os.urandom(16).hex()
with config_file.open("w") as f:
f.write(tomli_w.dumps(dat))
print("Done")
sys.exit(0)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Aborted")
sys.exit(1)