2022-04-05 16:58:07 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-03-28 22:48:58 +00:00
|
|
|
# Copyright (c) HashiCorp, Inc.
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-04-05 16:58:07 +00:00
|
|
|
|
|
|
|
readonly SCRIPT_NAME="$(basename ${BASH_SOURCE[0]})"
|
|
|
|
readonly SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
|
|
|
|
readonly SOURCE_DIR="$(dirname "$(dirname "${SCRIPT_DIR}")")"
|
|
|
|
readonly FN_DIR="$(dirname "${SCRIPT_DIR}")/functions"
|
|
|
|
|
|
|
|
source "${SCRIPT_DIR}/functions.sh"
|
|
|
|
|
|
|
|
unset CDPATH
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
cat <<-EOF
|
|
|
|
Usage: ${SCRIPT_NAME} [<options ...>]
|
|
|
|
|
|
|
|
Description:
|
2022-05-23 15:50:45 +00:00
|
|
|
Regenerates regenerates all Go files from protobuf definitions. In addition
|
|
|
|
to running the protoc generator it will also fixup build tags in the
|
|
|
|
generated code and regenerate mog outputs and RPC stubs.
|
2022-04-05 16:58:07 +00:00
|
|
|
|
|
|
|
Options:
|
|
|
|
-h | --help Print this help text.
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
function err_usage {
|
|
|
|
err "$1"
|
|
|
|
err ""
|
|
|
|
err "$(usage)"
|
|
|
|
}
|
|
|
|
|
|
|
|
function main {
|
|
|
|
while test $# -gt 0
|
|
|
|
do
|
|
|
|
case "$1" in
|
|
|
|
-h | --help )
|
|
|
|
usage
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2023-05-11 15:36:41 +00:00
|
|
|
# clear old ratelimit.tmp files
|
|
|
|
find . -name .ratelimit.tmp -delete
|
|
|
|
|
2023-01-04 16:07:02 +00:00
|
|
|
local mods=$(find . -name 'buf.gen.yaml' -exec dirname {} \; | sort)
|
|
|
|
for mod in $mods
|
2022-05-23 14:37:52 +00:00
|
|
|
do
|
2023-02-17 21:14:46 +00:00
|
|
|
status_stage "Generating protobuf module: $mod"
|
2022-05-23 14:37:52 +00:00
|
|
|
(
|
|
|
|
cd $mod
|
2023-02-17 21:14:46 +00:00
|
|
|
buf generate
|
2022-05-23 14:37:52 +00:00
|
|
|
for proto_file in $(buf ls-files)
|
|
|
|
do
|
|
|
|
postprocess_protobuf_code $proto_file
|
|
|
|
done
|
|
|
|
)
|
2022-04-05 16:58:07 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
status "Generated all protobuf Go files"
|
|
|
|
|
|
|
|
generate_mog_code
|
|
|
|
|
|
|
|
status "Generated all mog Go files"
|
|
|
|
|
2023-01-04 16:07:02 +00:00
|
|
|
generate_rate_limit_mappings $mods
|
|
|
|
|
|
|
|
status "Generated gRPC rate limit mapping file"
|
|
|
|
|
2023-06-26 12:53:55 +00:00
|
|
|
generate_protoset_file
|
|
|
|
|
|
|
|
status "Generated protoset file"
|
|
|
|
|
2022-04-05 16:58:07 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-05-23 14:37:52 +00:00
|
|
|
function postprocess_protobuf_code {
|
2022-04-05 16:58:07 +00:00
|
|
|
local proto_path="${1:-}"
|
|
|
|
if [[ -z "${proto_path}" ]]; then
|
|
|
|
err "missing protobuf path argument"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local proto_go_path="${proto_path%%.proto}.pb.go"
|
2023-01-11 14:39:10 +00:00
|
|
|
local proto_go_grpc_path="${proto_path%%.proto}_grpc.pb.go"
|
2022-04-05 16:58:07 +00:00
|
|
|
local proto_go_bin_path="${proto_path%%.proto}.pb.binary.go"
|
|
|
|
local proto_go_rpcglue_path="${proto_path%%.proto}.rpcglue.pb.go"
|
|
|
|
|
2022-05-23 14:37:52 +00:00
|
|
|
status_stage "Post-Processing generated files for ${proto_path}"
|
2022-04-05 16:58:07 +00:00
|
|
|
|
|
|
|
print_run protoc-go-inject-tag -input="${proto_go_path}" || {
|
|
|
|
err "Failed to run protoc-go-inject-tag for ${proto_path}"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
local build_tags
|
|
|
|
build_tags="$(head -n 2 "${proto_path}" | grep '^//go:build\|// +build' || true)"
|
|
|
|
if test -n "${build_tags}"; then
|
2023-01-11 14:39:10 +00:00
|
|
|
for file in "${proto_go_path}" "${proto_go_bin_path}" "${proto_go_grpc_path}"
|
2022-05-23 14:37:52 +00:00
|
|
|
do
|
2023-02-17 21:14:46 +00:00
|
|
|
if test -f "${file}" -a "$(head -n 2 ${file})" != "${build_tags}"
|
2022-05-23 14:37:52 +00:00
|
|
|
then
|
2023-01-11 14:39:10 +00:00
|
|
|
echo "Adding build tags to ${file}"
|
2022-05-23 14:37:52 +00:00
|
|
|
echo -e "${build_tags}\n" >> "${file}.new"
|
|
|
|
cat "${file}" >> "${file}.new"
|
|
|
|
mv "${file}.new" "${file}"
|
|
|
|
fi
|
|
|
|
done
|
2022-04-05 16:58:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# NOTE: this has to run after we fix up the build tags above
|
|
|
|
rm -f "${proto_go_rpcglue_path}"
|
2022-05-23 14:37:52 +00:00
|
|
|
print_run go run ${SOURCE_DIR}/internal/tools/proto-gen-rpc-glue/main.go -path "${proto_go_path}" || {
|
2022-04-05 16:58:07 +00:00
|
|
|
err "Failed to generate consul rpc glue outputs from ${proto_path}"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate_mog_code {
|
|
|
|
local mog_order
|
|
|
|
|
2023-02-17 21:14:46 +00:00
|
|
|
mog_order="$(go list -tags "${GOTAGS}" -deps ./proto/private/pb... | grep "consul/proto/private")"
|
2022-04-05 16:58:07 +00:00
|
|
|
|
|
|
|
for FULL_PKG in ${mog_order}; do
|
|
|
|
PKG="${FULL_PKG/#github.com\/hashicorp\/consul\/}"
|
|
|
|
status_stage "Generating ${PKG}/*.pb.go into ${PKG}/*.gen.go with mog"
|
|
|
|
find "$PKG" -name '*.gen.go' -delete
|
|
|
|
if [[ -n "${GOTAGS}" ]]; then
|
|
|
|
print_run mog -tags "${GOTAGS}" -source "./${PKG}/*.pb.go"
|
|
|
|
else
|
|
|
|
print_run mog -source "./${PKG}/*.pb.go"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:07:02 +00:00
|
|
|
function generate_rate_limit_mappings {
|
|
|
|
local flags=(
|
|
|
|
"-output ${SOURCE_DIR}/agent/grpc-middleware/rate_limit_mappings.gen.go"
|
|
|
|
)
|
|
|
|
for path in $@; do
|
|
|
|
flags+=("-input $path")
|
|
|
|
done
|
|
|
|
|
|
|
|
print_run go run ${SOURCE_DIR}/internal/tools/protoc-gen-consul-rate-limit/postprocess/main.go ${flags[@]} || {
|
|
|
|
err "Failed to generate gRPC rate limit mappings"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 12:53:55 +00:00
|
|
|
function generate_protoset_file {
|
|
|
|
local pkg_dir="${SOURCE_DIR}/pkg"
|
|
|
|
mkdir -p "$pkg_dir"
|
|
|
|
print_run buf build -o "${pkg_dir}/consul.protoset"
|
|
|
|
}
|
|
|
|
|
2022-04-05 16:58:07 +00:00
|
|
|
main "$@"
|
|
|
|
exit $?
|