From f2c9be5c292e9a07355317445166d5bba8106352 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sat, 21 Jan 2023 22:42:36 -0800 Subject: [PATCH] fix making tags add image_tags function refactor image_tag function --- .gitignore | 1 + lib/src/02-make-tag | 15 ++++++--------- lib/src/image-info | 6 ++++++ lib/src/image-tag | 31 ++++++++++++++----------------- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 054a8bb..4e11286 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /archive/ /build.log _opt/ +.src \ No newline at end of file diff --git a/lib/src/02-make-tag b/lib/src/02-make-tag index 68828f7..3e8a813 100755 --- a/lib/src/02-make-tag +++ b/lib/src/02-make-tag @@ -2,8 +2,7 @@ make_tag () { -local DIR -DIR=$(cd "$(dirname "$BASH_SOURCE")" >/dev/null 2>&1 ; pwd -P ) +local tag # generate a full image name with tag # $1 name, $2 user(or repo), $3 repo @@ -11,12 +10,9 @@ DIR=$(cd "$(dirname "$BASH_SOURCE")" >/dev/null 2>&1 ; pwd -P ) [[ $# -lt 1 ]] && echo "image base name required" && exit declare OPTION; declare OPTARG; declare OPTIND -while getopts 'daht:r:u:' OPTION; do +while getopts 'at:r:u:' OPTION; do # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} case "$OPTION" in -d) - delete=true -;; r) REPO=$OPTARG ;; @@ -38,12 +34,13 @@ done shift $((OPTIND - 1)) -# image tag -name=$1 +tag=$( echo $1 | cut -s -d ":" -f2) +tag=${tag:-$TAG} +name=${1%:*} user=${2:-$RUSER} repo=${3:-$REPO} -tag=$([[ $repo ]] && echo ${repo}/)$([[ $user ]] && echo ${user}/)$name$([[ $arm ]] && echo -arm64):${TAG:-latest} +tag=$([[ $repo ]] && echo ${repo}/)$([[ $user ]] && echo ${user}/)$name$([[ $arm ]] && echo -arm64):${tag:-latest} echo $tag diff --git a/lib/src/image-info b/lib/src/image-info index c44f125..3d3adc0 100755 --- a/lib/src/image-info +++ b/lib/src/image-info @@ -2,6 +2,8 @@ image_info () { [[ $1 == "-k" ]] && key=$2 && shift 2 tag=$(make_tag "$@") +# TODO try using --format to extract keys +# https://docs.docker.com/engine/reference/commandline/inspect/ info=$(docker image inspect $tag 2> /dev/null) || info=$(docker image inspect $1 2> /dev/null) || return 1 if [[ $key ]]; then # echo image: $tag, key:$key @@ -20,6 +22,10 @@ image_arch () { image_info -k Architecture "$@" } +image_tags () { + image_info -k RepoTags "$@" +} + image_id () { image_info -k Id "$@" | sed 's/.*\://' | sed 's/"//' } diff --git a/lib/src/image-tag b/lib/src/image-tag index 26798b7..4b866a7 100755 --- a/lib/src/image-tag +++ b/lib/src/image-tag @@ -2,31 +2,28 @@ image_tag () { -local DIR -DIR=$(cd "$(dirname "$BASH_SOURCE")" >/dev/null 2>&1 ; pwd -P ) +local name; local remove; local id # tags an image -# -d -f +# -i [[ $# -lt 1 ]] && echo "image base name required" && exit - - -[[ $1 == "-d" ]] && delete=true && shift 1 -[[ $1 == "-f" ]] && force=true && shift 1 +[[ $1 == "-r" ]] && remove=true && shift 1 [[ $1 == "-i" ]] && { shift 1; id=$1; } || id=$(image_id $1) +name=$(make_tag "$1") +[[ ! $id ]] && { echo "no image with id:$id name:$name"; return 1; } -[[ ! $id ]] && { echo "no image with id $id $(make_tag "@")"; return 1; } - - -[[ $force ]] && docker rmi -f $id && return 0 - -if [[ $delete ]];then - docker rmi $id -else +if [[ $2 ]];then +# echo making tag for $2 $(make_tag $2) docker tag $id $(make_tag $2) +else + [[ $remove ]] && docker rmi $name || echo to remove an image tag use -r fi -echo tags after operation for image $id -image_info -k RepoTags $id +image_tags $id +} + +image_delete () { + docker rmi -f "$@" }