|
3 | 3 | import plain.runtime |
4 | 4 |
|
5 | 5 |
|
6 | | -@click.command() |
| 6 | +@click.group() |
| 7 | +def settings() -> None: |
| 8 | + """View and inspect settings""" |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +@settings.command() |
7 | 13 | @click.argument("setting_name") |
8 | | -def setting(setting_name: str) -> None: |
9 | | - """Print the value of a setting at runtime""" |
| 14 | +def get(setting_name: str) -> None: |
| 15 | + """Get the value of a specific setting""" |
10 | 16 | try: |
11 | | - setting = getattr(plain.runtime.settings, setting_name) |
12 | | - click.echo(setting) |
| 17 | + value = getattr(plain.runtime.settings, setting_name) |
| 18 | + click.echo(value) |
13 | 19 | except AttributeError: |
14 | 20 | click.secho(f'Setting "{setting_name}" not found', fg="red") |
15 | 21 |
|
16 | 22 |
|
17 | | -# @plain_cli.command() |
18 | | -# @click.option("--filter", "-f", "name_filter", help="Filter settings by name") |
19 | | -# @click.option("--overridden", is_flag=True, help="Only show overridden settings") |
20 | | -# def settings(name_filter, overridden): |
21 | | -# """Print Plain settings""" |
22 | | -# table = Table(box=box.MINIMAL) |
23 | | -# table.add_column("Setting") |
24 | | -# table.add_column("Default value") |
25 | | -# table.add_column("App value") |
26 | | -# table.add_column("Type") |
27 | | -# table.add_column("Module") |
28 | | - |
29 | | -# for setting in dir(settings): |
30 | | -# if setting.isupper(): |
31 | | -# if name_filter and name_filter.upper() not in setting: |
32 | | -# continue |
33 | | - |
34 | | -# is_overridden = settings.is_overridden(setting) |
35 | | - |
36 | | -# if overridden and not is_overridden: |
37 | | -# continue |
38 | | - |
39 | | -# default_setting = settings._default_settings.get(setting) |
40 | | -# if default_setting: |
41 | | -# default_value = default_setting.value |
42 | | -# annotation = default_setting.annotation |
43 | | -# module = default_setting.module |
44 | | -# else: |
45 | | -# default_value = "" |
46 | | -# annotation = "" |
47 | | -# module = "" |
48 | | - |
49 | | -# table.add_row( |
50 | | -# setting, |
51 | | -# Pretty(default_value) if default_value else "", |
52 | | -# Pretty(getattr(settings, setting)) |
53 | | -# if is_overridden |
54 | | -# else Text("<Default>", style="italic dim"), |
55 | | -# Pretty(annotation) if annotation else "", |
56 | | -# str(module.__name__) if module else "", |
57 | | -# ) |
58 | | - |
59 | | -# console = Console() |
60 | | -# console.print(table) |
| 23 | +@settings.command(name="list") |
| 24 | +def list_settings() -> None: |
| 25 | + """List all settings with their sources""" |
| 26 | + if not (items := plain.runtime.settings.get_settings()): |
| 27 | + click.echo("No settings configured.") |
| 28 | + return |
| 29 | + |
| 30 | + # Calculate column widths |
| 31 | + max_name = max(len(name) for name, _ in items) |
| 32 | + max_source = max(len(defn.env_var_name or defn.source) for _, defn in items) |
| 33 | + |
| 34 | + # Print header |
| 35 | + header = ( |
| 36 | + click.style(f"{'Setting':<{max_name}}", bold=True) |
| 37 | + + " " |
| 38 | + + click.style(f"{'Source':<{max_source}}", bold=True) |
| 39 | + + " " |
| 40 | + + click.style("Value", bold=True) |
| 41 | + ) |
| 42 | + click.echo(header) |
| 43 | + click.secho("-" * (max_name + max_source + 10), dim=True) |
| 44 | + |
| 45 | + # Print each setting |
| 46 | + for name, defn in items: |
| 47 | + source_info = defn.env_var_name or defn.source |
| 48 | + value = defn.display_value() |
| 49 | + |
| 50 | + # Style based on source |
| 51 | + if defn.source == "env": |
| 52 | + source_styled = click.style(f"{source_info:<{max_source}}", fg="green") |
| 53 | + elif defn.source == "explicit": |
| 54 | + source_styled = click.style(f"{source_info:<{max_source}}", fg="cyan") |
| 55 | + else: |
| 56 | + source_styled = click.style(f"{source_info:<{max_source}}", dim=True) |
| 57 | + |
| 58 | + # Style secret values |
| 59 | + if defn.is_secret: |
| 60 | + value_styled = click.style(value, dim=True) |
| 61 | + else: |
| 62 | + value_styled = value |
| 63 | + |
| 64 | + click.echo(f"{name:<{max_name}} {source_styled} {value_styled}") |
0 commit comments