Skip to content
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

List specific config section and its values using the cli #28334

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion airflow/cli/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,10 @@ def string_lower_type(val):
("option",),
help="The option name",
)
ARG_OPTIONAL_SECTION = Arg(
("--section",),
help="The section name",
)

# kubernetes cleanup-pods
ARG_NAMESPACE = Arg(
Expand Down Expand Up @@ -1823,7 +1827,7 @@ class GroupCommand(NamedTuple):
name="list",
help="List options for the configuration",
func=lazy_load_command("airflow.cli.commands.config_command.show_config"),
args=(ARG_COLOR, ARG_VERBOSE),
args=(ARG_OPTIONAL_SECTION, ARG_COLOR, ARG_VERBOSE),
),
)

Expand Down
2 changes: 1 addition & 1 deletion airflow/cli/commands/config_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
def show_config(args):
"""Show current application configuration."""
with io.StringIO() as output:
conf.write(output)
conf.write(output, section=args.section)
code = output.getvalue()
if should_use_colors(args):
code = pygments.highlight(code=code, formatter=get_terminal_formatter(), lexer=IniLexer())
Expand Down
15 changes: 11 additions & 4 deletions airflow/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,9 @@ def getsection(self, section: str) -> ConfigOptionsDictType | None:
_section[key] = False
return _section

def write(self, fp: IO, space_around_delimiters: bool = True): # type: ignore[override]
def write( # type: ignore[override]
self, fp: IO, space_around_delimiters: bool = True, section: str | None = None
) -> None:
# This is based on the configparser.RawConfigParser.write method code to add support for
# reading options from environment variables.
# Various type ignores below deal with less-than-perfect RawConfigParser superclass typing
Expand All @@ -942,9 +944,14 @@ def write(self, fp: IO, space_around_delimiters: bool = True): # type: ignore[o
self._write_section( # type: ignore[attr-defined]
fp, self.default_section, self._defaults.items(), delimiter # type: ignore[attr-defined]
)
for section in self._sections: # type: ignore[attr-defined]
item_section: ConfigOptionsDictType = self.getsection(section) # type: ignore[assignment]
self._write_section(fp, section, item_section.items(), delimiter) # type: ignore[attr-defined]
sections = (
{section: dict(self.getsection(section))} # type: ignore[arg-type]
if section
else self._sections # type: ignore[attr-defined]
)
for sect in sections:
item_section: ConfigOptionsDictType = self.getsection(sect) # type: ignore[assignment]
self._write_section(fp, sect, item_section.items(), delimiter) # type: ignore[attr-defined]

def as_dict(
self,
Expand Down
14 changes: 13 additions & 1 deletion tests/cli/commands/test_config_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ def setup_class(cls):
@mock.patch("airflow.cli.commands.config_command.conf")
def test_cli_show_config_should_write_data(self, mock_conf, mock_stringio):
config_command.show_config(self.parser.parse_args(["config", "list", "--color", "off"]))
mock_conf.write.assert_called_once_with(mock_stringio.return_value.__enter__.return_value)
mock_conf.write.assert_called_once_with(
mock_stringio.return_value.__enter__.return_value, section=None
)

@mock.patch("airflow.cli.commands.config_command.io.StringIO")
@mock.patch("airflow.cli.commands.config_command.conf")
def test_cli_show_config_should_write_data_specific_section(self, mock_conf, mock_stringio):
config_command.show_config(
self.parser.parse_args(["config", "list", "--section", "core", "--color", "off"])
)
mock_conf.write.assert_called_once_with(
mock_stringio.return_value.__enter__.return_value, section="core"
)

@conf_vars({("core", "testkey"): "test_value"})
def test_cli_show_config_should_display_key(self):
Expand Down