Skip to content

Instantly share code, notes, and snippets.

@bluegraybox
Created January 18, 2022 17:29
Show Gist options
  • Save bluegraybox/74d55fad33cae75b67c9824332cbdd8c to your computer and use it in GitHub Desktop.
Save bluegraybox/74d55fad33cae75b67c9824332cbdd8c to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
function wrapInGraph() {
# print beginning of DOT file, including formatting commands
cat <<END
digraph G {
rankdir=LR;
graph [
K=1.0,
overlap=false,
splines = spline,
];
node [
shape=box
style="rounded"
# color="#9999ff",
# fontcolor="#9999ff",
color="#666666",
fontcolor="#666666",
# fontname="Helvetica",
];
edge [
# arrowsize=2,
# color="#9999ff",
];
END
# Read `go mod graph` output from stdin as
# parent-module child-module
# Format it as DOT node pairs
# "parent-module" -> "child-module" [color="#cc33cc"]
# Set color for edges by cycling through color list @cl
# Write it to stdout
perl -e '$c=0; @cl=("cc3333","33cc33","3333cc","cccc33","cc33cc","33cccc"); $cls=scalar @cl;
while(<>){ $col="$cl[$c%$cls]"; s/^(\S+) (\S+)$/ "$1" -> "$2" [color="#$col"]/;
print;
$c++;}'
# Close the digraph definition
echo '}'
}
# Create a graph of the module dependencies, treating different versions as distinct
go mod graph |
wrapInGraph |
dot -Tpng > modgraph.png
# Create a graph of the module dependencies, treating different versions as the same module
go mod graph |
perl -pe 's/^(\S+?)(?:\@\S+)? (\S+?)(?:\@\S+)?$/$1 $2/' | # strip off the version info
sort | uniq | # remove duplicates
wrapInGraph |
dot -Tpng > modgraph-unversioned.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment