Skip to content

Commit

Permalink
Add settings --schema support (#1093)
Browse files Browse the repository at this point in the history
  • Loading branch information
cidrblock authored Mar 17, 2022
1 parent 51486d3 commit bf2c852
Show file tree
Hide file tree
Showing 21 changed files with 550 additions and 11 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ repos:
- astroid == 2.9.0
- ansible-runner
- jinja2
- jsonschema
- libtmux
- onigurumacffi
- pytest
Expand Down
7 changes: 7 additions & 0 deletions docs/changelog-fragments.d/1093.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Added the ability to produce a json schema file for the settings file.

```bash
ansible-navigator settings --schema
```

-- by {user}`cidrblock`
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ ignore_missing_imports = true
# No type hints as of version 2.1.2
ignore_missing_imports = true

[mypy-jsonschema.*]
# No type hints as of version 4.4.0
ignore_missing_imports = true

[mypy-libtmux]
# No type hints as of version 0.10.3
ignore_missing_imports = true
Expand Down
8 changes: 8 additions & 0 deletions src/ansible_navigator/actions/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from dataclasses import asdict
from typing import Tuple

from ansible_navigator.configuration_subsystem.definitions import Constants
from ..action_base import ActionBase
from ..action_defs import RunStdoutReturn
from ..app_public import AppPublic
from ..configuration_subsystem import PresentableSettingsEntries
from ..configuration_subsystem import PresentableSettingsEntry
from ..configuration_subsystem import to_presentable
from ..configuration_subsystem import to_schema
from ..content_defs import ContentView
from ..content_defs import SerializationFormat
from ..steps import StepType
Expand Down Expand Up @@ -116,6 +118,12 @@ def run_stdout(self) -> RunStdoutReturn:
:returns: RunStdoutReturn
"""
self._logger.debug("settings requested in stdout mode")
if self._args.entry("settings_schema").value.source is not Constants.DEFAULT_CFG:
if self._args.settings_schema == "json":
schema = to_schema(self._args)
print(schema)
return RunStdoutReturn(message="", return_code=0)

self._settings = to_presentable(self._args)
info_dump = serialize(
content=list(self._settings),
Expand Down
2 changes: 2 additions & 0 deletions src/ansible_navigator/configuration_subsystem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .defs_presentable import PresentableSettingsEntry
from .navigator_configuration import NavigatorConfiguration
from .transform import to_presentable
from .transform import to_schema


__all__ = (
Expand All @@ -19,4 +20,5 @@
"PresentableSettingsEntries",
"SettingsEntry",
"to_presentable",
"to_schema",
)
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class CliParameters:
"""An object to hold the CLI parameters."""

action: Optional[str] = None
const: Optional[Union[bool, str]] = None
long_override: Optional[str] = None
nargs: Optional[str] = None
positional: bool = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,22 @@ class Internals:
),
value=SettingsEntryValue(),
),
SettingsEntry(
name="settings_schema",
choices=["json"],
cli_parameters=CliParameters(
short="--ss",
long_override="--schema",
const="json",
nargs="?",
),
settings_file_path_override="settings.schema",
short_description=(
"Generate a schema for the settings file. ('json'= draft-07 JSON Schema)"
),
subcommands=["settings"],
value=SettingsEntryValue(default="json"),
),
SettingsEntry(
name="time_zone",
cli_parameters=CliParameters(short="--tz"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,13 @@ def exec_shell(
return self._true_or_false(entry, config)

@_post_processor
def _help_for_command(
def _forced_stdout(
self,
entry: SettingsEntry,
config: ApplicationConfiguration,
subcommand: str,
) -> PostProcessorReturn:
"""Post process help_xxxx
"""Force mode stdout for a settings parameter.
:param entry: The current settings entry
:param config: The full application configuration
Expand All @@ -452,11 +452,11 @@ def _help_for_command(
messages.append(LogMessage(level=logging.DEBUG, message=message))
return messages, exit_messages

help_builder = partialmethod(_help_for_command, subcommand="builder")
help_config = partialmethod(_help_for_command, subcommand="config")
help_doc = partialmethod(_help_for_command, subcommand="doc")
help_inventory = partialmethod(_help_for_command, subcommand="inventory")
help_playbook = partialmethod(_help_for_command, subcommand="run")
help_builder = partialmethod(_forced_stdout, subcommand="builder")
help_config = partialmethod(_forced_stdout, subcommand="config")
help_doc = partialmethod(_forced_stdout, subcommand="doc")
help_inventory = partialmethod(_forced_stdout, subcommand="inventory")
help_playbook = partialmethod(_forced_stdout, subcommand="run")

@staticmethod
@_post_processor
Expand Down Expand Up @@ -766,6 +766,28 @@ def pull_arguments(
entry.value.current = flatten_list(entry.value.current)
return messages, exit_messages

@_post_processor
def settings_schema(
self,
entry: SettingsEntry,
config: ApplicationConfiguration,
) -> PostProcessorReturn:
"""Force mode stdout for schema parameter.
:param entry: The current settings entry
:param config: The full application configuration
:returns: An instance of the standard post process return object
"""
messages: List[LogMessage] = []
exit_messages: List[ExitMessage] = []

if entry.value.source is not C.DEFAULT_CFG and config.app == "settings":
mode = Mode.STDOUT
self._requested_mode.append(ModeChangeRequest(entry=entry.name, mode=mode))
message = message = f"`{entry.name} requesting mode {mode.value}"
messages.append(LogMessage(level=logging.DEBUG, message=message))
return messages, exit_messages

@staticmethod
@_post_processor
def set_environment_variable(
Expand Down
2 changes: 2 additions & 0 deletions src/ansible_navigator/configuration_subsystem/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def generate_argument(entry) -> Tuple[Any, Any, Dict[str, Any]]:
kwargs["dest"] = entry.name
if entry.cli_parameters.nargs is not None:
kwargs["nargs"] = entry.cli_parameters.nargs
if entry.cli_parameters.const is not None:
kwargs["const"] = entry.cli_parameters.const

if entry.cli_parameters.metavar is not None:
kwargs["metavar"] = entry.cli_parameters.metavar
Expand Down
Loading

0 comments on commit bf2c852

Please sign in to comment.