From 9f953b7ab3b5862d6c5f8e35275aac1c0a8b651c Mon Sep 17 00:00:00 2001 From: infogulch Date: Thu, 6 May 2021 15:28:22 -0500 Subject: [PATCH] Clean up example; split new function image_parts --- docker-artifact.sh | 66 ++++++++++++++++++++++++---------------------- example/example.sh | 14 ++++++++-- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/docker-artifact.sh b/docker-artifact.sh index 6262c93..d918b3c 100755 --- a/docker-artifact.sh +++ b/docker-artifact.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash - set -e function docker_cli_plugin_metadata { @@ -40,22 +39,14 @@ Examples: function main { case "$1" in docker-cli-plugin-metadata) - docker_cli_plugin_metadata + docker_cli_plugin_metadata ;; artifact) case "$2" in - ls|list) - list "${@:3}" - ;; - label) - label "${@:3}" - ;; - download) - download "${@:3}" - ;; - *) - echo "$__usage" - ;; + ls|list) list "${@:3}" ;; + label) label "${@:3}" ;; + download) download "${@:3}" ;; + *) echo "$__usage" ;; esac ;; esac @@ -68,28 +59,38 @@ function list { } function list_json { + { read -r registry; read -r repo; read -r tag; } <<< "$(image_parts "$1")" + local token="$(get_token "$registry" "$repo")" + LOG "Querying manifest to extract labels for '$registry/$repo:$tag" + local manifest="$(curl --silent \ + -H "Accept:application/vnd.docker.container.image.v1+json" \ + -H "Authorization: Bearer $token" \ + "https://$registry/v2/$repo/manifests/$tag")" + # >&2 jq <<< "$manifest" + local labels="$(jq -r '.history[].v1Compatibility' <<< "$manifest" | jq --slurp '[.[].config | select(.Labels != null) | .Labels] | add')" + jq <<< "$labels" +} + +function image_parts { local image="$1" local regex='^([-_a-z\.]+)/([-_a-z]+)(:([-_a-z]+))?$' if [[ $image =~ $regex ]] ; then - registry="${BASH_REMATCH[1]}" - image="${BASH_REMATCH[2]}" - tag="${BASH_REMATCH[4]:-latest}" + local registry="${BASH_REMATCH[1]}" + local repo="${BASH_REMATCH[2]}" + local tag="${BASH_REMATCH[4]:-latest}" + # if no . in registry part, then it must be an image from docker hub; translate appropriately if ! [[ $registry =~ \. ]] ; then - image="$registry/$image" + repo="$registry/$repo" registry="registry-1.docker.io" fi else echo "Failed to parse image '$image'" exit 1 fi - local token="$(get_token "$registry" "$image")" - LOG "Querying manifest to extract labels for '$registry/$image:$tag" - local manifest="$(curl --silent \ - -H "Accept:application/vnd.docker.container.image.v1+json" \ - -H "Authorization: Bearer $token" \ - "https://$registry/v2/$image/manifests/$tag")" - local labels="$(jq -r '.history[].v1Compatibility' <<< "$manifest" | jq --slurp -r '[.[].config | select(.Labels != null) | .Labels] | add')" - jq <<< "$labels" + # >&2 echo "$image => $registry - $repo - $tag" #debug + echo "$registry" + echo "$repo" + echo "$tag" } function get_token { @@ -184,14 +185,17 @@ function delete_files { rm "$@" } -export verbose=1 function LOG { [ $verbose ] && >&2 echo -e "$(tput setaf 4) => $@$(tput sgr0)" } -# Export functions used in subshells -export -f LOG -export -f _search_layer +# run in subshell to allow sourcing this file while not stomping on parents' namespace +( + # Exports used in subshells + export verbose=1 + export -f LOG + export -f _search_layer -main "$@" + main "$@" +) diff --git a/example/example.sh b/example/example.sh index 39dd9ac..2255d2e 100755 --- a/example/example.sh +++ b/example/example.sh @@ -3,11 +3,21 @@ pushd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null # Build example image -echo "Rebuilding docker image for example" -printf 'FROM busybox \n RUN mkdir app && echo "Hello World!" > /app/testfile.txt' | docker build -t infogulch/artifact-test - &> /dev/null +echo "Rebuilding docker image infogulch/artifact-test" +printf 'FROM busybox \n RUN mkdir app && echo "Hello World!" > /app/testfile.txt' | docker build -t infogulch/artifact-test - # Execute docker artifact to add a label that identifies /app/testfile.txt +echo "" +echo "Labeling /app/testfile.txt in infogulch/artifact-test" ../docker-artifact.sh artifact label infogulch/artifact-test /app/testfile.txt +echo "" +echo "Pushing image infogulch/artifact-test" +docker push infogulch/artifact-test + +echo "" +echo "Listing files in infogulch/artifact-test" +../docker-artifact.sh artifact ls infogulch/artifact-test + popd &> /dev/null