diff --git a/action.yml b/action.yml index a963f0c..f40309f 100644 --- a/action.yml +++ b/action.yml @@ -8,7 +8,11 @@ inputs: required: false bufArgs: description: The arguments to pass to buf - default: generate + default: check lint + required: false + protocPlugins: + description: The protoc language plugins to install, space separated. See README for supported plugins. + default: "" required: false runs: @@ -19,7 +23,10 @@ runs: - name: Maybe install and definitely invoke buf shell: bash run: | - source ${{ github.action_path }}/scripts/install-buf.sh ${{ inputs.bufVersion }} + REPO_DIR="$(pwd)" + cd ${{ github.action_path }} + source scripts/install-buf.sh ${{ inputs.bufVersion }} ${{ inputs.protocPlugins }} + cd $REPO_DIR buf ${{ inputs.bufArgs }} branding: diff --git a/scripts/executor-info.sh b/scripts/executor-info.sh new file mode 100755 index 0000000..05ffe59 --- /dev/null +++ b/scripts/executor-info.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +UNAME_OS=$(uname -s) +UNAME_ARCH=$(uname -m) + +NORMALIZED_OS=$UNAME_OS +if [ "$NORMALIZED_OS" = "Linux" ]; then + NORMALIZED_OS="linux" +fi + +NORMALIZED_ARCH=$UNAME_ARCH +if [ "$NORMALIZED_ARCH" = "x86_64" ]; then + NORMALIZED_ARCH="amd64" +fi + +export UNAME_OS +export UNAME_ARCH +export NORMALIZED_OS +export NORMALIZED_ARCH diff --git a/scripts/install-buf.sh b/scripts/install-buf.sh index aa9a8d4..10143bf 100755 --- a/scripts/install-buf.sh +++ b/scripts/install-buf.sh @@ -1,11 +1,22 @@ #!/bin/bash set -euo pipefail -UNAME_OS=$(uname -s) -UNAME_ARCH=$(uname -m) +source scripts/executor-info.sh BUF_VERSION=$1 +IFS=" " read -r -a PROTOC_PLUGINS <<< "$2" +for part in "${PROTOC_PLUGINS[@]}"; do + IFS="@" read -r -a langAndVersion <<< "$part" + + if [ "${#langAndVersion[@]}" != 2 ]; then + echo "Malformed plugin specifier \"$part\", expected e.g. go@v1.25.0" + exit 1 + fi + + bash "scripts/install-${langAndVersion[0]}-plugin.sh" "${langAndVersion[1]}" +done + if [ ! -f ".bin/buf" ]; then echo "Installing buf $BUF_VERSION for OS $UNAME_OS Arch $UNAME_ARCH" mkdir -p .bin diff --git a/scripts/install-go-plugin.sh b/scripts/install-go-plugin.sh new file mode 100755 index 0000000..3d2f08b --- /dev/null +++ b/scripts/install-go-plugin.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +source scripts/executor-info.sh + +GO_PROTOC_VERSION=$1 + +if [ ! -f ".bin/protoc-gen-go" ] || .bin/protoc-gen-go --version | grep -q "$GO_PROTOC_VERSION"; then + echo "Installing protobuf-gen-go $GO_PROTOC_VERSION for OS $UNAME_OS Arch $UNAME_ARCH" + mkdir -p .bin + curl --fail -sSL "https://github.com/protocolbuffers/protobuf-go/releases/download/$GO_PROTOC_VERSION/protoc-gen-go.$GO_PROTOC_VERSION.$NORMALIZED_OS.$NORMALIZED_ARCH.tar.gz" -o .bin/protoc-gen-go.tar.gz + tar -xf .bin/protoc-gen-go.tar.gz -C .bin + chmod +x .bin/protoc-gen-go +fi