Skip to content

Commit

Permalink
Add --output-file flag to get cluster action
Browse files Browse the repository at this point in the history
  • Loading branch information
allenporter committed Dec 22, 2023
1 parent 82b660e commit e08f50d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
13 changes: 9 additions & 4 deletions flux_local/tool/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

from typing import Generator, Any

import sys
from typing import TextIO
import yaml


PADDING = 4


Expand Down Expand Up @@ -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:
Expand All @@ -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
)
22 changes: 17 additions & 5 deletions flux_local/tool/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"]
Expand All @@ -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:
Expand Down

0 comments on commit e08f50d

Please sign in to comment.