Skip to content

Commit

Permalink
Add --output-file for build and diff and use in action (#459)
Browse files Browse the repository at this point in the history
Add the --output-file command for `diff` and `build`. Also update the
diff action to use the output file command as in the example in the
feature request.

Fixes #455
  • Loading branch information
allenporter authored Dec 22, 2023
1 parent ca7728b commit 1f97b1b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 43 deletions.
15 changes: 10 additions & 5 deletions action/diff/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ runs:
with:
ref: ${{ inputs.live-branch }}
path: live
- name: flux-local diff
- name: Diff Resources
id: flux_diff
run: |
delimiter="$(openssl rand -hex 8)"
echo "diff<<${delimiter}" >> $GITHUB_OUTPUT
if [[ "${{ inputs.resource }}" == "helmrelease" ]]; then
extra_flags="--api-versions=${{ inputs.api-versions}}"
else
Expand All @@ -106,7 +104,14 @@ runs:
--all-namespaces \
--kustomize-build-flags="${{ inputs.kustomize-build-flags }}" \
--sources "${{ inputs.sources }}" \
${extra_flags} \
>> $GITHUB_OUTPUT
--output-file diff.patch \
${extra_flags}
shell: bash
- name: Generate Diff output
id: flux_diff_output
run: |
delimiter="$(openssl rand -hex 8)"
echo "diff<<${delimiter}" >> $GITHUB_OUTPUT
cat diff.patch >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
shell: bash
28 changes: 18 additions & 10 deletions flux_local/tool/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def register(
action=BooleanOptionalAction,
help="Enable use of HelmRelease inflation",
)
args.add_argument(
"--output-file",
type=str,
default="/dev/stdout",
help="Output file for the results of the command",
)
# pylint: disable=duplicate-code
selector.add_common_flags(args)
selector.add_helm_options_flags(args)
Expand All @@ -59,6 +65,7 @@ async def run( # type: ignore[no-untyped-def]
enable_helm: bool,
skip_crds: bool,
skip_secrets: bool,
output_file: str,
**kwargs, # pylint: disable=unused-argument
) -> None:
"""Async Action implementation."""
Expand Down Expand Up @@ -97,14 +104,15 @@ async def run( # type: ignore[no-untyped-def]
helm_options,
)

keys = list(content.content)
keys.sort()
for key in keys:
for line in content.content[key]:
print(line)
with open(output_file, "w") as file:
keys = list(content.content)
keys.sort()
for key in keys:
for line in content.content[key]:
print(line, file=file)

keys = list(helm_content.content)
keys.sort()
for key in keys:
for line in helm_content.content[key]:
print(line)
keys = list(helm_content.content)
keys.sort()
for key in keys:
for line in helm_content.content[key]:
print(line, file=file)
72 changes: 44 additions & 28 deletions flux_local/tool/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ def register(
),
),
)
args.add_argument(
"--output-file",
type=str,
default="/dev/stdout",
help="Output file for the results of the command",
)
selector.add_ks_selector_flags(args)
add_diff_flags(args)
args.set_defaults(cls=cls)
Expand All @@ -260,6 +266,7 @@ async def run( # type: ignore[no-untyped-def]
unified: int,
strip_attrs: list[str] | None,
limit_bytes: int,
output_file: str,
**kwargs, # pylint: disable=unused-argument
) -> None:
"""Async Action implementation."""
Expand All @@ -285,19 +292,20 @@ async def run( # type: ignore[no-untyped-def]
return

_LOGGER.debug("Diffing content")
if output == "yaml":
result = perform_yaml_diff(orig_content, content, unified, limit_bytes)
for line in result:
print(line)
elif external_diff := os.environ.get("DIFF"):
async for line in perform_external_diff(
shlex.split(external_diff), orig_content, content, limit_bytes
):
print(line)
else:
result = perform_object_diff(orig_content, content, unified, limit_bytes)
for line in result:
print(line)
with open(output_file, "w") as file:
if output == "yaml":
result = perform_yaml_diff(orig_content, content, unified, limit_bytes)
for line in result:
print(line, file=file)
elif external_diff := os.environ.get("DIFF"):
async for line in perform_external_diff(
shlex.split(external_diff), orig_content, content, limit_bytes
):
print(line, file=file)
else:
result = perform_object_diff(orig_content, content, unified, limit_bytes)
for line in result:
print(line, file=file)


class DiffHelmReleaseAction:
Expand All @@ -320,6 +328,12 @@ def register(
),
),
)
args.add_argument(
"--output-file",
type=str,
default="/dev/stdout",
help="Output file for the results of the command",
)
selector.add_hr_selector_flags(args)
selector.add_helm_options_flags(args)
add_diff_flags(args)
Expand All @@ -332,6 +346,7 @@ async def run( # type: ignore[no-untyped-def]
unified: int,
strip_attrs: list[str] | None,
limit_bytes: int,
output_file: str,
**kwargs, # pylint: disable=unused-argument
) -> None:
"""Async Action implementation."""
Expand Down Expand Up @@ -393,21 +408,22 @@ async def run( # type: ignore[no-untyped-def]
),
)

if output == "yaml":
for line in perform_yaml_diff(
orig_helm_content, helm_content, unified, limit_bytes
):
print(line)
elif external_diff := os.environ.get("DIFF"):
async for line in perform_external_diff(
shlex.split(external_diff), orig_helm_content, helm_content, limit_bytes
):
print(line)
else:
for line in perform_object_diff(
orig_helm_content, helm_content, unified, limit_bytes
):
print(line)
with open(output_file, "w") as file:
if output == "yaml":
for line in perform_yaml_diff(
orig_helm_content, helm_content, unified, limit_bytes
):
print(line, file=file)
elif external_diff := os.environ.get("DIFF"):
async for line in perform_external_diff(
shlex.split(external_diff), orig_helm_content, helm_content, limit_bytes
):
print(line, file=file)
else:
for line in perform_object_diff(
orig_helm_content, helm_content, unified, limit_bytes
):
print(line, file=file)


class DiffAction:
Expand Down

0 comments on commit 1f97b1b

Please sign in to comment.