Skip to content

Instantly share code, notes, and snippets.

@renier
Created May 1, 2024 21:35
Show Gist options
  • Save renier/0beddc56dff9cd27be434200efe7fe6a to your computer and use it in GitHub Desktop.
Save renier/0beddc56dff9cd27be434200efe7fe6a to your computer and use it in GitHub Desktop.
Lists out the dependencies importing the module you specify all the way up to the project's main module
#!/bin/bash
home_module="$(go list -m | head -1)"
cache="$(mktemp)"
gomodule="go@$(go version | cut -d ' ' -f 3 | sed -e 's/^go//' | cut -d '.' -f 1,2)"
function explain() {
local importers="$(go mod graph | grep " ${1}" | cut -d ' ' -f 1)"
local importer;
for importer in $importers; do
if [[ $importer =~ "$home_module" ]]; then
continue
fi
if [ "$importer" = "$gomodule" ]; then
continue
fi
if grep "$importer" $cache > /dev/null; then
continue
fi
echo "$importer" | tee -a $cache
explain "$importer"
done
}
explain "$1"
echo "$home_module"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment