-
Notifications
You must be signed in to change notification settings - Fork 176
fix(config_list): Add JSON output #994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
changelog.d/20241028_145054_salome.voltz_refacto_json_output.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ### Added | ||
|
|
||
| - `ggshield config list` command now supports the `--json` option, allowing output in JSON format. | ||
|
|
||
| ### Changed | ||
|
|
||
| - `ggshield api-status --json` now also outputs the instance URL. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "title": "ggshield config list", | ||
| "type": "object", | ||
| "properties": { | ||
| "instances": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "object", | ||
| "properties": { | ||
| "instance_name": { | ||
| "type": "string", | ||
| "description": "Name of the GitGuardian instance" | ||
| }, | ||
| "default_token_lifetime": { | ||
| "type": ["string", "null"], | ||
| "description": "Default token lifetime" | ||
| }, | ||
| "workspace_id": { | ||
| "type": ["number", "string"], | ||
| "description": "Workspace ID" | ||
| }, | ||
| "url": { | ||
| "type": "string", | ||
| "format": "uri", | ||
| "description": "URL of the GitGuardian instance" | ||
| }, | ||
| "token": { | ||
| "type": "string", | ||
| "description": "API Token for the instance" | ||
| }, | ||
| "token_name": { | ||
| "type": "string", | ||
| "description": "Name of the token" | ||
| }, | ||
| "expiry": { | ||
| "type": "string", | ||
| "description": "Expiration date of the token" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "instance_name", | ||
| "workspace_id", | ||
| "url", | ||
| "token", | ||
| "token_name", | ||
| "expiry" | ||
| ] | ||
| } | ||
| }, | ||
| "global_values": { | ||
| "type": "object", | ||
| "properties": { | ||
| "instance": { | ||
| "type": ["string", "null"], | ||
| "description": "Name of the default GitGuardian instance" | ||
| }, | ||
| "default_token_lifetime": { | ||
| "type": ["string", "null"], | ||
| "description": "Default token lifetime" | ||
| } | ||
| }, | ||
| "required": ["instance", "default_token_lifetime"] | ||
| } | ||
| }, | ||
| "required": ["instances", "global_values"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,112 @@ | ||
| from typing import Any, Tuple | ||
| import json | ||
| from dataclasses import dataclass, field | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
| import click | ||
|
|
||
| from ggshield.cmd.utils.common_options import add_common_options | ||
| from ggshield.cmd.utils.common_options import ( | ||
| add_common_options, | ||
| json_option, | ||
| text_json_format_option, | ||
| ) | ||
| from ggshield.cmd.utils.context_obj import ContextObj | ||
| from ggshield.core.config.auth_config import InstanceConfig | ||
|
|
||
| from .constants import DATETIME_FORMAT, FIELDS | ||
|
|
||
|
|
||
| @dataclass | ||
| class InstanceInfo: | ||
| instance_name: str | ||
| default_token_lifetime: Optional[int] | ||
| workspace_id: Any | ||
| url: str | ||
| token: str | ||
| token_name: str | ||
| expiry: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigData: | ||
| instances: List[InstanceInfo] = field(default_factory=list) | ||
| global_values: Dict[str, Any] = field(default_factory=dict) | ||
|
|
||
| def as_dict(self) -> Dict[str, Any]: | ||
| return { | ||
| "instances": [instance.__dict__ for instance in self.instances], | ||
| "global_values": self.global_values, | ||
| } | ||
|
|
||
|
|
||
| def get_instance_info( | ||
| instance: InstanceConfig, default_token_lifetime: Any | ||
| ) -> InstanceInfo: | ||
| """Helper function to extract instance information.""" | ||
| instance_name = instance.name or instance.url | ||
| account = instance.account | ||
|
|
||
| if account is not None: | ||
| workspace_id = account.workspace_id | ||
| token = account.token | ||
| token_name = account.token_name | ||
| expire_at = account.expire_at | ||
| expiry = expire_at.strftime(DATETIME_FORMAT) if expire_at else "never" | ||
| else: | ||
| workspace_id = token = token_name = expiry = "not set" | ||
|
|
||
| _default_token_lifetime = instance.default_token_lifetime or default_token_lifetime | ||
|
|
||
| return InstanceInfo( | ||
| instance_name=instance_name, | ||
| default_token_lifetime=_default_token_lifetime, | ||
| workspace_id=workspace_id, | ||
| url=instance.url, | ||
| token=token, | ||
| token_name=token_name, | ||
| expiry=expiry, | ||
| ) | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.pass_context | ||
| @json_option | ||
| @text_json_format_option | ||
| @add_common_options() | ||
| def config_list_cmd(ctx: click.Context, **kwargs: Any) -> int: | ||
| """ | ||
| Print the list of configuration keys and values. | ||
| """ | ||
| config = ContextObj.get(ctx).config | ||
| ctx_obj = ContextObj.get(ctx) | ||
| config = ctx_obj.config | ||
| default_token_lifetime = config.auth_config.default_token_lifetime | ||
|
|
||
| message_lines = [] | ||
|
|
||
| def add_entries(*entries: Tuple[str, Any]): | ||
| for key, value in entries: | ||
| message_lines.append(f"{key}: {value}") | ||
|
|
||
| # List global values | ||
| for field in FIELDS.values(): | ||
| config_obj = config.auth_config if field.auth_config else config.user_config | ||
| value = getattr(config_obj, field.name) | ||
| add_entries((field.name, value)) | ||
| message_lines.append("") | ||
|
|
||
| # List instance values | ||
| for instance in config.auth_config.instances: | ||
| instance_name = instance.name or instance.url | ||
|
|
||
| if instance.account is not None: | ||
| workspace_id = instance.account.workspace_id | ||
| token = instance.account.token | ||
| token_name = instance.account.token_name | ||
| expire_at = instance.account.expire_at | ||
| expiry = ( | ||
| expire_at.strftime(DATETIME_FORMAT) | ||
| if expire_at is not None | ||
| else "never" | ||
| ) | ||
| else: | ||
| workspace_id = token = token_name = expiry = "not set" | ||
|
|
||
| _default_token_lifetime = ( | ||
| instance.default_token_lifetime | ||
| if instance.default_token_lifetime is not None | ||
| else default_token_lifetime | ||
| config_data = ConfigData() | ||
| for config_field in FIELDS.values(): | ||
| config_obj = ( | ||
| config.auth_config if config_field.auth_config else config.user_config | ||
| ) | ||
| value = getattr(config_obj, config_field.name) | ||
| config_data.global_values[config_field.name] = value | ||
|
|
||
| message_lines.append(f"[{instance_name}]") | ||
| add_entries( | ||
| ("default_token_lifetime", _default_token_lifetime), | ||
| ("workspace_id", workspace_id), | ||
| ("url", instance.url), | ||
| ("token", token), | ||
| ("token_name", token_name), | ||
| ("expiry", expiry), | ||
| ) | ||
| config_data.instances = [ | ||
| get_instance_info(instance, default_token_lifetime) | ||
| for instance in config.auth_config.instances | ||
| ] | ||
|
|
||
| if ctx_obj.use_json: | ||
| click.echo(json.dumps(config_data.as_dict())) | ||
| else: | ||
| message_lines = [ | ||
| f"{key}: {value}" for key, value in config_data.global_values.items() | ||
| ] | ||
| message_lines.append("") | ||
| for instance in config_data.instances: | ||
| message_lines.append(f"[{instance.instance_name}]") | ||
| for key, value in instance.__dict__.items(): | ||
| if key != "instance_name": | ||
| message_lines.append(f"{key}: {value}") | ||
| message_lines.append("") | ||
|
|
||
| click.echo("\n".join(message_lines).strip()) | ||
|
|
||
| click.echo("\n".join(message_lines).strip()) | ||
| return 0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.