Skip to content

Commit 9aa39fb

Browse files
committed
chore(shellcheck): Enforce "require-variable-braces" rule
1 parent 87958a0 commit 9aa39fb

File tree

2 files changed

+79
-79
lines changed

2 files changed

+79
-79
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ install-docker-plugin:
55
.PHONY: shellcheck
66
shellcheck:
77
find . -path "./tmp" -prune -o -type f \( -name "docker-pussh" -o -name "*.sh" \) -print0 \
8-
| xargs -0 shellcheck --enable=check-extra-masked-returns,check-set-e-suppressed,quote-safe-variables,require-double-brackets ;
8+
| xargs -0 shellcheck --enable=all ;

docker-pussh

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ ssh_remote() {
8080
local ssh_addr="$1"
8181
local target port
8282
# Split out the port component, if exists
83-
if [[ "$ssh_addr" =~ ^([^:]+)(:([0-9]+))?$ ]]; then
83+
if [[ "${ssh_addr}" =~ ^([^:]+)(:([0-9]+))?$ ]]; then
8484
target="${BASH_REMATCH[1]}"
8585
port="${BASH_REMATCH[3]:-}"
8686
else
@@ -96,17 +96,17 @@ ssh_remote() {
9696
-o "ConnectTimeout=15"
9797
)
9898
# Add port if specified
99-
if [[ -n "$port" ]]; then
100-
ssh_opts+=(-p "$port")
99+
if [[ -n "${port}" ]]; then
100+
ssh_opts+=(-p "${port}")
101101
fi
102102
# Add SSH key option if provided.
103-
if [[ -n "$SSH_KEY" ]]; then
104-
ssh_opts+=(-i "$SSH_KEY")
103+
if [[ -n "${SSH_KEY}" ]]; then
104+
ssh_opts+=(-i "${SSH_KEY}")
105105
fi
106106

107107
# Establish ControlMaster connection in the background.
108108
if ! ssh "${ssh_opts[@]}" -f -N "${target}"; then
109-
error "Failed to connect to remote host via SSH: $ssh_addr"
109+
error "Failed to connect to remote host via SSH: ${ssh_addr}"
110110
fi
111111

112112
# Populate SSH_ARGS array for reuse in all subsequent commands.
@@ -172,9 +172,9 @@ find_containerd_socket() {
172172
for socket_path in "${socket_paths[@]}"; do
173173
# Try without sudo first, then with sudo if REMOTE_SUDO is set
174174
# shellcheck disable=SC2029
175-
if ssh "${SSH_ARGS[@]}" "test -S '$socket_path'" 2>/dev/null ||
176-
ssh "${SSH_ARGS[@]}" "sudo -n test -S '$socket_path'" 2>/dev/null; then
177-
CONTAINERD_SOCKET="$socket_path"
175+
if ssh "${SSH_ARGS[@]}" "test -S '${socket_path}'" 2>/dev/null ||
176+
ssh "${SSH_ARGS[@]}" "sudo -n test -S '${socket_path}'" 2>/dev/null; then
177+
CONTAINERD_SOCKET="${socket_path}"
178178
return 0
179179
fi
180180
done
@@ -194,42 +194,42 @@ run_unregistry() {
194194
# Pull unregistry image if it doesn't exist on the remote host. This is done separately to not capture the output
195195
# and print the pull progress to the terminal.
196196
# shellcheck disable=SC2029
197-
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker image inspect $UNREGISTRY_IMAGE" >/dev/null 2>&1; then
198-
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker pull $UNREGISTRY_IMAGE"
197+
if ! ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker image inspect ${UNREGISTRY_IMAGE}" >/dev/null 2>&1; then
198+
ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker pull ${UNREGISTRY_IMAGE}"
199199
fi
200200

201201
for _ in {1..10}; do
202202
UNREGISTRY_PORT=$(random_port)
203-
UNREGISTRY_CONTAINER="unregistry-pussh-$$-$UNREGISTRY_PORT"
203+
UNREGISTRY_CONTAINER="unregistry-pussh-$$-${UNREGISTRY_PORT}"
204204

205205
# shellcheck disable=SC2029
206-
if output=$(ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker run -d \
207-
--name $UNREGISTRY_CONTAINER \
208-
-p 127.0.0.1:$UNREGISTRY_PORT:5000 \
209-
-v $CONTAINERD_SOCKET:/run/containerd/containerd.sock \
206+
if output=$(ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker run -d \
207+
--name ${UNREGISTRY_CONTAINER} \
208+
-p 127.0.0.1:${UNREGISTRY_PORT}:5000 \
209+
-v ${CONTAINERD_SOCKET}:/run/containerd/containerd.sock \
210210
--userns=host \
211211
--user root:root \
212-
$UNREGISTRY_IMAGE" 2>&1);
212+
${UNREGISTRY_IMAGE}" 2>&1);
213213
then
214214
# Wait a moment for the container to start
215215
sleep 1
216216

217217
# Verify the container is actually running and healthy
218-
if ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker ps --filter name=$UNREGISTRY_CONTAINER --filter status=running --quiet" | grep -q .; then
218+
if ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker ps --filter name=${UNREGISTRY_CONTAINER} --filter status=running --quiet" | grep -q .; then
219219
return 0
220220
fi
221221
fi
222222

223223
# Remove the container that failed to start if it was created.
224224
# shellcheck disable=SC2029
225-
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rm -f $UNREGISTRY_CONTAINER" >/dev/null 2>&1 || true
225+
ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker rm -f ${UNREGISTRY_CONTAINER}" >/dev/null 2>&1 || true
226226
# Check if the error is due to port binding.
227-
if ! echo "$output" | grep -q --ignore-case "bind.*$UNREGISTRY_PORT"; then
228-
error "Failed to start unregistry container:\n$output"
227+
if ! echo "${output}" | grep -q --ignore-case "bind.*${UNREGISTRY_PORT}"; then
228+
error "Failed to start unregistry container:\n${output}"
229229
fi
230230
done
231231

232-
error "Failed to start unregistry container:\n$output"
232+
error "Failed to start unregistry container:\n${output}"
233233
}
234234

235235
# Forward a local port to a remote port over the established SSH connection.
@@ -244,16 +244,16 @@ forward_port() {
244244

245245
# Check if port is already in use locally.
246246
# TODO: handle the case when nc is not available.
247-
if command -v nc >/dev/null && nc -z 127.0.0.1 "$local_port" 2>/dev/null; then
247+
if command -v nc >/dev/null && nc -z 127.0.0.1 "${local_port}" 2>/dev/null; then
248248
continue
249249
fi
250250

251-
if output=$(ssh "${SSH_ARGS[@]}" -O forward -L "$local_port:127.0.0.1:$remote_port" 2>&1); then
252-
echo "$local_port"
251+
if output=$(ssh "${SSH_ARGS[@]}" -O forward -L "${local_port}:127.0.0.1:${remote_port}" 2>&1); then
252+
echo "${local_port}"
253253
return 0
254254
fi
255255

256-
error "Failed to forward local port $local_port to remote unregistry port 127.0.0.1:$remote_port: $output"
256+
error "Failed to forward local port ${local_port} to remote unregistry port 127.0.0.1:${remote_port}: ${output}"
257257
done
258258

259259
error "Failed to find an available local port to forward to remote unregistry port. Please try again."
@@ -264,7 +264,7 @@ is_additional_tunneling_needed() {
264264
# Read all output to a variable to avoid issues with pipefail when 'grep -q' exits early.
265265
local output
266266
output=$(docker version 2>/dev/null)
267-
echo "$output" | grep -E -q "Docker Desktop|colima" && return 0
267+
echo "${output}" | grep -E -q "Docker Desktop|colima" && return 0
268268
return 1
269269
}
270270

@@ -283,24 +283,24 @@ run_docker_desktop_tunnel() {
283283
DOCKER_DESKTOP_TUNNEL_PORT=$(random_port)
284284

285285
if output=$(docker run -d --rm \
286-
--name "$DOCKER_DESKTOP_TUNNEL_CONTAINER" \
287-
-p "127.0.0.1:$DOCKER_DESKTOP_TUNNEL_PORT:5000" \
286+
--name "${DOCKER_DESKTOP_TUNNEL_CONTAINER}" \
287+
-p "127.0.0.1:${DOCKER_DESKTOP_TUNNEL_PORT}:5000" \
288288
alpine/socat \
289289
TCP-LISTEN:5000,fork,reuseaddr \
290-
"TCP-CONNECT:host.docker.internal:$host_port" 2>&1);
290+
"TCP-CONNECT:host.docker.internal:${host_port}" 2>&1);
291291
then
292292
return 0
293293
fi
294294

295295
# Remove the container that failed to start if it was created.
296-
docker rm -f "$DOCKER_DESKTOP_TUNNEL_CONTAINER" >/dev/null 2>&1 || true
296+
docker rm -f "${DOCKER_DESKTOP_TUNNEL_CONTAINER}" >/dev/null 2>&1 || true
297297
# Check if error is due to port binding.
298-
if ! echo "$output" | grep -q --ignore-case "bind.*$DOCKER_DESKTOP_TUNNEL_PORT"; then
299-
error "Failed to create a tunnel from Docker Desktop VM to localhost:$host_port:\n$output"
298+
if ! echo "${output}" | grep -q --ignore-case "bind.*${DOCKER_DESKTOP_TUNNEL_PORT}"; then
299+
error "Failed to create a tunnel from Docker Desktop VM to localhost:${host_port}:\n${output}"
300300
fi
301301
done
302302

303-
error "Failed to create a tunnel from Docker Desktop VM to localhost:$host_port:\n$output"
303+
error "Failed to create a tunnel from Docker Desktop VM to localhost:${host_port}:\n${output}"
304304
}
305305

306306
DOCKER_PLATFORM=""
@@ -319,14 +319,14 @@ while [[ $# -gt 0 ]]; do
319319
case "$1" in
320320
-i|--ssh-key)
321321
if [[ -z "${2:-}" ]]; then
322-
error "-i/--ssh-key option requires an argument.\n$help_command"
322+
error "-i/--ssh-key option requires an argument.\n${help_command}"
323323
fi
324324
SSH_KEY="$2"
325325
shift 2
326326
;;
327327
--platform)
328328
if [[ -z "${2:-}" ]]; then
329-
error "--platform option requires an argument.\n$help_command"
329+
error "--platform option requires an argument.\n${help_command}"
330330
fi
331331
DOCKER_PLATFORM="$2"
332332
shift 2
@@ -341,38 +341,38 @@ while [[ $# -gt 0 ]]; do
341341
exit 0
342342
;;
343343
-*)
344-
error "Unknown option: $1\n$help_command"
344+
error "Unknown option: $1\n${help_command}"
345345
;;
346346
*)
347347
# First non-option argument is the image.
348-
if [[ -z "$IMAGE" ]]; then
348+
if [[ -z "${IMAGE}" ]]; then
349349
IMAGE="$1"
350350
# Second non-option argument is the SSH address.
351-
elif [[ -z "$SSH_ADDRESS" ]]; then
351+
elif [[ -z "${SSH_ADDRESS}" ]]; then
352352
SSH_ADDRESS="$1"
353353
else
354-
error "Too many arguments.\n$help_command"
354+
error "Too many arguments.\n${help_command}"
355355
fi
356356
shift
357357
;;
358358
esac
359359
done
360360

361361
# Validate required arguments.
362-
if [[ -z "$IMAGE" ]] || [[ -z "$SSH_ADDRESS" ]]; then
363-
error "IMAGE and HOST are required.\n$help_command"
362+
if [[ -z "${IMAGE}" ]] || [[ -z "${SSH_ADDRESS}" ]]; then
363+
error "IMAGE and HOST are required.\n${help_command}"
364364
fi
365365
# Validate SSH key file exists if provided.
366-
if [[ -n "$SSH_KEY" ]] && [[ ! -f "$SSH_KEY" ]]; then
367-
error "SSH key file not found: $SSH_KEY"
366+
if [[ -n "${SSH_KEY}" ]] && [[ ! -f "${SSH_KEY}" ]]; then
367+
error "SSH key file not found: ${SSH_KEY}"
368368
fi
369369

370370
get_temp_image_name() {
371371
local image=$1
372372
local regex="^(.*:.*?\/)?(.*\/)?(.+)$"
373373
local repo_with_tag
374374

375-
if [[ $image =~ $regex ]]; then
375+
if [[ ${image} =~ ${regex} ]]; then
376376
repo_with_tag=${BASH_REMATCH[3]}
377377
echo "${RANDOM}-${repo_with_tag}"
378378
return 0
@@ -391,19 +391,19 @@ cleanup() {
391391
fi
392392

393393
# Remove Docker Desktop tunnel container if exists.
394-
if [[ -n "$DOCKER_DESKTOP_TUNNEL_CONTAINER" ]]; then
395-
docker rm -f "$DOCKER_DESKTOP_TUNNEL_CONTAINER" >/dev/null 2>&1 || true
394+
if [[ -n "${DOCKER_DESKTOP_TUNNEL_CONTAINER}" ]]; then
395+
docker rm -f "${DOCKER_DESKTOP_TUNNEL_CONTAINER}" >/dev/null 2>&1 || true
396396
fi
397397

398398
# Clean up the temporary registry image tag.
399399
if [[ -n "${REGISTRY_IMAGE:-}" ]]; then
400-
docker rmi "$REGISTRY_IMAGE" >/dev/null 2>&1 || true
400+
docker rmi "${REGISTRY_IMAGE}" >/dev/null 2>&1 || true
401401
fi
402402

403403
# Stop and remove unregistry container on remote host.
404-
if [[ -n "$UNREGISTRY_CONTAINER" ]]; then
404+
if [[ -n "${UNREGISTRY_CONTAINER}" ]]; then
405405
# shellcheck disable=SC2029
406-
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rm -f $UNREGISTRY_CONTAINER" >/dev/null 2>&1 || true
406+
ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker rm -f ${UNREGISTRY_CONTAINER}" >/dev/null 2>&1 || true
407407
fi
408408

409409
# Terminate the shared SSH connection if it was established.
@@ -413,37 +413,37 @@ cleanup() {
413413
}
414414
trap cleanup EXIT
415415

416-
info "Connecting to $SSH_ADDRESS..."
417-
ssh_remote "$SSH_ADDRESS"
416+
info "Connecting to ${SSH_ADDRESS}..."
417+
ssh_remote "${SSH_ADDRESS}"
418418
check_remote_docker
419419

420420
info "Starting unregistry container on remote host..."
421421
run_unregistry
422-
success "Unregistry is listening localhost:$UNREGISTRY_PORT on remote host."
422+
success "Unregistry is listening localhost:${UNREGISTRY_PORT} on remote host."
423423

424424
# Forward random local port to remote unregistry port through established SSH connection.
425-
LOCAL_PORT=$(forward_port "$UNREGISTRY_PORT")
426-
success "Forwarded localhost:$LOCAL_PORT to unregistry over SSH connection."
425+
LOCAL_PORT=$(forward_port "${UNREGISTRY_PORT}")
426+
success "Forwarded localhost:${LOCAL_PORT} to unregistry over SSH connection."
427427

428-
PUSH_PORT=$LOCAL_PORT
428+
PUSH_PORT=${LOCAL_PORT}
429429
# Handle virtualized Docker on macOS (e.g., Docker Desktop, Colima, etc.)
430430
# shellcheck disable=SC2310
431431
if is_additional_tunneling_needed; then
432-
info "Detected virtualized Docker, creating additional tunnel to localhost:$LOCAL_PORT..."
433-
run_docker_desktop_tunnel "$LOCAL_PORT"
434-
PUSH_PORT=$DOCKER_DESKTOP_TUNNEL_PORT
435-
success "Additional tunnel created: localhost:$PUSH_PORT → localhost:$LOCAL_PORT"
432+
info "Detected virtualized Docker, creating additional tunnel to localhost:${LOCAL_PORT}..."
433+
run_docker_desktop_tunnel "${LOCAL_PORT}"
434+
PUSH_PORT=${DOCKER_DESKTOP_TUNNEL_PORT}
435+
success "Additional tunnel created: localhost:${PUSH_PORT} → localhost:${LOCAL_PORT}"
436436
fi
437437

438438
IMAGE_NAME_TAG=$(get_temp_image_name "${IMAGE}")
439439
# Tag and push the image to unregistry through the forwarded port.
440-
REGISTRY_IMAGE="localhost:$PUSH_PORT/$IMAGE_NAME_TAG"
441-
docker tag "$IMAGE" "$REGISTRY_IMAGE"
442-
info "Pushing '$REGISTRY_IMAGE' to unregistry..."
440+
REGISTRY_IMAGE="localhost:${PUSH_PORT}/${IMAGE_NAME_TAG}"
441+
docker tag "${IMAGE}" "${REGISTRY_IMAGE}"
442+
info "Pushing '${REGISTRY_IMAGE}' to unregistry..."
443443

444444
DOCKER_PUSH_OPTS=()
445-
if [[ -n "$DOCKER_PLATFORM" ]]; then
446-
DOCKER_PUSH_OPTS+=("--platform" "$DOCKER_PLATFORM")
445+
if [[ -n "${DOCKER_PLATFORM}" ]]; then
446+
DOCKER_PUSH_OPTS+=("--platform" "${DOCKER_PLATFORM}")
447447
fi
448448

449449
# Try push with retry logic for connection issues
@@ -453,44 +453,44 @@ PUSH_SLEEP_INTERVAL=3
453453

454454
for attempt in $(seq 1 "${PUSH_RETRY_COUNT}"); do
455455
# That DOCKER_PUSH_OPTS expansion is needed to avoid issues with empty array expansion in older bash versions.
456-
if docker push ${DOCKER_PUSH_OPTS[@]+"${DOCKER_PUSH_OPTS[@]}"} "$REGISTRY_IMAGE"; then
456+
if docker push ${DOCKER_PUSH_OPTS[@]+"${DOCKER_PUSH_OPTS[@]}"} "${REGISTRY_IMAGE}"; then
457457
PUSH_SUCCESS=true
458458
break
459459
else
460460
if [[ "${attempt}" -lt "${PUSH_RETRY_COUNT}" ]]; then
461-
warning "Push attempt $attempt failed, retrying in ${PUSH_SLEEP_INTERVAL} seconds..."
461+
warning "Push attempt ${attempt} failed, retrying in ${PUSH_SLEEP_INTERVAL} seconds..."
462462
sleep "${PUSH_SLEEP_INTERVAL}"
463463
fi
464464
fi
465465
done
466466

467-
if [[ "$PUSH_SUCCESS" = false ]]; then
468-
error "Failed to push image after $PUSH_RETRY_COUNT attempts."
467+
if [[ "${PUSH_SUCCESS}" = false ]]; then
468+
error "Failed to push image after ${PUSH_RETRY_COUNT} attempts."
469469
fi
470470

471471
REMOTE_REGISTRY_IMAGE=""
472472
# Pull image from unregistry if remote Docker doesn't uses containerd image store.
473473
# shellcheck disable=SC2029
474-
if ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker info -f '{{ .DriverStatus }}' | grep -q 'containerd.snapshotter'"; then
474+
if ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker info -f '{{ .DriverStatus }}' | grep -q 'containerd.snapshotter'"; then
475475
# Remote image store uses containerd, so we can use the image directly.
476-
REMOTE_REGISTRY_IMAGE="$IMAGE_NAME_TAG"
476+
REMOTE_REGISTRY_IMAGE="${IMAGE_NAME_TAG}"
477477
else
478478
info "Remote Docker doesn't use containerd image store. Pulling image from unregistry..."
479-
REMOTE_REGISTRY_IMAGE="localhost:$UNREGISTRY_PORT/$IMAGE_NAME_TAG"
480-
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker pull $REMOTE_REGISTRY_IMAGE"; then
479+
REMOTE_REGISTRY_IMAGE="localhost:${UNREGISTRY_PORT}/${IMAGE_NAME_TAG}"
480+
if ! ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker pull ${REMOTE_REGISTRY_IMAGE}"; then
481481
error "Failed to pull image from unregistry on remote host."
482482
fi
483483
fi
484484

485485
# shellcheck disable=SC2029
486-
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker tag $REMOTE_REGISTRY_IMAGE $IMAGE"; then
487-
error "Failed to retag image on remote host $REMOTE_REGISTRY_IMAGE$IMAGE"
486+
if ! ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker tag ${REMOTE_REGISTRY_IMAGE} ${IMAGE}"; then
487+
error "Failed to retag image on remote host ${REMOTE_REGISTRY_IMAGE}${IMAGE}"
488488
fi
489489
# shellcheck disable=SC2029
490-
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rmi $REMOTE_REGISTRY_IMAGE" >/dev/null || true
490+
ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker rmi ${REMOTE_REGISTRY_IMAGE}" >/dev/null || true
491491

492492
info "Removing unregistry container on remote host..."
493493
# shellcheck disable=SC2029
494-
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rm -f $UNREGISTRY_CONTAINER" >/dev/null || true
494+
ssh "${SSH_ARGS[@]}" "${REMOTE_SUDO} docker rm -f ${UNREGISTRY_CONTAINER}" >/dev/null || true
495495

496-
success "Successfully pushed '$IMAGE' to $SSH_ADDRESS"
496+
success "Successfully pushed '${IMAGE}' to ${SSH_ADDRESS}"

0 commit comments

Comments
 (0)