Skip to content

Instantly share code, notes, and snippets.

@nilium nilium/gomodgraph
Created Mar 20, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.