Skip to content

Commit

Permalink
chore: Refactor digest targets into a private helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
mislavmandaricaxilis committed Dec 11, 2024
1 parent 6cf867d commit 7d58390
Showing 1 changed file with 31 additions and 43 deletions.
74 changes: 31 additions & 43 deletions oci/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,53 @@ def _write_nl_seperated_file(name, kind, elems, forwarded_kwargs):
)
return label

def oci_image_index(name, **kwargs):
"""Macro wrapper around [oci_image_index_rule](#oci_image_index_rule).
Produces a target `[name].digest`, whose default output is a file containing the sha256 digest of the resulting image.
This is the same output as for the `oci_image` macro.
Args:
name: name of resulting oci_image_index_rule
**kwargs: other named arguments to [oci_image_index_rule](#oci_image_index_rule) and
[common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
forwarded_kwargs = propagate_common_rule_attributes(kwargs)

oci_image_index_rule(
name = name,
**kwargs
)

def _digest(name, **kwargs):
# `oci_image_rule` and `oci_image_index_rule` produce a directory as default output.
# Label for the [name]/index.json file
directory_path(
name = "_{}_index_json".format(name),
directory = name,
path = "index.json",
**forwarded_kwargs
**kwargs
)

copy_file(
name = "_{}_index_json_cp".format(name),
src = "_{}_index_json".format(name),
out = "_{}_index.json".format(name),
**forwarded_kwargs
**kwargs
)

# Matches the [name].digest target produced by rules_docker container_image
jq(
name = name + ".digest",
args = ["--raw-output"],
srcs = ["_{}_index.json".format(name)],
filter = """.manifests[0].digest""",
out = name + ".json.sha256", # path chosen to match rules_docker for easy migration
**kwargs
)

def oci_image_index(name, **kwargs):
"""Macro wrapper around [oci_image_index_rule](#oci_image_index_rule).
Produces a target `[name].digest`, whose default output is a file containing the sha256 digest of the resulting image.
This is the same output as for the `oci_image` macro.
Args:
name: name of resulting oci_image_index_rule
**kwargs: other named arguments to [oci_image_index_rule](#oci_image_index_rule) and
[common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
forwarded_kwargs = propagate_common_rule_attributes(kwargs)

oci_image_index_rule(
name = name,
**kwargs
)

_digest(
name = name,
**forwarded_kwargs
)

Expand Down Expand Up @@ -200,29 +209,8 @@ def oci_image(
**kwargs
)

# `oci_image_rule` produces a directory as default output.
# Label for the [name]/index.json file
directory_path(
name = "_{}_index_json".format(name),
directory = name,
path = "index.json",
**forwarded_kwargs
)

copy_file(
name = "_{}_index_json_cp".format(name),
src = "_{}_index_json".format(name),
out = "_{}_index.json".format(name),
**forwarded_kwargs
)

# Matches the [name].digest target produced by rules_docker container_image
jq(
name = name + ".digest",
args = ["--raw-output"],
srcs = ["_{}_index.json".format(name)],
filter = """.manifests[0].digest""",
out = name + ".json.sha256", # path chosen to match rules_docker for easy migration
_digest(
name = name,
**forwarded_kwargs
)

Expand Down

0 comments on commit 7d58390

Please sign in to comment.