|
16 | 16 |
|
17 | 17 | # list Go licenses |
18 | 18 | if [ -f go.mod ]; then |
19 | | - # List all direct Go module dependencies, transform their paths to root module paths |
20 | | - # (e.g., github.com/ory/x instead of github.com/ory/x/foo/bar), and generate a license report |
21 | | - # for each unique root module. This ensures that the license report is generated for the root |
22 | | - # module of a repository, where licenses are typically defined. |
23 | | - go_modules=$( |
24 | | - go list -f "{{if not .Indirect}}{{.Path}}{{end}}" -m ... | |
25 | | - sort -u | |
26 | | - awk -F/ '{ if ($1 == "github.com" && NF >= 3) { print $1"/"$2"/"$3 } else { print } }' | |
27 | | - sort -u |
| 19 | + # For each direct Go module dependency, pick one importable package and let |
| 20 | + # go-licenses report on it. Calling `go-licenses report <module>` directly |
| 21 | + # does not work for two common cases: |
| 22 | + # - Versioned modules carry a /vN suffix that must be present verbatim |
| 23 | + # (github.com/golang-jwt/jwt/v5, github.com/dgraph-io/ristretto/v2). |
| 24 | + # - Some module roots have no Go file of their own; only sub-packages |
| 25 | + # are importable (cloud.google.com/go/secretmanager -> .../apiv1). |
| 26 | + # Walking the actual import graph with `go list -deps` sidesteps both. |
| 27 | + go_packages=$( |
| 28 | + go list -deps -f '{{if .Module}}{{if not .Module.Main}}{{if not .Module.Indirect}}{{.Module.Path}}|{{.ImportPath}}{{end}}{{end}}{{end}}' ./... | |
| 29 | + sort -u -t'|' -k1,1 | |
| 30 | + cut -d'|' -f2 |
28 | 31 | ) |
29 | | - if [ -z "$go_modules" ]; then |
| 32 | + if [ -z "$go_packages" ]; then |
30 | 33 | echo "No Go modules found" >&2 |
31 | 34 | else |
32 | | - # Workaround until https://github.com/google/go-licenses/issues/307 is fixed |
33 | | - # .bin/go-licenses report "$module_name" --template .bin/license-template-go.tpl 2>/dev/null |
34 | | - # |
35 | | - echo "$go_modules" | xargs -I {} sh -c '.bin/go-licenses report --template .bin/license-template-go.tpl {}' | grep -v '^$' |
| 35 | + echo "$go_packages" | xargs -I {} sh -c '.bin/go-licenses report --template .bin/license-template-go.tpl {}' | grep -v '^$' |
36 | 36 | echo |
37 | 37 | fi |
38 | 38 | fi |
0 commit comments