Use parent methods for print/prompt in install command

This commit is contained in:
Lance Edgar 2023-01-02 09:51:13 -06:00
parent 2500a3c3d8
commit 617515a167

View file

@ -2,7 +2,7 @@
######################################################################
#
# Messkit -- Generic-ish Data Utility App
# Copyright © 2022 Lance Edgar
# Copyright © 2022-2023 Lance Edgar
#
# This file is part of Messkit.
#
@ -30,9 +30,6 @@ import sys
import subprocess
import sqlalchemy as sa
from prompt_toolkit import prompt
from prompt_toolkit.styles import Style
from rich import print as rprint
from alembic.util.messaging import obfuscate_url_pw
from rattail import commands
@ -69,19 +66,19 @@ class Install(commands.Subcommand):
def run(self, args):
rprint("\n\t[blue]Welcome to Messkit![/blue]")
rprint("\n\tThis tool will install and configure a new app.")
rprint("\n\t[italic]NB. You should already have created a new database in PostgreSQL or MySQL.[/italic]")
self.rprint("\n\t[blue]Welcome to Messkit![/blue]")
self.rprint("\n\tThis tool will install and configure a new app.")
self.rprint("\n\t[italic]NB. You should already have created a new database in PostgreSQL or MySQL.[/italic]")
# continue?
if not self.basic_prompt("continue?", True, is_bool=True):
rprint()
self.rprint()
sys.exit(0)
# appdir must not yet exist
appdir = os.path.join(sys.prefix, 'app')
if os.path.exists(appdir):
rprint("\n\t[bold red]appdir already exists:[/bold red] {}\n".format(appdir))
self.rprint("\n\t[bold red]appdir already exists:[/bold red] {}\n".format(appdir))
sys.exit(1)
# get db info
@ -97,15 +94,15 @@ class Install(commands.Subcommand):
dbpass = self.basic_prompt('db pass', is_password=True)
# test db connection
rprint("\n\ttesting db connection... ", end='')
self.rprint("\n\ttesting db connection... ", end='')
dburl = self.make_db_url(dbtype, dbhost, dbport, dbname, dbuser, dbpass)
error = self.test_db_connection(dburl)
if error:
rprint("[bold red]cannot connect![/bold red] ..error was:")
rprint("\n{}".format(error))
rprint("\n\t[bold yellow]aborting mission[/bold yellow]\n")
self.rprint("[bold red]cannot connect![/bold red] ..error was:")
self.rprint("\n{}".format(error))
self.rprint("\n\t[bold yellow]aborting mission[/bold yellow]\n")
sys.exit(1)
rprint("[bold green]good[/bold green]")
self.rprint("[bold green]good[/bold green]")
# make the appdir
self.app.make_appdir(appdir)
@ -142,13 +139,13 @@ class Install(commands.Subcommand):
| stat.S_IROTH
| stat.S_IXOTH)
rprint("\n\tappdir created at: [bold green]{}[/bold green]".format(appdir))
self.rprint("\n\tappdir created at: [bold green]{}[/bold green]".format(appdir))
bindir = os.path.join(sys.prefix, 'bin')
schema_installed = False
if self.basic_prompt("install db schema?", True, is_bool=True):
rprint()
self.rprint()
# install db schema
alembic = os.path.join(bindir, 'alembic')
@ -178,7 +175,7 @@ class Install(commands.Subcommand):
'setting-put', 'tailbone.favicon_url', '/messkit/img/messkit-small.png']
subprocess.check_call(cmd)
rprint("\n\tdb schema installed to: [bold green]{}[/bold green]".format(
self.rprint("\n\tdb schema installed to: [bold green]{}[/bold green]".format(
obfuscate_url_pw(dburl)))
if self.basic_prompt("create admin user?", True, is_bool=True):
@ -191,11 +188,11 @@ class Install(commands.Subcommand):
if password:
confirm = self.basic_prompt('confirm password', is_password=True)
if not confirm or confirm != password:
rprint("[bold yellow]passwords did not match[/bold yellow]")
self.rprint("[bold yellow]passwords did not match[/bold yellow]")
password = None
fullname = self.basic_prompt('full name')
rprint()
self.rprint()
# make admin user
rattail = os.path.join(bindir, 'rattail')
@ -205,71 +202,30 @@ class Install(commands.Subcommand):
cmd.extend(['--full-name', fullname])
subprocess.check_call(cmd)
rprint("\n\tadmin user created: [bold green]{}[/bold green]".format(
self.rprint("\n\tadmin user created: [bold green]{}[/bold green]".format(
username))
if self.basic_prompt("make poser dir?", True, is_bool=True):
rprint()
self.rprint()
# make poser dir
poser_handler = self.app.get_poser_handler()
poserdir = poser_handler.make_poser_dir()
rprint("\n\tposer dir created: [bold green]{}[/bold green]".format(
self.rprint("\n\tposer dir created: [bold green]{}[/bold green]".format(
poserdir))
rprint("\n\t[bold green]initial setup is complete![/bold green]")
self.rprint("\n\t[bold green]initial setup is complete![/bold green]")
if schema_installed:
rprint("\n\tyou can run the web app with this command:")
self.rprint("\n\tyou can run the web app with this command:")
pserve = os.path.join(bindir, 'pserve')
rprint("\n\t[blue]{} file+ini:{}[/blue]".format(pserve, web_conf))
self.rprint("\n\t[blue]{} file+ini:{}[/blue]".format(pserve, web_conf))
rprint()
self.rprint()
# TODO: somewhere should ask about apache proxy, https etc.?
def basic_prompt(self, info, default=None, is_password=False, is_bool=False):
# message formatting styles
style = Style.from_dict({
'': '',
'bold': 'bold',
})
# build prompt message
message = [
('', '\n'),
('class:bold', info),
]
if default is not None:
if is_bool:
message.append(('', ' [{}]: '.format('Y' if default else 'N')))
else:
message.append(('', ' [{}]: '.format(default)))
else:
message.append(('', ': '))
# prompt user for input
try:
text = prompt(message, style=style, is_password=is_password)
except (KeyboardInterrupt, EOFError):
rprint("\n\t[bold yellow]operation canceled by user[/bold yellow]\n",
file=self.stderr)
sys.exit(2)
if is_bool:
if text == '':
return default
elif text.upper() == 'Y':
return True
elif text.upper() == 'N':
return False
rprint("\n\t[bold yellow]ambiguous, please try again[/bold yellow]\n")
return self.basic_prompt(info, default, is_bool=True)
return text or default
def make_db_url(self, dbtype, dbhost, dbport, dbname, dbuser, dbpass):
try:
# newer style