Skip to content

Commit

Permalink
feat: support passing label to load command (#441)
Browse files Browse the repository at this point in the history
Co-authored-by: Sahin Yort <[email protected]>
  • Loading branch information
jchorl and thesayyn authored Jan 2, 2024
1 parent 8f64741 commit e420a13
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/tarball.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion oci/private/tarball.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ attrs = {
allow_single_file = [".txt"],
mandatory = True,
),
"command": attr.label(
doc = """\
target for a container cli tool (i.e. docker or podman or other) that will be used to load the image into the local engine when using 'bazel run //my/image'.",
""",
allow_single_file = True,
mandatory = False,
executable = True,
cfg = "target",
),
"_run_template": attr.label(
default = Label("//oci/private:tarball_run.sh.tpl"),
doc = """ \
Expand Down Expand Up @@ -90,12 +99,16 @@ def _tarball_impl(ctx):
output = exe,
substitutions = {
"{{image_path}}": tarball.short_path,
"{{command}}": ctx.file.command.path if ctx.file.command else "",
},
is_executable = True,
)
runfiles = [tarball]
if ctx.file.command:
runfiles.append(ctx.file.command)

return [
DefaultInfo(files = depset([tarball]), runfiles = ctx.runfiles(files = [tarball]), executable = exe),
DefaultInfo(files = depset([tarball]), runfiles = ctx.runfiles(files = runfiles), executable = exe),
]

oci_tarball = rule(
Expand Down
6 changes: 4 additions & 2 deletions oci/private/tarball_run.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
set -o pipefail -o errexit -o nounset

readonly IMAGE="{{image_path}}"
if command -v docker &> /dev/null; then
if [ -e "{{command}}" ]; then
CONTAINER_CLI="{{command}}"
elif command -v docker &> /dev/null; then
CONTAINER_CLI="docker"
elif command -v podman &> /dev/null; then
CONTAINER_CLI="podman"
else
echo >&2 "Neither docker or podman could be found."
echo >&2 "If you wish to use another container runtime, please comment on https://github.com/bazel-contrib/rules_oci/issues/295."
echo >&2 "If you wish to use another container runtime, you can pass an executable to the 'command' attribute."
exit 1
fi

Expand Down

0 comments on commit e420a13

Please sign in to comment.