Skip to content

Commit 793db8e

Browse files
committed
chore: format docker-pussh output
1 parent eeb3063 commit 793db8e

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

docker-pussh

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,61 @@
22
set -euo pipefail
33

44
# Return metadata expected by the Docker CLI plugin framework: https://github.com/docker/cli/pull/1564
5-
# TODO: update ShortDescription and Version as needed.
65
if [ "${1:-}" = "docker-cli-plugin-metadata" ]; then
76
cat <<EOF
87
{
98
"SchemaVersion": "0.1.0",
109
"Vendor": "https://github.com/psviderski",
1110
"Version": "0.1.0",
12-
"ShortDescription": "Push Docker images to remote daemons via SSH using unregistry"
11+
"ShortDescription": "Upload image to remote Docker daemon via SSH without external registry"
1312
}
1413
EOF
1514
exit 0
1615
fi
1716

1817
UNREGISTRY_IMAGE="ghcr.io/psviderski/unregistry:latest"
1918

20-
# Colors for output.
19+
# Colors and symbols for output.
2120
RED='\033[0;31m'
2221
GREEN='\033[0;32m'
2322
YELLOW='\033[0;33m'
23+
BLUE='\033[0;34m'
2424
NC='\033[0m' # no color
2525

2626
info() {
27-
echo -e "${GREEN}[INFO]${NC} $1"
27+
echo -e " ${BLUE}${NC} $1"
28+
}
29+
30+
success() {
31+
echo -e " ${GREEN}${NC} $1"
2832
}
2933

3034
warning() {
31-
echo -e "${YELLOW}[WARNING]${NC} $1"
35+
echo -e " ${YELLOW}!${NC} $1"
3236
}
3337

3438
error() {
35-
echo -e "${RED}[ERROR]${NC} $1" >&2
39+
echo -e "${RED}ERROR:${NC} $1" >&2
3640
exit 1
3741
}
3842

43+
usage() {
44+
echo "Usage: docker pussh [OPTIONS] IMAGE[:TAG] [USER@]HOST[:PORT]"
45+
echo ""
46+
echo "Upload a Docker image to a remote Docker daemon via SSH without an external registry."
47+
echo ""
48+
echo "Options:"
49+
echo " -h, --help Show this help message."
50+
echo " -i, --ssh-key path Path to SSH private key for remote login (if not already added to SSH agent)."
51+
echo " --platform string Push a specific platform for a multi-platform image (e.g., linux/amd64, linux/arm64)."
52+
echo " Local Docker has to use containerd image store to support multi-platform images."
53+
echo ""
54+
echo "Examples:"
55+
echo " docker pussh myimage:latest user@host"
56+
echo " docker pussh --platform linux/amd64 myimage:latest host"
57+
echo " docker pussh myimage:latest user@host:2222 -i ~/.ssh/id_ed25519"
58+
}
59+
3960
# SSH command arguments to be used for all ssh commands after establishing a shared "master" connection
4061
# using ssh_remote.
4162
declare -a SSH_ARGS=()
@@ -59,7 +80,7 @@ ssh_remote() {
5980

6081
# Establish ControlMaster connection in the background.
6182
if ! ssh "${ssh_opts[@]}" -f -N "$ssh_addr"; then
62-
error "failed to connect to remote host via SSH: $ssh_addr"
83+
error "Failed to connect to remote host via SSH: $ssh_addr"
6384
fi
6485

6586
# Populate SSH_ARGS array for reuse in all subsequent commands.
@@ -84,7 +105,7 @@ check_remote_docker() {
84105
if ssh "${SSH_ARGS[@]}" "[ \$(id -u) -ne 0 ] && sudo docker version" >/dev/null; then
85106
REMOTE_SUDO="sudo"
86107
else
87-
error "failed to run docker commands on remote host. Please ensure:
108+
error "Failed to run docker commands on remote host. Please ensure:
88109
- Docker is installed and running on the remote host
89110
- SSH user has permissions to run docker commands (user is root or non-root user is in 'docker' group)
90111
- If sudo is required, ensure the user can run 'sudo docker' without a password prompt"
@@ -130,11 +151,11 @@ run_unregistry() {
130151
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rm -f $UNREGISTRY_CONTAINER" >/dev/null 2>&1 || true
131152
# Check if the error is due to port binding.
132153
if ! echo "$output" | grep -q --ignore-case "bind.*$UNREGISTRY_PORT"; then
133-
error "failed to start unregistry container:\n$output"
154+
error "Failed to start unregistry container:\n$output"
134155
fi
135156
done
136157

137-
error "failed to start unregistry container:\n$output"
158+
error "Failed to start unregistry container:\n$output"
138159
}
139160

140161
# Forward a local port to a remote port over the established SSH connection.
@@ -158,10 +179,10 @@ forward_port() {
158179
return 0
159180
fi
160181

161-
error "failed to forward local port $local_port to remote unregistry port 127.0.0.1:$remote_port: $output"
182+
error "Failed to forward local port $local_port to remote unregistry port 127.0.0.1:$remote_port: $output"
162183
done
163184

164-
error "failed to find an available local port to forward to remote unregistry port. Please try again."
185+
error "Failed to find an available local port to forward to remote unregistry port. Please try again."
165186
}
166187

167188
# Check if the local Docker server is Docker Desktop.
@@ -201,28 +222,11 @@ run_docker_desktop_tunnel() {
201222
docker rm -f "$DOCKER_DESKTOP_TUNNEL_CONTAINER" >/dev/null 2>&1 || true
202223
# Check if error is due to port binding.
203224
if ! echo "$output" | grep -q --ignore-case "bind.*$DOCKER_DESKTOP_TUNNEL_PORT"; then
204-
error "failed to create a tunnel from Docker Desktop VM to localhost:$host_port:\n$output"
225+
error "Failed to create a tunnel from Docker Desktop VM to localhost:$host_port:\n$output"
205226
fi
206227
done
207228

208-
error "failed to create a tunnel from Docker Desktop VM to localhost:$host_port:\n$output"
209-
}
210-
211-
usage() {
212-
echo "Usage: docker pussh [OPTIONS] IMAGE[:TAG] [USER@]HOST[:PORT]"
213-
echo ""
214-
echo "Upload a Docker image to a remote Docker daemon via SSH."
215-
echo ""
216-
echo "Options:"
217-
echo " -h, --help Show this help message."
218-
echo " -i, --ssh-key path Path to SSH private key for remote login (if not already added to SSH agent)."
219-
echo " --platform string Push a specific platform for a multi-platform image (e.g., linux/amd64, linux/arm64)."
220-
echo " Local Docker has to use containerd image store to support multi-platform images."
221-
echo ""
222-
echo "Examples:"
223-
echo " docker pussh myimage:latest user@host"
224-
echo " docker pussh --platform linux/amd64 myimage:latest host"
225-
echo " docker pussh myimage:latest user@host:2222 -i ~/.ssh/id_ed25519"
229+
error "Failed to create a tunnel from Docker Desktop VM to localhost:$host_port:\n$output"
226230
}
227231

228232
DOCKER_PLATFORM=""
@@ -258,7 +262,7 @@ while [ $# -gt 0 ]; do
258262
exit 0
259263
;;
260264
-*)
261-
error "unknown option: $1\n$help_command"
265+
error "Unknown option: $1\n$help_command"
262266
;;
263267
*)
264268
# First non-option argument is the image.
@@ -268,7 +272,7 @@ while [ $# -gt 0 ]; do
268272
elif [ -z "$SSH_ADDRESS" ]; then
269273
SSH_ADDRESS="$1"
270274
else
271-
error "too many arguments.\n$help_command"
275+
error "Too many arguments.\n$help_command"
272276
fi
273277
shift
274278
;;
@@ -322,19 +326,19 @@ check_remote_docker
322326

323327
info "Starting unregistry container on remote host..."
324328
run_unregistry
325-
info "Unregistry is listening localhost:$UNREGISTRY_PORT on remote host."
329+
success "Unregistry is listening localhost:$UNREGISTRY_PORT on remote host."
326330

327331
# Forward random local port to remote unregistry port through established SSH connection.
328332
LOCAL_PORT=$(forward_port "$UNREGISTRY_PORT")
329-
info "Forwarded localhost:$LOCAL_PORT to unregistry over SSH connection."
333+
success "Forwarded localhost:$LOCAL_PORT to unregistry over SSH connection."
330334

331335
# Handle Docker Desktop on macOS.
332336
PUSH_PORT=$LOCAL_PORT
333337
if is_docker_desktop; then
334338
info "Detected Docker Desktop, creating additional tunnel from Docker Desktop VM to localhost:$LOCAL_PORT..."
335339
run_docker_desktop_tunnel "$LOCAL_PORT"
336340
PUSH_PORT=$DOCKER_DESKTOP_TUNNEL_PORT
337-
info "Docker Desktop tunnel created: localhost:$PUSH_PORT → localhost:$LOCAL_PORT"
341+
success "Docker Desktop tunnel created: localhost:$PUSH_PORT → localhost:$LOCAL_PORT"
338342
fi
339343

340344
# Tag and push the image to unregistry through the forwarded port.
@@ -356,15 +360,15 @@ if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker info -f '{{ .DriverStatus }}' | g
356360

357361
remote_registry_image="localhost:$UNREGISTRY_PORT/$IMAGE"
358362
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker pull $remote_registry_image"; then
359-
error "failed to pull image from unregistry on remote host."
363+
error "Failed to pull image from unregistry on remote host."
360364
fi
361365
if ! ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker tag $remote_registry_image $IMAGE"; then
362-
error "failed to retag image on remote host $remote_registry_image$IMAGE"
366+
error "Failed to retag image on remote host $remote_registry_image$IMAGE"
363367
fi
364368
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rmi $remote_registry_image" >/dev/null || true
365369
fi
366370

367371
info "Removing unregistry container on remote host..."
368372
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rm -f $UNREGISTRY_CONTAINER" >/dev/null || true
369373

370-
info "Successfully pushed '$IMAGE' to $SSH_ADDRESS"
374+
success "Successfully pushed '$IMAGE' to $SSH_ADDRESS"

0 commit comments

Comments
 (0)