From add6fda3c16d324376b471cce872d1bdfa21ec88 Mon Sep 17 00:00:00 2001 From: Andrew Hobson Date: Wed, 14 Apr 2021 14:28:08 +0000 Subject: [PATCH] Use flock if available, otherwise use perl(!) --- pre-commit-hadolint | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/pre-commit-hadolint b/pre-commit-hadolint index 3375552..402f06f 100755 --- a/pre-commit-hadolint +++ b/pre-commit-hadolint @@ -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=()