Respect "-" as stdout

This commit is contained in:
vrde 2016-04-26 02:09:53 +02:00
parent 2d6c502ee3
commit c3ad343e77
No known key found for this signature in database
GPG Key ID: 6581C7C39B3D397D
2 changed files with 25 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import logging
import argparse import argparse
import copy import copy
import json import json
import builtins
import bigchaindb import bigchaindb
import bigchaindb.config_utils import bigchaindb.config_utils
@ -23,6 +24,14 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# We need this because `input` always prints on stdout, while it should print
# to stderr. It's a very old bug, check it out here:
# - https://bugs.python.org/issue1927
def input(prompt):
print(prompt, end='', file=sys.stderr)
return builtins.input()
def run_show_config(args): def run_show_config(args):
"""Show the current configuration""" """Show the current configuration"""
# TODO Proposal: remove the "hidden" configuration. Only show config. If # TODO Proposal: remove the "hidden" configuration. Only show config. If
@ -43,7 +52,11 @@ def run_configure(args, skip_if_exists=False):
skip_if_exists (bool): skip the function if a config file already exists skip_if_exists (bool): skip the function if a config file already exists
""" """
config_path = args.config or bigchaindb.config_utils.CONFIG_DEFAULT_PATH config_path = args.config or bigchaindb.config_utils.CONFIG_DEFAULT_PATH
config_file_exists = os.path.exists(config_path)
config_file_exists = False
# if the config path is `-` then it's stdout
if config_path != '-':
config_file_exists = os.path.exists(config_path)
if config_file_exists and skip_if_exists: if config_file_exists and skip_if_exists:
return return
@ -57,7 +70,7 @@ def run_configure(args, skip_if_exists=False):
# Patch the default configuration with the new values # Patch the default configuration with the new values
conf = copy.deepcopy(bigchaindb._config) conf = copy.deepcopy(bigchaindb._config)
print('Generating keypair') print('Generating keypair', file=sys.stderr)
conf['keypair']['private'], conf['keypair']['public'] = \ conf['keypair']['private'], conf['keypair']['public'] = \
crypto.generate_key_pair() crypto.generate_key_pair()
@ -80,9 +93,12 @@ def run_configure(args, skip_if_exists=False):
input('Statsd {}? (default `{}`): '.format(key, val)) \ input('Statsd {}? (default `{}`): '.format(key, val)) \
or val or val
bigchaindb.config_utils.write_config(conf, config_path) if config_path != '-':
print('Configuration written to {}'.format(config_path)) bigchaindb.config_utils.write_config(conf, config_path)
print('Ready to go!') else:
print(json.dumps(conf, indent=4, sort_keys=True))
print('Configuration written to {}'.format(config_path), file=sys.stderr)
print('Ready to go!', file=sys.stderr)
def run_export_my_pubkey(args): def run_export_my_pubkey(args):
@ -110,8 +126,8 @@ def run_init(args):
try: try:
db.init() db.init()
except DatabaseAlreadyExists: except DatabaseAlreadyExists:
print('The database already exists.') print('The database already exists.', file=sys.stderr)
print('If you wish to re-initialize it, first drop it.') print('If you wish to re-initialize it, first drop it.', file=sys.stderr)
def run_drop(args): def run_drop(args):

View File

@ -69,7 +69,8 @@ def start(parser, scope):
base_parser = argparse.ArgumentParser(add_help=False, prog='bigchaindb') base_parser = argparse.ArgumentParser(add_help=False, prog='bigchaindb')
base_parser.add_argument('-c', '--config', base_parser.add_argument('-c', '--config',
help='Specify the location of the configuration file') help='Specify the location of the configuration file '
'(use "-" for stdout)')
base_parser.add_argument('-y', '--yes', '--yes-please', base_parser.add_argument('-y', '--yes', '--yes-please',
action='store_true', action='store_true',