From e08f50d2de119b8fca01f5d49cf6dc8649ef5c05 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Fri, 22 Dec 2023 18:59:16 +0000 Subject: [PATCH] Add --output-file flag to get cluster action --- flux_local/tool/format.py | 13 +++++++++---- flux_local/tool/get.py | 22 +++++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/flux_local/tool/format.py b/flux_local/tool/format.py index 33178a62..cf5fa705 100644 --- a/flux_local/tool/format.py +++ b/flux_local/tool/format.py @@ -2,8 +2,11 @@ from typing import Generator, Any +import sys +from typing import TextIO import yaml + PADDING = 4 @@ -47,10 +50,10 @@ def format(self, data: list[dict[str, Any]]) -> Generator[str, None, None]: for result in format_columns(cols, rows): yield result - def print(self, data: list[dict[str, Any]]) -> None: + def print(self, data: list[dict[str, Any]], file: TextIO = sys.stdout) -> None: """Output the data objects.""" for result in self.format(data): - print(result) + print(result, file=file) class YamlFormatter: @@ -63,6 +66,8 @@ def format(self, data: list[dict[str, Any]]) -> Generator[str, None, None]: ): yield line - def print(self, data: list[dict[str, Any]]) -> None: + def print(self, data: list[dict[str, Any]], file: TextIO = sys.stdout) -> None: """Format the data objects.""" - print(yaml.dump_all(data, sort_keys=False, explicit_start=True), end="") + print( + yaml.dump_all(data, sort_keys=False, explicit_start=True), end="", file=file + ) diff --git a/flux_local/tool/get.py b/flux_local/tool/get.py index cab92628..2ecf50c6 100644 --- a/flux_local/tool/get.py +++ b/flux_local/tool/get.py @@ -171,12 +171,19 @@ def register( default="diff", help="Output format of the command", ) + args.add_argument( + "--output-file", + type=str, + default="/dev/stdout", + help="Output file for the results of the command", + ) args.set_defaults(cls=cls) return args async def run( # type: ignore[no-untyped-def] self, output: str, + output_file: str, enable_images: bool, **kwargs, # pylint: disable=unused-argument ) -> None: @@ -216,7 +223,8 @@ async def run( # type: ignore[no-untyped-def] ) helm_content.update_manifest(manifest) - YamlFormatter().print([manifest.compact_dict()]) + with open(output_file, "w") as file: + YamlFormatter().print([manifest.compact_dict()], file=file) return cols = ["path", "kustomizations"] @@ -226,11 +234,15 @@ async def run( # type: ignore[no-untyped-def] value["kustomizations"] = len(cluster.kustomizations) results.append(value) - if not results: - print(selector.not_found("flux cluster Kustmization", query.cluster)) - return + with open(output_file, "w") as file: + if not results: + print( + selector.not_found("flux cluster Kustmization", query.cluster), + file=file, + ) + return - PrintFormatter(cols).print(results) + PrintFormatter(cols).print(results, file=file) class GetAction: