Skip to content

Commit

Permalink
Use flock if available, otherwise use perl(!)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hobson committed Apr 14, 2021
1 parent ade5e56 commit add6fda
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions pre-commit-hadolint
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ HADOLINT_URL="${HADOLINT_PREFIX_URL}/${HADOLINT_VERSION}/${HADOLINT_BINARY}"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
LOCAL_BINARY="${DIR}/${HADOLINT_BINARY}-${HADOLINT_VERSION}"

function mylock() {
# *sigh*
# macOS does not have flock so fall back to perl(!) if it's not
# available
if command -v flock &>/dev/null; then
flock -x 200
else
perl -e 'use Fcntl qw(:flock); open($fh, "<&=", 200) || die "Cannot open"; flock($fh, LOCK_EX) || die "Cannot lock"'
fi
}

# Download the binary if it doesn't already exist
if [[ ! -x ${LOCAL_BINARY} ]]; then
# use mkdir as a lock so only single download happens
# can't use flock as that doesn't exist on macOS
if mkdir "${LOCAL_BINARY}.lock" 2>/dev/null; then
(
mylock
if [[ ! -x ${LOCAL_BINARY} ]]; then
curl -o "${LOCAL_BINARY}" -L -sSf "${HADOLINT_URL}"
chmod 755 "${LOCAL_BINARY}"
else
# another process is downloading, give it time to complete
sleep 5
fi
fi
) 200> "${LOCAL_BINARY}.lock"

files=()

Expand Down

0 comments on commit add6fda

Please sign in to comment.