|
1 | | -import typing as t # noqa: F401 |
| 1 | +import typing as t |
| 2 | +from dataclasses import fields |
2 | 3 |
|
3 | 4 | import click |
| 5 | +from click.shell_completion import CompletionItem |
4 | 6 |
|
| 7 | +from opendigger_pycli.console import CONSOLE |
5 | 8 |
|
6 | | -@click.command("config") |
7 | | -def config(): |
8 | | - pass |
| 9 | +from ..base import pass_environment |
| 10 | +from ..config import ALL_CONFIGS |
| 11 | + |
| 12 | +if t.TYPE_CHECKING: |
| 13 | + from click.core import Context, Parameter |
| 14 | + |
| 15 | + from opendigger_pycli.datatypes.config import BaseConfig |
| 16 | + |
| 17 | + from ..base import Environment |
| 18 | + |
| 19 | + |
| 20 | +def config_shell_completion( |
| 21 | + ctx: t.Optional["Context"], param: t.Optional["Parameter"], incomplete: str |
| 22 | +) -> t.List[CompletionItem]: |
| 23 | + incomplete_splited = incomplete.rsplit(".", 1)[0] |
| 24 | + if incomplete_splited == "": |
| 25 | + return [ |
| 26 | + CompletionItem(config_dataclass_key) for config_dataclass_key in ALL_CONFIGS |
| 27 | + ] |
| 28 | + is_key = False |
| 29 | + last_config_dataclass: t.Optional[BaseConfig] = None |
| 30 | + for config_dataclass_key in ALL_CONFIGS: |
| 31 | + if incomplete_splited.startswith(config_dataclass_key): |
| 32 | + if incomplete_splited != config_dataclass_key: |
| 33 | + return [CompletionItem(config_dataclass_key + ".")] |
| 34 | + else: |
| 35 | + is_key = True |
| 36 | + last_config_dataclass = ALL_CONFIGS[config_dataclass_key] # type: ignore |
| 37 | + break |
| 38 | + else: |
| 39 | + continue |
| 40 | + |
| 41 | + if not is_key and last_config_dataclass is None: |
| 42 | + return [] |
| 43 | + |
| 44 | + if incomplete[:-1] == incomplete_splited: |
| 45 | + return [CompletionItem(f"{incomplete_splited}.{field.name}") for field in fields(last_config_dataclass)] # type: ignore |
| 46 | + |
| 47 | + for field in fields(last_config_dataclass): # type: ignore |
| 48 | + if incomplete in f"{incomplete_splited}.{field.name}": |
| 49 | + return [CompletionItem(f"{incomplete_splited}.{field.name}")] |
| 50 | + |
| 51 | + return [] |
| 52 | + |
| 53 | + |
| 54 | +def parse_config_key(key: str) -> t.Tuple[str, str]: |
| 55 | + section_name, config_key = key.rsplit(".", 1) |
| 56 | + return section_name, config_key |
| 57 | + |
| 58 | + |
| 59 | +def check_config_setting( |
| 60 | + ctx: "Context", param: "Parameter", values: t.List[t.Tuple[str, str]] |
| 61 | +) -> t.List[t.Tuple[str, str]]: |
| 62 | + for value in values: |
| 63 | + try: |
| 64 | + section_name, config_key = parse_config_key(value[0]) |
| 65 | + except Exception: |
| 66 | + raise click.BadParameter(f"{value[0]} is not a valid config key") |
| 67 | + else: |
| 68 | + if section_name not in ALL_CONFIGS or config_key not in [ |
| 69 | + field.name for field in fields(ALL_CONFIGS[section_name]) |
| 70 | + ]: |
| 71 | + raise click.BadParameter(f"{value[0]} is not a valid config key") |
| 72 | + return values |
| 73 | + |
| 74 | + |
| 75 | +@click.command("config") # type: ignore |
| 76 | +@click.option( |
| 77 | + "--set", |
| 78 | + "-s", |
| 79 | + "config_settings", |
| 80 | + type=(str, str), |
| 81 | + multiple=True, |
| 82 | + callback=check_config_setting, |
| 83 | + shell_complete=config_shell_completion, |
| 84 | + help="Set config value", |
| 85 | + required=True, |
| 86 | +) |
| 87 | +@click.option( |
| 88 | + "--persist/--no-persist", |
| 89 | + "-p/-n", |
| 90 | + is_flag=True, |
| 91 | + default=True, |
| 92 | + help="Set config value persistently", |
| 93 | +) |
| 94 | +@pass_environment |
| 95 | +def config( |
| 96 | + env: "Environment", config_settings: t.List[t.Tuple[str, str]], persist: bool |
| 97 | +) -> None: |
| 98 | + """Set config value""" |
| 99 | + for config_setting in config_settings: |
| 100 | + key, value = config_setting |
| 101 | + section_name, key = parse_config_key(key) |
| 102 | + env.dlog(f"set config: {section_name}.{key}={value}") |
| 103 | + env.set_config(section_name, key, value, is_persist=persist) |
| 104 | + env.dlog(f"finished to set config: {section_name}.{key}={value}") |
| 105 | + |
| 106 | + CONSOLE.print("[green]Config set successfully[/]") |
| 107 | + CONSOLE.print(env.cli_config) |
0 commit comments