From 8dcace4bfdf1bb513b5f5ab5aada1bd9e8f8c5d0 Mon Sep 17 00:00:00 2001 From: palmtree5 <3577255+palmtree5@users.noreply.github.com> Date: Sun, 19 Nov 2017 14:17:01 -0900 Subject: [PATCH] [Core V3] add instance listing cli flag (#1079) * [Core] add instance listing cli flag * Alphabetically sort instance names --- redbot/__main__.py | 22 +++++++++++++++++++++- redbot/core/cli.py | 5 ++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/redbot/__main__.py b/redbot/__main__.py index 7ce9a5cc3..560b9b200 100644 --- a/redbot/__main__.py +++ b/redbot/__main__.py @@ -6,7 +6,8 @@ import sys import discord from redbot.core.bot import Red, ExitCodes from redbot.core.cog_manager import CogManagerUI -from redbot.core.data_manager import load_basic_configuration +from redbot.core.data_manager import load_basic_configuration, config_file +from redbot.core.json_io import JsonIO from redbot.core.global_checks import init_global_checks from redbot.core.events import init_events from redbot.core.sentry_setup import init_sentry_logging @@ -81,8 +82,27 @@ async def _get_prefix_and_token(red, indict): indict['enable_sentry'] = await red.db.enable_sentry() +def list_instances(): + if not config_file.exists(): + print("No instances have been configured! Configure one " + "using `redbot-setup` before trying to run the bot!") + sys.exit(1) + else: + data = JsonIO(config_file)._load_json() + text = "Configured Instances:\n\n" + for instance_name in sorted(data.keys()): + text += "{}\n".format(instance_name) + print(text) + sys.exit(0) + + def main(): cli_flags = parse_cli_flags(sys.argv[1:]) + if cli_flags.list_instances: + list_instances() + elif not cli_flags.instance_name: + print("Error: No instance name was provided!") + sys.exit(1) load_basic_configuration(cli_flags.instance_name) log, sentry_log = init_loggers(cli_flags) description = "Red - Version {}".format(__version__) diff --git a/redbot/core/cli.py b/redbot/core/cli.py index fe6e00185..87c58cb92 100644 --- a/redbot/core/cli.py +++ b/redbot/core/cli.py @@ -63,6 +63,9 @@ def ask_sentry(red: Red): def parse_cli_flags(args): parser = argparse.ArgumentParser(description="Red - Discord Bot") + parser.add_argument("--list-instances", action="store_true", + help="List all instance names setup " + "with 'redbot-setup'") parser.add_argument("--owner", type=int, help="ID of the owner. Only who hosts " "Red should be owner, this has " @@ -106,7 +109,7 @@ def parse_cli_flags(args): action="store_true", help="Enables the built-in RPC server. Please read the docs" "prior to enabling this!") - parser.add_argument("instance_name", + parser.add_argument("instance_name", nargs="?", help="Name of the bot instance created during `redbot-setup`.") args = parser.parse_args(args)