Skip to content

Instantly share code, notes, and snippets.

@lsjostro
Last active February 7, 2023 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsjostro/6c98f6b499127b0d115dd44d9909aa10 to your computer and use it in GitHub Desktop.
Save lsjostro/6c98f6b499127b0d115dd44d9909aa10 to your computer and use it in GitHub Desktop.
buf.build protoc local exec using container images from custom registry
#!/usr/bin/env bash
# Symlink this script as ./protoc-gen-<plugin-name>.
# container images should be built as protoc-gen-<plugin-name>
# make sure `buf` is installed and available in your PATH as well
# as this script and the symlinks. Works both with docker and podman.
# podman is preferred if both are available. (podman is preferred in CI environments)
#
# Example buf.gen.yaml:
# ---
# version: v1
# plugins:
# - plugin: go # symlink protoc-gen-go to this script
# out: internal/pb
# opt:
# - paths=source_relative
#
# export PROTOC_PLUGIN_REGISTRY environment variable to use a
# custom container registry (e.g. registry.gitlab.com/my-org/protoc-plugins), defaults to localhost.
# export DEBUG environment variable to enable debug logging
#
# Upsteam protoc plugin images can be found here: https://github.com/bufbuild/plugins
set -fe -o pipefail
# log executions if debug is enabled
if [[ -n "${DEBUG}" ]]; then
set -x
fi
_log() {
local -r prefix="$1"
shift
echo "[${prefix}$(date -u "+%Y-%m-%d %H:%M:%S %Z")] ""$*" >&2
}
info() {
_log "INFO " "$*"
}
error() {
_log "ERROR " "$*"
}
check_container_runtime() {
if command -v podman &>/dev/null; then
export RUNTIME="podman"
elif command -v docker &>/dev/null; then
export RUNTIME="docker"
else
error "No container runtime found"
exit 1
fi
}
use_container_image_registry() {
if [[ -n "${PROTOC_PLUGIN_REGISTRY}" ]]; then
export PROTOC_PLUGIN="${PROTOC_PLUGIN_REGISTRY}/$1"
else
export PROTOC_PLUGIN="$1"
fi
}
main() {
check_container_runtime
use_container_image_registry "$1"
info "Running ${RUNTIME} run ${PROTOC_PLUGIN}"
exec $RUNTIME run -i "${PROTOC_PLUGIN}"
}
_self=${0##*/}
main "$_self"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment