-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdocker_parse.sh
More file actions
executable file
·51 lines (43 loc) · 1.44 KB
/
docker_parse.sh
File metadata and controls
executable file
·51 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
# Copyright 2023-2025 Broadcom
# SPDX-License-Identifier: Apache-2.0
# Parses a Docker image name and extracts information such as the registry, username, repository, and tag.
#
# Upon success the function sets the following environment variables based on the input image name:
# IMAGE_REGISTRY: The Docker image registry. If no registry is specified in the image name, this will default to docker.io.
# IMAGE_USERNAME: The username or namespace under which the image resides. If no username is specified, this will default to an empty string.
# IMAGE_REPOSITORY: The repository where the Docker image resides.
# IMAGE_TAG: The tag of the Docker image. If no tag is specified, this will default to latest.
function parse_docker_image() {
local image="$1"
if [[ -z "$image" ]]; then
echo "Error: No image name provided."
return 1
fi
# Check if there is a registry and port number
if [[ $image == *"/"* ]]; then
REGISTRY=${image%%/*}
image=${image#*/}
else
REGISTRY="docker.io"
fi
# Check if there is a tag
if [[ $image == *":"* ]]; then
TAG=${image##*:}
image=${image%:*}
else
TAG="latest"
fi
# Check if there is a username
if [[ $image == *"/"* ]]; then
USERNAME=${image%%/*}
REPOSITORY=${image#*/}
else
USERNAME=""
REPOSITORY=$image
fi
export IMAGE_REGISTRY=$REGISTRY
export IMAGE_USERNAME=$USERNAME
export IMAGE_REPOSITORY=$REPOSITORY
export IMAGE_TAG=$TAG
}