|
1 | | -from typing import Any, Tuple |
| 1 | +import json |
| 2 | +from dataclasses import dataclass, field |
| 3 | +from typing import Any, Dict, List, Optional |
2 | 4 |
|
3 | 5 | import click |
4 | 6 |
|
5 | | -from ggshield.cmd.utils.common_options import add_common_options |
| 7 | +from ggshield.cmd.utils.common_options import ( |
| 8 | + add_common_options, |
| 9 | + json_option, |
| 10 | + text_json_format_option, |
| 11 | +) |
6 | 12 | from ggshield.cmd.utils.context_obj import ContextObj |
| 13 | +from ggshield.core.config.auth_config import InstanceConfig |
7 | 14 |
|
8 | 15 | from .constants import DATETIME_FORMAT, FIELDS |
9 | 16 |
|
10 | 17 |
|
| 18 | +@dataclass |
| 19 | +class InstanceInfo: |
| 20 | + instance_name: str |
| 21 | + default_token_lifetime: Optional[int] |
| 22 | + workspace_id: Any |
| 23 | + url: str |
| 24 | + token: str |
| 25 | + token_name: str |
| 26 | + expiry: str |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class ConfigData: |
| 31 | + instances: List[InstanceInfo] = field(default_factory=list) |
| 32 | + global_values: Dict[str, Any] = field(default_factory=dict) |
| 33 | + |
| 34 | + def as_dict(self) -> Dict[str, Any]: |
| 35 | + return { |
| 36 | + "instances": [instance.__dict__ for instance in self.instances], |
| 37 | + "global_values": self.global_values, |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +def get_instance_info( |
| 42 | + instance: InstanceConfig, default_token_lifetime: Any |
| 43 | +) -> InstanceInfo: |
| 44 | + """Helper function to extract instance information.""" |
| 45 | + instance_name = instance.name or instance.url |
| 46 | + account = instance.account |
| 47 | + |
| 48 | + if account is not None: |
| 49 | + workspace_id = account.workspace_id |
| 50 | + token = account.token |
| 51 | + token_name = account.token_name |
| 52 | + expire_at = account.expire_at |
| 53 | + expiry = expire_at.strftime(DATETIME_FORMAT) if expire_at else "never" |
| 54 | + else: |
| 55 | + workspace_id = token = token_name = expiry = "not set" |
| 56 | + |
| 57 | + _default_token_lifetime = instance.default_token_lifetime or default_token_lifetime |
| 58 | + |
| 59 | + return InstanceInfo( |
| 60 | + instance_name=instance_name, |
| 61 | + default_token_lifetime=_default_token_lifetime, |
| 62 | + workspace_id=workspace_id, |
| 63 | + url=instance.url, |
| 64 | + token=token, |
| 65 | + token_name=token_name, |
| 66 | + expiry=expiry, |
| 67 | + ) |
| 68 | + |
| 69 | + |
11 | 70 | @click.command() |
12 | 71 | @click.pass_context |
| 72 | +@json_option |
| 73 | +@text_json_format_option |
13 | 74 | @add_common_options() |
14 | 75 | def config_list_cmd(ctx: click.Context, **kwargs: Any) -> int: |
15 | 76 | """ |
16 | 77 | Print the list of configuration keys and values. |
17 | 78 | """ |
18 | | - config = ContextObj.get(ctx).config |
| 79 | + ctx_obj = ContextObj.get(ctx) |
| 80 | + config = ctx_obj.config |
19 | 81 | default_token_lifetime = config.auth_config.default_token_lifetime |
20 | 82 |
|
21 | | - message_lines = [] |
22 | | - |
23 | | - def add_entries(*entries: Tuple[str, Any]): |
24 | | - for key, value in entries: |
25 | | - message_lines.append(f"{key}: {value}") |
26 | | - |
27 | | - # List global values |
28 | | - for field in FIELDS.values(): |
29 | | - config_obj = config.auth_config if field.auth_config else config.user_config |
30 | | - value = getattr(config_obj, field.name) |
31 | | - add_entries((field.name, value)) |
32 | | - message_lines.append("") |
33 | | - |
34 | | - # List instance values |
35 | | - for instance in config.auth_config.instances: |
36 | | - instance_name = instance.name or instance.url |
37 | | - |
38 | | - if instance.account is not None: |
39 | | - workspace_id = instance.account.workspace_id |
40 | | - token = instance.account.token |
41 | | - token_name = instance.account.token_name |
42 | | - expire_at = instance.account.expire_at |
43 | | - expiry = ( |
44 | | - expire_at.strftime(DATETIME_FORMAT) |
45 | | - if expire_at is not None |
46 | | - else "never" |
47 | | - ) |
48 | | - else: |
49 | | - workspace_id = token = token_name = expiry = "not set" |
50 | | - |
51 | | - _default_token_lifetime = ( |
52 | | - instance.default_token_lifetime |
53 | | - if instance.default_token_lifetime is not None |
54 | | - else default_token_lifetime |
| 83 | + config_data = ConfigData() |
| 84 | + for config_field in FIELDS.values(): |
| 85 | + config_obj = ( |
| 86 | + config.auth_config if config_field.auth_config else config.user_config |
55 | 87 | ) |
| 88 | + value = getattr(config_obj, config_field.name) |
| 89 | + config_data.global_values[config_field.name] = value |
56 | 90 |
|
57 | | - message_lines.append(f"[{instance_name}]") |
58 | | - add_entries( |
59 | | - ("default_token_lifetime", _default_token_lifetime), |
60 | | - ("workspace_id", workspace_id), |
61 | | - ("url", instance.url), |
62 | | - ("token", token), |
63 | | - ("token_name", token_name), |
64 | | - ("expiry", expiry), |
65 | | - ) |
| 91 | + config_data.instances = [ |
| 92 | + get_instance_info(instance, default_token_lifetime) |
| 93 | + for instance in config.auth_config.instances |
| 94 | + ] |
| 95 | + |
| 96 | + if ctx_obj.use_json: |
| 97 | + click.echo(json.dumps(config_data.as_dict())) |
| 98 | + else: |
| 99 | + message_lines = [ |
| 100 | + f"{key}: {value}" for key, value in config_data.global_values.items() |
| 101 | + ] |
66 | 102 | message_lines.append("") |
| 103 | + for instance in config_data.instances: |
| 104 | + message_lines.append(f"[{instance.instance_name}]") |
| 105 | + for key, value in instance.__dict__.items(): |
| 106 | + if key != "instance_name": |
| 107 | + message_lines.append(f"{key}: {value}") |
| 108 | + message_lines.append("") |
| 109 | + |
| 110 | + click.echo("\n".join(message_lines).strip()) |
67 | 111 |
|
68 | | - click.echo("\n".join(message_lines).strip()) |
69 | 112 | return 0 |
0 commit comments