Initial support for ls

This commit is contained in:
Joe 2021-05-04 21:28:35 -05:00
parent 8f71fce660
commit 2c05bfe4f8

View File

@ -12,13 +12,13 @@ function docker_cli_plugin_metadata {
} }
__usage=" __usage="
Usage: docker artifact [command] [options] Usage: docker artifact [command] [options] [args]
Commands: Commands:
ls - List files available to download directly from an image ls - List labeled files available to download directly from an image
download - Download files directly from an image that has been labeled download - Download labeled files directly from an image
label - Label an image label - Label files in an image
Command usage: Command usage:
@ -44,7 +44,7 @@ function main {
;; ;;
artifact) artifact)
case "$2" in case "$2" in
ls) ls|list)
list "${@:3}" list "${@:3}"
;; ;;
label) label)
@ -61,15 +61,57 @@ function main {
esac esac
} }
function _search_layer { function list {
local idmap="$1" local list="$(list_json "$@" | jq -r 'keys | .[]' | sed 's_^_ _')"
local imagetar="$2" echo " ** The following files are available to download from $1: "
local layertar="$3" echo "$list"
local search="$4" }
# look up digest associated with layer path
local digest="$(jq --arg key "$layertar" -r '.[$key]' <<< "$idmap")" function list_json {
# extract layer from image | list files in layer | add / prefix | search for file | append =$digest to each found file local image="$1"
tar -f "$imagetar" -x "$layertar" -O | tar -t | sed s_^_/_ | grep -wx "$search" | sed 's_.$_\0='"$digest"'_' 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}"
if ! [[ $registry =~ \. ]] ; then
image="$registry/$image"
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"
}
function get_token {
local registry="$1"
local image="$2"
if [[ ! -z "$REGISTRY_TOKEN" ]] ; then
echo "$REGISTRY_TOKEN"
return
fi
LOG "Retrieving registry token for $registry/$image"
case "$registry" in
registry-1.docker.io)
curl --silent "https://auth.docker.io/token?scope=repository:$image:pull&service=registry.docker.io" \
| jq -r .token
;;
*.azurecr.io)
echo "$REGISTRY_TOKEN"
;;
*.dkr.ecr.*.amazonaws.com)
aws ecr get-authorization-token | jq -r '.authorizationData[0].authorizationToken'
;;
esac
} }
function label { function label {
@ -126,6 +168,17 @@ function label {
echo " ** Run 'docker push $image' to push it to your container repository" echo " ** Run 'docker push $image' to push it to your container repository"
} }
function _search_layer {
local idmap="$1"
local imagetar="$2"
local layertar="$3"
local search="$4"
# look up digest associated with layer path
local digest="$(jq --arg key "$layertar" -r '.[$key]' <<< "$idmap")"
# extract layer from image | list files in layer | add / prefix | search for file | append =$digest to each found file
tar -f "$imagetar" -x "$layertar" -O | tar -t | sed s_^_/_ | grep -wx "$search" | sed 's_.$_\0='"$digest"'_'
}
function delete_files { function delete_files {
LOG "Cleaning up temp files $@" LOG "Cleaning up temp files $@"
rm "$@" rm "$@"