Browse Source

Update "generate-dockerfile-links-partial.sh" to utilize new bashbrew features for parsing manifest files

Tianon Gravi 9 years ago
parent
commit
eb5d2577d1

+ 4 - 65
.template-helpers/generate-dockerfile-links-partial.sh

@@ -1,5 +1,5 @@
 #!/bin/bash
-set -eo pipefail
+set -e
 
 repo="$1"
 if [ -z "$repo" ]; then
@@ -8,67 +8,6 @@ if [ -z "$repo" ]; then
 	exit 1
 fi
 
-IFS=$'\n'
-lines=( $(curl -fsSL 'https://raw.githubusercontent.com/docker-library/official-images/master/library/'"$repo" | grep -vE '^$|^#') )
-unset IFS
-
-if [ "${#lines[@]}" -eq 0 ]; then
-	echo >&2 "Failed to read manifest file for $repo"
-	exit 1
-fi
-
-repoDirs=()
-declare -A repoDirTags=()
-
-latest=
-uniqImages=()
-for line in "${lines[@]}"; do
-	tag="$(echo "$line" | awk -F ': +' '{ print $1 }')"
-	if [ -z "$latest" -o "$tag" = 'latest' ]; then
-		latest="$tag"
-	fi
-	repoDir="$(echo "$line" | awk -F ': +' '{ print $2 }')"
-	if [ -z "${repoDirTags[$repoDir]}" ]; then
-		repoDirs+=( "$repoDir" )
-		uniqImages+=( "$repo:$tag" )
-	else
-		repoDirTags["$repoDir"]+=', '
-	fi
-	repoDirTags["$repoDir"]+='`'"$tag"'`'
-done
-
-IFS=','
-imageLayers="https://imagelayers.io/?images=${uniqImages[*]}"
-unset IFS
-
-for repoDir in "${repoDirs[@]}"; do
-	if [[ "$repoDir" != *github.com* ]]; then
-		# skip non-github.com for now
-		continue
-	fi
-	
-	# split out some data
-	gitUrl="${repoDir%%@*}"
-	commitDir="${repoDir#*@}"
-	commit="${commitDir%% *}"
-	dir="${commitDir#* }"
-	if [ "$dir" = "$commitDir" ]; then
-		dir=''
-	fi
-	
-	# sanitize some data
-	gitUrl="${gitUrl#git://}"
-	gitUrl="${gitUrl%/}"
-	gitUrl="${gitUrl%.git}"
-	dir="${dir#/}"
-	dir="${dir%/}"
-	[ -z "$dir" ] || dir="$dir/"
-	
-	url="https://$gitUrl/blob/$commit/${dir}Dockerfile"
-	
-	echo $'-\t['"${repoDirTags["$repoDir"]}"' (*'"${dir}Dockerfile"'*)]('"$url"')'
-done
-
-echo
-echo "[![](https://badge.imagelayers.io/$repo:$latest.svg)]($imageLayers)"
-echo
+bashbrew cat \
+		-F "$(dirname "$BASH_SOURCE")/$(basename "$BASH_SOURCE" .sh).tmpl" \
+		"https://github.com/docker-library/official-images/raw/master/library/$repo"

+ 52 - 0
.template-helpers/generate-dockerfile-links-partial.tmpl

@@ -0,0 +1,52 @@
+{{- /*
+
+This template defines the "Supported tags and Dockerfile links" portion of an image description.
+
+*/ -}}
+
+{{- define "dockerfilePath" -}}
+	{{- if ne .Directory "." -}}
+		{{- .Directory -}}
+		/
+	{{- end -}}
+	Dockerfile
+{{- end -}}
+
+{{- range $i, $e := $.Entries -}}
+	{{- /* force bashbrew to "fetch" the GitCommit so that it is fully resolved */ -}}
+	{{- $from := $.DockerFrom $e -}}
+
+	{{- $repoUrlBase := $e.GitRepo | replace "git://" "https://" | trimSuffixes ".git" -}}
+	{{- $isGitHub := hasPrefix "https://github.com/" $repoUrlBase -}}
+
+	{{- if $i -}}
+		{{- "\n" -}}
+	{{- end -}}
+
+	- {{- "\t" -}}
+	{{- if $isGitHub -}} [ {{- end -}}
+	` {{- $e.Tags | join "`, `" -}} ` (* {{- template "dockerfilePath" $e -}} *)
+	{{- if $isGitHub -}} ]( {{- $repoUrlBase -}} /blob/ {{- $e.GitCommit -}} / {{- template "dockerfilePath" $e -}} ) {{- end -}}
+	{{- /* TODO decide what to do (if anything) about non-GitHub repos with respect to URL */ -}}
+{{- end -}}
+
+{{- "\n\n" -}}
+
+[![](
+	{{- "https://badge.imagelayers.io/" -}}
+
+	{{- /* either "repo:latest" or "repo:first-tag" */ -}}
+	{{- printf "%s:%s" .RepoName (.Manifest.GetTag "latest" | ternary "latest" ((.Entries | first).Tags | first)) -}}
+
+	{{- ".svg" -}}
+)](
+	{{- "https://imagelayers.io/?images=" -}}
+
+	{{- /* list all "repo:tag" combinations, comma separated */ -}}
+	{{- range $i, $e := $.Entries -}}
+		{{- if $i -}} , {{- end -}}
+		{{- printf "%s:%s" $.RepoName ($e.Tags | first) -}}
+	{{- end -}}
+)
+
+{{- "\n\n" -}}