This commit is contained in:
Tomas Mirchev 2025-11-03 08:04:08 +02:00
commit 1c8064ebf0

110
parse_image_ref.sh Normal file
View File

@ -0,0 +1,110 @@
#!/usr/bin/env bash
# parse_image_ref.sh
DEFAULT_REGISTRY="registry.tm0.app"
DEFAULT_TAG="latest"
parse_image_ref() {
local input="$1"
local image_ref registry repo tag label
if [[ $input == */* ]]; then
local prefix="${input%%/*}"
if [[ "$prefix" == "docker" ]]; then
input="docker.io/library/${input#*/}"
elif [[ "$prefix" == "tm0" ]]; then
input="${DEFAULT_REGISTRY}/${input#*/}"
fi
registry="${input%%/*}"
input=${input#*/}
else
registry="$DEFAULT_REGISTRY"
fi
if [[ "${input##*/}" == *:* ]]; then
tag="${input##*:}"
input="${input%:*}"
else
tag="$DEFAULT_TAG"
fi
repo="${registry}/${input}"
repo="${repo#*/}"
image_ref="${registry}/${repo}:${tag}"
label="${registry%.*}"
label="${label##*.}/${repo##*/}"
echo "$image_ref $repo $tag $label"
}
# -------------------------------------------------------------------
# Test harness
# -------------------------------------------------------------------
test_case() {
local input="$1"
local expected="$2"
local output
output="$(parse_image_ref "$input")"
if [[ "$output" == "$expected" ]]; then
printf "✅ PASS: %-35s => %s\n" "$input" "$output"
else
printf "❌ FAIL: %-35s\n" "$input"
printf " expected: %s\n" "$expected"
printf " got: %s\n" "$output"
fi
}
run_tests() {
echo "Running test cases..."
echo
test_case "tm0/nginx" \
"registry.tm0.app/nginx:latest nginx latest tm0/nginx"
test_case "tm0/dev/api" \
"registry.tm0.app/dev/api:latest dev/api latest tm0/api"
test_case "registry.tm0.app/nginx" \
"registry.tm0.app/nginx:latest nginx latest tm0/nginx"
test_case "registry.tm0.app:2525/nginx" \
"registry.tm0.app:2525/nginx:latest nginx latest tm0/nginx"
test_case "registry.tm0.app/dev/api" \
"registry.tm0.app/dev/api:latest dev/api latest tm0/api"
test_case "a.b.c.registry.tm0.app/dev/api" \
"a.b.c.registry.tm0.app/dev/api:latest dev/api latest tm0/api"
test_case "docker/nginx" \
"docker.io/library/nginx:latest library/nginx latest docker/nginx"
test_case "docker/nginx:1.25" \
"docker.io/library/nginx:1.25 library/nginx 1.25 docker/nginx"
test_case "docker.io/library/nginx:1.25" \
"docker.io/library/nginx:1.25 library/nginx 1.25 docker/nginx"
test_case "docker/redis" \
"docker.io/library/redis:latest library/redis latest docker/redis"
test_case "custom-registry.io/team/api:2.3.1" \
"custom-registry.io/team/api:2.3.1 team/api 2.3.1 custom-registry/api"
test_case "custom-registry.io:2525/team/api:2.3.1" \
"custom-registry.io:2525/team/api:2.3.1 team/api 2.3.1 custom-registry/api"
test_case "custom-registry.io:2525/api:2.3.1" \
"custom-registry.io:2525/api:2.3.1 api 2.3.1 custom-registry/api"
test_case "custom-registry.io/app" \
"custom-registry.io/app:latest app latest custom-registry/app"
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
run_tests
fi