Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
Fix various inconsistencies/bugs in cli.py
Browse files Browse the repository at this point in the history
  • Loading branch information
b1naryth1ef committed Feb 15, 2018
1 parent 90098c1 commit 51576d2
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions disco/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,35 @@
monkey.patch_all()

parser = argparse.ArgumentParser()
parser.add_argument('--config', help='Configuration file', default='config.json')

# Command line specific arguments
parser.add_argument('--run-bot', help='run a disco bot on this client', action='store_true', default=False)
parser.add_argument('--plugin', help='load plugins into the bot', nargs='*', default=[])
parser.add_argument('--config', help='Configuration file', default=None)
parser.add_argument('--shard-auto', help='Automatically run all shards', action='store_true', default=False)

# Configuration overrides
parser.add_argument('--token', help='Bot Authentication Token', default=None)
parser.add_argument('--shard-count', help='Total number of shards', default=None)
parser.add_argument('--shard-id', help='Current shard number/id', default=None)
parser.add_argument('--shard-auto', help='Automatically run all shards', action='store_true', default=False)
parser.add_argument('--shard-count', help='Total number of shards', default=None)
parser.add_argument('--max-reconnects', help='Maximum reconnect attempts', default=None)
parser.add_argument('--log-level', help='log level', default=None)
parser.add_argument('--manhole', action='store_true', help='Enable the manhole', default=None)
parser.add_argument('--manhole-bind', help='host:port for the manhole to bind too', default=None)
parser.add_argument('--encoder', help='encoder for gateway data', default=None)
parser.add_argument('--run-bot', help='run a disco bot on this client', action='store_true', default=False)
parser.add_argument('--plugin', help='load plugins into the bot', nargs='*', default=[])
parser.add_argument('--log-level', help='log level', default=None)
parser.add_argument('--http-bind', help='bind information for http server', default=None)


# Mapping of argument names to configuration overrides
CONFIG_OVERRIDE_MAPPING = {
'token': 'token',
'shard_id': 'shard_id',
'shard_count': 'shard_count',
'max_reconnects': 'max_reconnects',
'log_level': 'log_level',
'manhole': 'manhole_enable',
'manhole_bind': 'manhole_bind',
'encoder': 'encoder',
}


def disco_main(run=False):
Expand All @@ -38,36 +55,41 @@ def disco_main(run=False):
:class:`Client`
A new Client from the provided command line arguments
"""
args = parser.parse_args()

from disco.client import Client, ClientConfig
from disco.bot import Bot, BotConfig
from disco.util.logging import setup_logging

if os.path.exists(args.config):
# Parse out all our command line arguments
args = parser.parse_args()

# Create the base configuration object
if args.config:
config = ClientConfig.from_file(args.config)
else:
config = ClientConfig()

config.manhole_enable = args.manhole
if args.manhole_bind:
config.manhole_bind = args.manhole_bind
if args.log_level:
config.log_level = args.log_level
if os.path.exists('config.json'):
config = ClientConfig.from_file('config.json')
elif os.path.exists('config.yaml'):
config = ClientConfig.from_file('config.yaml')
else:
config = ClientConfig()

for k, v in six.iteritems(vars(args)):
if hasattr(config, k) and v is not None:
setattr(config, k, v)
for arg_key, config_key in six.iteritems(CONFIG_OVERRIDE_MAPPING):
if getattr(args, arg_key) is not None:
setattr(config, config_key, getattr(args, arg_key))

# Setup the auto-sharder
if args.shard_auto:
from disco.gateway.sharder import AutoSharder
AutoSharder(config).run()
return

# Setup logging based on the configured level
setup_logging(level=getattr(logging, config.log_level.upper()))

# Build out client object
client = Client(config)

# If applicable, build the bot and load plugins
bot = None
if args.run_bot or hasattr(config, 'bot'):
bot_config = BotConfig(config.bot) if hasattr(config, 'bot') else BotConfig()
Expand All @@ -76,12 +98,6 @@ def disco_main(run=False):
else:
bot_config.plugins += args.plugin

if args.http_bind:
bot_config.http_enabled = True
host, port = args.http_bind.split(':', 1)
bot_config.http_host = host
bot_config.http_port = int(port)

bot = Bot(client, bot_config)

if run:
Expand Down

0 comments on commit 51576d2

Please sign in to comment.