Last active
March 10, 2017 18:32
-
-
Save nilium/0e70b090f72c23248fe6d281cd8ba869 to your computer and use it in GitHub Desktop.
Quick bash script to list Go package dependencies outside the stdlib
This file contains 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 | |
# Usage: lsdep [PACKAGE...] | |
# | |
# Example (list github.com/foo/bar and package dir deps [the . argument]) | |
# $ lsdep github.com/foo/bar . | |
# | |
# By default, this will list dependencies (imports), test imports, and test | |
# dependencies (imports made by test imports). You can recurse further by | |
# setting TESTIMPORTS to an integer greater than one, or to skip test | |
# dependencies, set TESTIMPORTS to 0 or a negative integer. | |
: "${TESTIMPORTS:=1}" | |
lsdep_impl__ () { | |
local txtestimps='{{range $v := .TestImports}}{{print . "\n"}}{{end}}' | |
local txdeps='{{range $v := .Deps}}{{print . "\n"}}{{end}}' | |
{ | |
go list -f "${txtestimps}${txdeps}" "$@" | |
if [[ -n "${TESTIMPORTS}" ]] && [[ "${TESTIMPORTS:-1}" -gt 0 ]] | |
then | |
go list -f "${txtestimps}" "$@" | | |
sort | uniq | | |
comm -23 - <(go list std | sort) | | |
TESTIMPORTS=$((TESTIMPORTS - 1)) xargs bash -c 'lsdep_impl__ "$@"' "$0" | |
fi | |
} | | |
sort | uniq | | |
comm -23 - <(go list std | sort) | |
} | |
export -f lsdep_impl__ | |
lsdep_impl__ "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment