Created
March 20, 2019 20:43
-
-
Save nilium/c143ce0e7092bc3495b4d4f2e312e3ae to your computer and use it in GitHub Desktop.
Tool to generate graphs of Go module dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Based on https://github.com/go-modules-by-example/index/blob/master/014_mod_graph/README.md | |
OPEN=0 | |
TYPE=svg | |
case "$1" in | |
-) | |
OUT=/dev/stdout | |
;; | |
'') | |
OUT="$(mktemp deps.XXXXXXXXXX.svg)" | |
OPEN=1 | |
;; | |
*.*) | |
OUT="$1" | |
TYPE="${1##*.}" | |
;; | |
*) | |
OUT="$1" | |
;; | |
esac | |
go mod graph | | |
sed -Ee 's/@[^[:blank:]]+//g' | | |
sort | uniq | | |
awk ' | |
BEGIN { | |
print "digraph {" | |
print " graph [rankdir=TB, overlap=false];" | |
} | |
{ | |
printf " \"%s\" -> \"%s\"\n", $1, $2 | |
} | |
END { | |
print "}" | |
} | |
' | | |
twopi "-T${TYPE}" -o "$OUT" /dev/stdin | |
if [ $OPEN = 0 ]; then | |
exit 0 | |
fi | |
# Give the program in question one second to load the file before | |
# unlinking the file because scattering the earth with tons of SVGs | |
# isn't polite (even if your /tmp is tmpfs). | |
trap 'sleep 1; rm -v "$OUT"' EXIT | |
case "$(uname | tr A-Z a-z)" in | |
linux) xdg-open "$OUT" ;; | |
darwin) open "$OUT" ;; | |
*) cat "$OUT" ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment