Skip to content

Instantly share code, notes, and snippets.

@nilium
Created March 20, 2019 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilium/c143ce0e7092bc3495b4d4f2e312e3ae to your computer and use it in GitHub Desktop.
Save nilium/c143ce0e7092bc3495b4d4f2e312e3ae to your computer and use it in GitHub Desktop.
Tool to generate graphs of Go module dependencies
#!/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