xref: /aosp_15_r20/external/grpc-grpc-java/buildscripts/run_in_docker.sh (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1#!/bin/bash
2set -eu -o pipefail
3
4quote() {
5  local arg
6  for arg in "$@"; do
7    printf "'"
8    printf "%s" "$arg" | sed -e "s/'/'\\\\''/g"
9    printf "' "
10  done
11}
12
13readonly docker_image=$1; shift
14
15readonly grpc_java_dir="$(dirname "$(readlink -f "$0")")/.."
16if [[ -t 0 ]]; then
17  DOCKER_ARGS="-it"
18else
19  # The input device on kokoro is not a TTY, so -it does not work.
20  DOCKER_ARGS=
21fi
22# Use a trap function to fix file permissions upon exit, without affecting
23# the original exit code. $DOCKER_ARGS can not be quoted, otherwise it becomes a '' which confuses
24# docker.
25exec docker run $DOCKER_ARGS --rm=true -v "${grpc_java_dir}":/grpc-java -w /grpc-java \
26  $docker_image \
27  bash -c "function fixFiles() { chown -R $(id -u):$(id -g) /grpc-java; }; trap fixFiles EXIT; $(quote "$@")"
28