@@ -9,13 +9,22 @@ cd -- "$(dirname -- "$0")"/..
99# GITHUB_TOKEN=$(gh auth token) ./tools/tidy.sh
1010#
1111# Note: This script requires the following tools:
12- # - docker
12+ # - docker or podman (or compatible CLI specified by TIDY_DOCKER_PATH. when both available and TIDY_DOCKER_PATH is not set, docker is preferred)
1313#
1414# This script is shared by projects under github.com/taiki-e, so there may also
1515# be checks for files not included in this repository, but they will be skipped
1616# if the corresponding files do not exist.
1717# It is not intended for manual editing.
1818
19+ bail () {
20+ if [[ -n " ${GITHUB_ACTIONS:- } " ]]; then
21+ printf ' ::error::%s\n' " $* "
22+ else
23+ printf >&2 ' error: %s\n' " $* "
24+ fi
25+ exit 1
26+ }
27+
1928if [[ $# -gt 0 ]]; then
2029 cat << EOF
2130USAGE:
2433 exit 1
2534fi
2635
36+ image=' ghcr.io/taiki-e/tidy'
2737if [[ -n " ${TIDY_DEV:- } " ]]; then
28- image= " ghcr.io/taiki-e/tidy :latest"
38+ image+= ' :latest'
2939else
30- image= " ghcr.io/taiki-e/tidy @sha256:bce85a4321f80c09f2b68420e9149bcf7c085130ab1e1fca54443f76833cd184 "
40+ image+= ' @sha256:4d7ec52a86bd3c0a2d96627b0ec3aa534afc02c2d56fc9a898df64e29aa03312 '
3141fi
3242user=" $( id -u) :$( id -g) "
3343workdir=$( pwd)
3444tmp=$( mktemp -d)
3545trap -- ' rm -rf -- "${tmp:?}"' EXIT
3646mkdir -p -- " ${tmp} " /{pwsh-cache,pwsh-local,zizmor-cache,dummy-dir,tmp}
37- touch -- " ${tmp} " /dummy
47+ printf ' ' > " ${tmp} " /dummy
3848code=0
3949color=' '
4050if [[ -t 1 ]] || [[ -n " ${GITHUB_ACTIONS:- } " ]]; then
4151 color=1
4252fi
53+ # Refs:
54+ # - https://docs.docker.com/reference/cli/docker/container/run/
55+ # - https://docs.podman.io/en/latest/markdown/podman-run.1.html
56+ # - https://cheatsheetseries.owasp.org/cheatsheets/Docker_Security_Cheat_Sheet.html
4357common_args=(
44- run --rm --init -i --user " ${user} "
58+ run --rm --init
4559 --cap-drop=all
4660 --security-opt=no-new-privileges
4761 --read-only
@@ -59,6 +73,30 @@ common_args=(
5973 --env TIDY_EXPECTED_SHELL_FILE_COUNT
6074 --env TIDY_EXPECTED_DOCKER_FILE_COUNT
6175)
76+ if [[ -n " ${TIDY_DOCKER_PATH:- } " ]]; then
77+ docker=" ${TIDY_DOCKER_PATH} "
78+ elif type -P docker > /dev/null; then
79+ docker=' docker'
80+ elif type -P podman > /dev/null; then
81+ docker=' podman'
82+ else
83+ bail ' this script requires docker or podman'
84+ fi
85+ rootless=' '
86+ if [[ " $( " ${docker} " --version) " == * ' podman' * ]]; then
87+ if [[ " $( " ${docker} " info) " == * ' rootless: true' * ]]; then
88+ rootless=1
89+ fi
90+ elif [[ " $( " ${docker} " info -f ' {{println .SecurityOptions}}' ) " == * ' rootless' * ]]; then
91+ rootless=1
92+ fi
93+ if [[ -n " ${rootless} " ]]; then
94+ printf ' docker path: %s\n' " ${docker} (rootless)"
95+ else
96+ printf ' docker path: %s\n' " ${docker} "
97+ common_args+=(--user " ${user} " )
98+ fi
99+
62100# Map ignored files (e.g., .env) to dummy files.
63101while IFS= read -r path; do
64102 if [[ -d " ${path} " ]]; then
@@ -73,7 +111,7 @@ while IFS= read -r path; do
73111done < <( git status --porcelain --ignored | grep -E ' ^!!' | cut -d' ' -f2)
74112
75113docker_run () {
76- docker " ${common_args[@]} " " $@ "
114+ " ${ docker} " " ${common_args[@]} " " $@ "
77115 code2=" $? "
78116 if [[ ${code} -eq 0 ]] && [[ ${code2} -ne 0 ]]; then
79117 code=" ${code2} "
0 commit comments