[V3] --token and --no-instance flags (#1872)

* Ability to run Red with token without instance

* --no-instance flag

* Reformatted cli with black

* Fix changes requested by @Tobotimus

- Use "system reboot" to be clearer
- save_default_config renamed to create_temp_config
- More documentation for the create_temp_config function

* Update create_temp_config call

* Fix up imports
This commit is contained in:
El Laggron 2018-06-25 11:25:23 +02:00 committed by Toby Harradine
parent c1bcca4432
commit ad27607ccc
3 changed files with 45 additions and 2 deletions

View File

@ -6,7 +6,7 @@ import sys
import discord import discord
from redbot.core.bot import Red, ExitCodes from redbot.core.bot import Red, ExitCodes
from redbot.core.cog_manager import CogManagerUI from redbot.core.cog_manager import CogManagerUI
from redbot.core.data_manager import load_basic_configuration, config_file from redbot.core.data_manager import create_temp_config, load_basic_configuration, config_file
from redbot.core.json_io import JsonIO from redbot.core.json_io import JsonIO
from redbot.core.global_checks import init_global_checks from redbot.core.global_checks import init_global_checks
from redbot.core.events import init_events from redbot.core.events import init_events
@ -106,9 +106,17 @@ def main():
elif cli_flags.version: elif cli_flags.version:
print(description) print(description)
sys.exit(0) sys.exit(0)
elif not cli_flags.instance_name: elif not cli_flags.instance_name and not cli_flags.no_instance:
print("Error: No instance name was provided!") print("Error: No instance name was provided!")
sys.exit(1) sys.exit(1)
if cli_flags.no_instance:
print(
"\033[1m"
"Warning: The data will be placed in a temporary folder and removed on next system reboot."
"\033[0m"
)
cli_flags.instance_name = "temporary_red"
create_temp_config()
load_basic_configuration(cli_flags.instance_name) load_basic_configuration(cli_flags.instance_name)
log, sentry_log = init_loggers(cli_flags) log, sentry_log = init_loggers(cli_flags)
red = Red(cli_flags=cli_flags, description=description, pm_help=None) red = Red(cli_flags=cli_flags, description=description, pm_help=None)
@ -122,6 +130,8 @@ def main():
tmp_data = {} tmp_data = {}
loop.run_until_complete(_get_prefix_and_token(red, tmp_data)) loop.run_until_complete(_get_prefix_and_token(red, tmp_data))
token = os.environ.get("RED_TOKEN", tmp_data["token"]) token = os.environ.get("RED_TOKEN", tmp_data["token"])
if cli_flags.token:
token = cli_flags.token
prefix = cli_flags.prefix or tmp_data["prefix"] prefix = cli_flags.prefix or tmp_data["prefix"]
if not (token and prefix): if not (token and prefix):
if cli_flags.no_prompt is False: if cli_flags.no_prompt is False:

View File

@ -138,6 +138,16 @@ def parse_cli_flags(args):
action="store_true", action="store_true",
help="Enables the built-in RPC server. Please read the docs prior to enabling this!", help="Enables the built-in RPC server. Please read the docs prior to enabling this!",
) )
parser.add_argument("--token", type=str, help="Run Red with the given token.")
parser.add_argument(
"--no-instance",
action="store_true",
help=(
"Run Red without any existing instance. "
"The data will be saved under a temporary folder "
"and deleted on next system restart."
),
)
parser.add_argument( parser.add_argument(
"instance_name", nargs="?", help="Name of the bot instance created during `redbot-setup`." "instance_name", nargs="?", help="Name of the bot instance created during `redbot-setup`."
) )

View File

@ -2,15 +2,18 @@ import sys
import os import os
from pathlib import Path from pathlib import Path
from typing import List from typing import List
from copy import deepcopy
import hashlib import hashlib
import shutil import shutil
import logging import logging
import appdirs import appdirs
import tempfile
from .json_io import JsonIO from .json_io import JsonIO
__all__ = [ __all__ = [
"create_temp_config",
"load_basic_configuration", "load_basic_configuration",
"cog_data_path", "cog_data_path",
"core_data_path", "core_data_path",
@ -39,6 +42,26 @@ if not config_dir:
config_file = config_dir / "config.json" config_file = config_dir / "config.json"
def create_temp_config():
"""
Creates a default instance for Red, so it can be ran
without creating an instance.
.. warning:: The data of this instance will be removed
on next system restart.
"""
name = "temporary_red"
default_dirs = deepcopy(basic_config_default)
default_dirs["DATA_PATH"] = tempfile.mkdtemp()
default_dirs["STORAGE_TYPE"] = "JSON"
default_dirs["STORAGE_DETAILS"] = {}
config = JsonIO(config_file)._load_json()
config[name] = default_dirs
JsonIO(config_file)._save_json(config)
def load_basic_configuration(instance_name_: str): def load_basic_configuration(instance_name_: str):
"""Loads the basic bootstrap configuration necessary for `Config` """Loads the basic bootstrap configuration necessary for `Config`
to know where to store or look for data. to know where to store or look for data.