From 849e2f3d4e1975bf02cfc7d2b32534294780a53b Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Mon, 8 Jul 2019 15:28:39 +0200 Subject: analyses: add dependency-tree.sh Add a new analysis script to print the dependency tree. Signed-off-by: Valentin Rothberg --- dependencies/analyses/README.md | 23 +++++++++++++++++++++++ dependencies/analyses/dependency-tree.sh | 13 +++++++++++++ 2 files changed, 36 insertions(+) create mode 100755 dependencies/analyses/dependency-tree.sh (limited to 'dependencies/analyses') diff --git a/dependencies/analyses/README.md b/dependencies/analyses/README.md index 70de3f621..2c6e4ca2e 100644 --- a/dependencies/analyses/README.md +++ b/dependencies/analyses/README.md @@ -1,5 +1,8 @@ # A set of scripts and instructions that help to analyze and debloat go-lang dependencies. +Note that all scripts mentioned below follow the [KISS principle](https://en.wikipedia.org/wiki/KISS_principle) on purpose. +The scripts are meant to be used in combination to aid in undestanding the packages' dependencies and how they contribute to the size of the compiled binary. + ## Size of packages To analyze the size of all go packages used during the build process, pass the `-work -a` build flags to `go build`. @@ -62,3 +65,23 @@ Running such an analysis on libpod may look as follows: ``` Running the script can help identify sources of bloat and reveal potential candidates (e.g., entire packages, types, or function) for refactoring. + + +## Dependency Tree + +Use the `dependency-tree.sh` script to figure out which package including which packages. +The output of the script has the format `package: dependency_1, dependency_2, ...`. +Each line is followed by a blank link to make it easier to read. +Note that the list of dependencies includes only the direct dependencies and not all transitive dependencies. +The transitive dependencies of a given package can be examined by running `go list -f '{{ .Name }}: {{ join .Deps ", " }}' $PACKAGE` or by browsing through the output of `dependency-tree.sh`. + +Running such a dependency-tree analysis may look as follows: + + +``` +[libpod]$ ./dependencies/analyses/dependency-tree.sh github.com/containers/libpod > tree.txt +[libpod]$ grep "^github.com/containers/libpod/pkg/registries" tree.txt +github.com/containers/libpod/pkg/registries: github.com/containers/libpod/vendor/github.com/containers/image/pkg/sysregistriesv2, github.com/containers/libpod/vendor/github.com/containers/image/types, github.com/containers/libpod/pkg/rootless, github.com/containers/libpod/vendor/github.com/docker/distribution/reference, github.com/containers/libpod/vendor/github.com/pkg/errors, os, path/filepath, strings +``` + +As shown above, the script's output can then be used to query for specific packages. diff --git a/dependencies/analyses/dependency-tree.sh b/dependencies/analyses/dependency-tree.sh new file mode 100755 index 000000000..3c9dccc51 --- /dev/null +++ b/dependencies/analyses/dependency-tree.sh @@ -0,0 +1,13 @@ +#!/usr/bin/bash + +if test "$#" -ne 1; then + echo "invalid arguments: usage: $0 path to package" + exit 1 +fi + +DATA=$(go list $1/... \ + | xargs -d '\n' go list -f '{{ .ImportPath }}: {{ join .Imports ", " }}' \ + | awk '{ printf "%s\n\n", $0 }' \ + ) + +echo "$DATA" -- cgit v1.2.3-54-g00ecf