2021-04-06 10:09:00 +00:00
|
|
|
from typing import List
|
2021-02-25 08:09:14 +00:00
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
import click
|
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
from .. import config as tutor_config
|
2021-11-23 08:25:09 +00:00
|
|
|
from .. import env, exceptions, fmt
|
2019-06-05 17:45:22 +00:00
|
|
|
from .. import interactive as interactive_config
|
2019-12-12 16:05:56 +00:00
|
|
|
from .. import serialize
|
2021-04-06 10:09:00 +00:00
|
|
|
from ..types import Config
|
2021-02-25 08:09:14 +00:00
|
|
|
from .context import Context
|
2019-01-22 20:25:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.group(
|
2019-05-29 08:30:30 +00:00
|
|
|
name="config",
|
2019-01-22 20:25:04 +00:00
|
|
|
short_help="Configure Open edX",
|
2019-05-05 09:45:24 +00:00
|
|
|
help="""Configure Open edX and store configuration values in $TUTOR_ROOT/config.yml""",
|
2019-01-22 20:25:04 +00:00
|
|
|
)
|
2021-02-25 08:09:14 +00:00
|
|
|
def config_command() -> None:
|
2019-01-22 20:25:04 +00:00
|
|
|
pass
|
|
|
|
|
2019-04-19 22:02:47 +00:00
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
@click.command(help="Create and save configuration interactively")
|
2019-06-05 17:45:22 +00:00
|
|
|
@click.option("-i", "--interactive", is_flag=True, help="Run interactively")
|
2019-07-07 22:29:24 +00:00
|
|
|
@click.option(
|
|
|
|
"-s",
|
|
|
|
"--set",
|
2020-11-12 14:31:13 +00:00
|
|
|
"set_vars",
|
2021-03-08 11:20:46 +00:00
|
|
|
type=serialize.YamlParamType(),
|
2019-07-07 22:29:24 +00:00
|
|
|
multiple=True,
|
|
|
|
metavar="KEY=VAL",
|
|
|
|
help="Set a configuration value (can be used multiple times)",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-U",
|
|
|
|
"--unset",
|
2020-11-12 14:31:13 +00:00
|
|
|
"unset_vars",
|
2019-07-07 22:29:24 +00:00
|
|
|
multiple=True,
|
|
|
|
help="Remove a configuration value (can be used multiple times)",
|
|
|
|
)
|
2021-06-22 09:45:12 +00:00
|
|
|
@click.option(
|
|
|
|
"-e", "--env-only", "env_only", is_flag=True, help="Skip updating config.yaml"
|
|
|
|
)
|
2019-12-12 16:05:56 +00:00
|
|
|
@click.pass_obj
|
2021-02-25 08:09:14 +00:00
|
|
|
def save(
|
2021-06-22 09:45:12 +00:00
|
|
|
context: Context,
|
|
|
|
interactive: bool,
|
|
|
|
set_vars: Config,
|
|
|
|
unset_vars: List[str],
|
|
|
|
env_only: bool,
|
2021-02-25 08:09:14 +00:00
|
|
|
) -> None:
|
2021-11-08 13:46:38 +00:00
|
|
|
config = interactive_config.load_user_config(context.root, interactive=interactive)
|
2020-11-12 14:31:13 +00:00
|
|
|
if set_vars:
|
2021-11-08 13:46:38 +00:00
|
|
|
for key, value in dict(set_vars).items():
|
|
|
|
config[key] = env.render_unknown(config, value)
|
2020-11-12 14:31:13 +00:00
|
|
|
for key in unset_vars:
|
2019-07-07 22:29:24 +00:00
|
|
|
config.pop(key, None)
|
2021-06-22 09:45:12 +00:00
|
|
|
if not env_only:
|
|
|
|
tutor_config.save_config_file(context.root, config)
|
2021-11-08 13:46:38 +00:00
|
|
|
|
2021-12-08 10:20:41 +00:00
|
|
|
# Reload configuration, without version checking
|
|
|
|
config = tutor_config.load_full(context.root)
|
2019-12-12 16:05:56 +00:00
|
|
|
env.save(context.root, config)
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-19 22:02:47 +00:00
|
|
|
|
2020-01-16 14:40:38 +00:00
|
|
|
@click.command(help="Render a template folder with eventual extra configuration files")
|
|
|
|
@click.option(
|
|
|
|
"-x",
|
|
|
|
"--extra-config",
|
|
|
|
"extra_configs",
|
|
|
|
multiple=True,
|
|
|
|
type=click.Path(exists=True, resolve_path=True, dir_okay=False),
|
|
|
|
help="Load extra configuration file (can be used multiple times)",
|
|
|
|
)
|
|
|
|
@click.argument("src", type=click.Path(exists=True, resolve_path=True))
|
|
|
|
@click.argument("dst")
|
|
|
|
@click.pass_obj
|
2021-02-25 08:09:14 +00:00
|
|
|
def render(context: Context, extra_configs: List[str], src: str, dst: str) -> None:
|
2020-01-16 14:40:38 +00:00
|
|
|
config = tutor_config.load(context.root)
|
|
|
|
for extra_config in extra_configs:
|
2021-11-08 13:46:38 +00:00
|
|
|
config.update(
|
|
|
|
env.render_unknown(config, tutor_config.get_yaml_file(extra_config))
|
2020-02-27 16:14:00 +00:00
|
|
|
)
|
2020-01-16 14:40:38 +00:00
|
|
|
renderer = env.Renderer(config, [src])
|
|
|
|
renderer.render_all_to(dst)
|
2022-02-07 17:11:43 +00:00
|
|
|
fmt.echo_info(f"Templates rendered to {dst}")
|
2020-01-16 14:40:38 +00:00
|
|
|
|
|
|
|
|
2019-05-05 09:45:24 +00:00
|
|
|
@click.command(help="Print the project root")
|
2019-12-12 16:05:56 +00:00
|
|
|
@click.pass_obj
|
2021-02-25 08:09:14 +00:00
|
|
|
def printroot(context: Context) -> None:
|
2019-12-12 16:05:56 +00:00
|
|
|
click.echo(context.root)
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-19 22:02:47 +00:00
|
|
|
|
2019-03-18 21:38:13 +00:00
|
|
|
@click.command(help="Print a configuration value")
|
|
|
|
@click.argument("key")
|
2019-12-12 16:05:56 +00:00
|
|
|
@click.pass_obj
|
2021-02-25 08:09:14 +00:00
|
|
|
def printvalue(context: Context, key: str) -> None:
|
2019-12-12 16:05:56 +00:00
|
|
|
config = tutor_config.load(context.root)
|
2019-03-18 21:38:13 +00:00
|
|
|
try:
|
2020-01-21 16:09:58 +00:00
|
|
|
# Note that this will incorrectly print None values
|
2021-04-06 10:09:00 +00:00
|
|
|
fmt.echo(str(config[key]))
|
2020-11-07 15:37:43 +00:00
|
|
|
except KeyError as e:
|
2022-02-07 17:11:43 +00:00
|
|
|
raise exceptions.TutorError(f"Missing configuration value: {key}") from e
|
2019-03-18 21:38:13 +00:00
|
|
|
|
2019-04-19 22:02:47 +00:00
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
config_command.add_command(save)
|
2020-01-16 14:40:38 +00:00
|
|
|
config_command.add_command(render)
|
2019-04-23 07:57:55 +00:00
|
|
|
config_command.add_command(printroot)
|
|
|
|
config_command.add_command(printvalue)
|