Skip to content

Commit

Permalink
Add a --sudo option to format_code.sh (LibrePCB#468)
Browse files Browse the repository at this point in the history
This allows to run docker with sudo for users which are not in the
docker group on their system.
  • Loading branch information
rnestler authored and ubruhin committed Jun 22, 2019
1 parent a7d9bc4 commit c3542c3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions dev/format_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ set -euo pipefail
# - Make sure the executables "clang-format" and "git" are available in PATH.
# - Run the command "./dev/format_code.sh" in the root of the repository.
# - To run clang-format in a docker-container, use the "--docker" parameter.
# - To run docker with sudo, use the "--sudo" parameter.
# - To format all files (instead of only modified ones), add the "--all"
# parameter. This is intended only for LibrePCB maintainers, don't use it!

DOCKER=""
DOCKER_CMD="docker"
ALL=""
for i in "$@"
do
Expand All @@ -20,6 +22,10 @@ case $i in
DOCKER="--docker"
shift
;;
--sudo)
DOCKER_CMD="sudo docker"
shift
;;
--all)
ALL="--all"
shift
Expand All @@ -34,13 +40,13 @@ REPO_ROOT=$(git rev-parse --show-toplevel)
if [ "$DOCKER" == "--docker" ]; then
DOCKER_IMAGE=librepcb/clang-format:6

if [ "$(docker images -q $DOCKER_IMAGE | wc -l)" == "0" ]; then
if [ "$($DOCKER_CMD images -q $DOCKER_IMAGE | wc -l)" == "0" ]; then
echo "Building clang-format container..."
docker build "$REPO_ROOT/dev/clang-format" -t librepcb/clang-format:6
$DOCKER_CMD build "$REPO_ROOT/dev/clang-format" -t librepcb/clang-format:6
fi

echo "[Re-running format_code.sh inside Docker container]"
docker run --rm -t -i --user "$(id -u):$(id -g)" \
$DOCKER_CMD run --rm -t -i --user "$(id -u):$(id -g)" \
-v "$REPO_ROOT:/code" \
$DOCKER_IMAGE \
/bin/bash -c "cd /code && dev/format_code.sh $ALL"
Expand Down

0 comments on commit c3542c3

Please sign in to comment.