Silent stderr output during app init for some commands (#293)

Avoid confusing the user for the commands outputing text to the user.

fix #277
This commit is contained in:
JocelynDelalande 2017-12-27 17:28:18 +01:00 committed by Alexis Metaireau
parent f2a53eb1e3
commit 1c9120e68b

View file

@ -3,6 +3,7 @@
import os
import pkgutil
import random
import sys
from getpass import getpass
from flask_script import Manager, Command, Option
@ -56,9 +57,20 @@ class ConfigTemplate(Command):
def main():
QUIET_COMMANDS = ('generate_password_hash', 'generate-config')
backup_stderr = sys.stderr
# Hack to divert stderr for commands generating content to stdout
# to avoid confusing the user
if len(sys.argv) > 1 and sys.argv[1] in QUIET_COMMANDS:
sys.stderr = open(os.devnull, 'w')
app = create_app()
Migrate(app, db)
# Restore stderr (among other: to be able to display help)
sys.stderr = backup_stderr
manager = Manager(app)
manager.add_command('db', MigrateCommand)
manager.add_command('generate_password_hash', GeneratePasswordHash)