Skip to content

Instantly share code, notes, and snippets.

@rezen
Last active November 16, 2017 13:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rezen/964afdf73c7334e8a1b3dffa7b0f7bfa to your computer and use it in GitHub Desktop.
Save rezen/964afdf73c7334e8a1b3dffa7b0f7bfa to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
format_text=1
if [[ "$@" == *"--nofmt"* ]]; then
format_text=0
fi
image="$1"
if [[ $image == '' ]]
then
echo "[!] You need to pass an image name" >&2
exit 2
fi
version=$(echo $image | cut -s -d':' -f 2)
if [[ $version == '' ]]
then
version='latest'
else
image=$(echo $image | cut -s -d':' -f 1)
fi
user_repo="docker-library/$image"
readonly meta=$(curl -s https://raw.githubusercontent.com/docker-library/official-images/master/library/$image )
readonly info=$(echo "$meta" | grep "Tags:.* ${version}" -A3)
readonly commit=$(echo "$info" | grep Commit | cut -d' ' -f2)
readonly directory=$(echo "$info" | grep Directory | cut -d' ' -f2)
tmp=$(echo "$meta" | grep GitRepo | cut -d' ' -f2)
# If a git repo is specifified use that!
if [ ! -z $tmp ]
then
tmp="${tmp/https:\/\/github.com\//}"
user_repo="${tmp/\.git/}"
fi
readonly spec=$(curl -s https://raw.githubusercontent.com/$user_repo/$commit/$directory/Dockerfile)
if (echo "$spec" | grep -q 404)
then
echo "[!] Could not find the Dockerfile for that image! [$image:$version]($commit/$directory)" >&2
exit 3
fi
if (which pygmentize &> /dev/null) && [ $format_text == 1 ]
then
echo "# @source github.com/$user_repo"
echo "$spec" | pygmentize -l docker
else
echo "# @source github.com/$user_repo"
echo "$spec"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment