Skip to content

Commit

Permalink
Add function to parse and filter Docker tags by date
Browse files Browse the repository at this point in the history
  • Loading branch information
sscscc committed Jan 22, 2024
1 parent db37321 commit b3f5d77
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions common.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
#!/bin/bash

one_year_ago=$(date -d "1 year ago" +%s)
parse_tags_js() {
is_empty=true
local date_names=($(grep -oP '(?<="last_updated":").+?(?=")|(?<="name":").+?(?=")' <(echo "$tags_js")))
for ((i=0; i<${#date_names[@]}/2; i++)); do
date=$(date -d "${date_names[2*i]}" +%s)
name=${date_names[2*i+1]}
if (( date > one_year_ago )); then
echo "$name"
is_empty=false
fi
done
}
docker-tags(){
image=library/$1
tags_js=$(curl -sSL "https://registry.hub.docker.com/v2/repositories/${image}/tags/")
if [ $? -ne 0 ]; then
echo "curl $image failed" >&2
return 1
fi
grep -oP '(?<="name":").+?(?=")' <(echo "$tags_js")
parse_tags_js
while next_page=$(grep -oP '(?<="next":").+?(?=")' <(echo "$tags_js") | sed 's/\\u0026/\&/' | xargs)
do
if [[ -z "$next_page" || "$next_page" == "null" ]]; then
if [[ -z "$next_page" || "$next_page" == "null" || $is_empty == true ]]; then
break
fi
tags_js=$(curl -sSL "$next_page")
if [ $? -ne 0 ]; then
echo "curl $next_page failed" >&2
return 1
fi
grep -oP '(?<="name":").+?(?=")' <(echo "$tags_js")
parse_tags_js
done
}
}

0 comments on commit b3f5d77

Please sign in to comment.