THIRD-PARTY-LICENSES.md holds the license and notice files of the Go distribution and of the 35 modules that ./cmd/uberbringer links on linux amd64, armv6, armv7 and arm64, as each distribution ships them. tools/genlicenses writes it on go generate from go list -deps, so modules only the tools and tests use stay out. Beside each module's root license it takes NOTICE files and the license files in its linked packages' directories, which cover vendored code in gofiber/utils, klauspost/compress and fasthttp. Each module is listed with its version, so any dependency change needs the file regenerated. The generator fails on a module with no license file or a license it does not recognize. The same text goes to internal/app/uberbringer/licenses_embed.md, which the binary embeds and the new `uberbringer licenses` command prints. The release archives carry the file, and the Docker image keeps it with LICENSE.txt in /usr/share/licenses/uberbringer/. The Makefile's openapi and openapi-check targets become generate and generate-check. They, the CI step and the gate in CLAUDE.md check docs/swagger.json and both license files, and a test fails when a committed copy is out of date. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
206 lines
5.7 KiB
Go
206 lines
5.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"cmp"
|
|
"encoding/json"
|
|
"fmt"
|
|
"maps"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// mainPackage is the binary the notices are for.
|
|
const mainPackage = "./cmd/uberbringer"
|
|
|
|
// platform is a target the binary is built for.
|
|
type platform struct {
|
|
goos, goarch, goarm string
|
|
}
|
|
|
|
// platforms are the targets of the release archives in .goreleaser.yaml.
|
|
// Build constraints can link a module on one platform and not another, so
|
|
// the notices cover them all.
|
|
var platforms = []platform{
|
|
{"linux", "amd64", ""},
|
|
{"linux", "arm", "6"},
|
|
{"linux", "arm", "7"},
|
|
{"linux", "arm64", ""},
|
|
}
|
|
|
|
// module is a third-party module linked into the binary.
|
|
type module struct {
|
|
path, version string
|
|
|
|
// dir is where the module's files are, in the module cache.
|
|
dir string
|
|
|
|
// files are the license and notice files covering the linked
|
|
// packages, as slash-separated paths relative to dir, those nearest the
|
|
// module root first.
|
|
files []string
|
|
}
|
|
|
|
// listedPackage is the part of a package's `go list -json` record read here.
|
|
type listedPackage struct {
|
|
Dir string
|
|
Standard bool
|
|
Module *struct {
|
|
Path string
|
|
Version string
|
|
Dir string
|
|
Main bool
|
|
}
|
|
}
|
|
|
|
// linkedModules returns, sorted by path, the third-party modules that
|
|
// mainPackage links on any of the platforms.
|
|
func linkedModules(root string) ([]module, error) {
|
|
byPath := make(map[string]*module)
|
|
found := make(map[string]map[string]bool)
|
|
names := make(map[string][]string)
|
|
for _, p := range platforms {
|
|
packages, err := listDeps(root, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, pkg := range packages {
|
|
if pkg.Standard || pkg.Module == nil || pkg.Module.Main {
|
|
continue
|
|
}
|
|
if pkg.Module.Dir == "" {
|
|
return nil, fmt.Errorf("module %s %s has no directory in the module cache", pkg.Module.Path, pkg.Module.Version)
|
|
}
|
|
m := byPath[pkg.Module.Path]
|
|
if m == nil {
|
|
m = &module{path: pkg.Module.Path, version: pkg.Module.Version, dir: pkg.Module.Dir}
|
|
byPath[m.path] = m
|
|
found[m.path] = make(map[string]bool)
|
|
}
|
|
files, err := coveringFiles(m.dir, pkg.Dir, names)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("module %s: %w", m.path, err)
|
|
}
|
|
for _, f := range files {
|
|
found[m.path][f] = true
|
|
}
|
|
}
|
|
}
|
|
|
|
modules := make([]module, 0, len(byPath))
|
|
for _, m := range byPath {
|
|
m.files = slices.SortedFunc(maps.Keys(found[m.path]), nearestFirst)
|
|
modules = append(modules, *m)
|
|
}
|
|
slices.SortFunc(modules, func(a, b module) int { return strings.Compare(a.path, b.path) })
|
|
return modules, nil
|
|
}
|
|
|
|
// listDeps returns the packages mainPackage links on platform p.
|
|
func listDeps(root string, p platform) ([]listedPackage, error) {
|
|
env := []string{"GOOS=" + p.goos, "GOARCH=" + p.goarch, "GOARM=" + p.goarm, "CGO_ENABLED=0"}
|
|
out, err := goOutput(root, env, "list", "-deps", "-json=Dir,Standard,Module", mainPackage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var packages []listedPackage
|
|
dec := json.NewDecoder(bytes.NewReader([]byte(out)))
|
|
for dec.More() {
|
|
var pkg listedPackage
|
|
if err := dec.Decode(&pkg); err != nil {
|
|
return nil, fmt.Errorf("reading go list output for %s/%s: %w", p.goos, p.goarch, err)
|
|
}
|
|
packages = append(packages, pkg)
|
|
}
|
|
return packages, nil
|
|
}
|
|
|
|
// coveringFiles returns the license and notice files in dir and in each of
|
|
// its parents up to moduleDir, as slash-separated paths relative to
|
|
// moduleDir. names caches the files found in each directory, by its path.
|
|
func coveringFiles(moduleDir, dir string, names map[string][]string) ([]string, error) {
|
|
rel, err := filepath.Rel(moduleDir, dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
|
return nil, fmt.Errorf("package directory %s is outside the module", dir)
|
|
}
|
|
var files []string
|
|
for {
|
|
abs := filepath.Join(moduleDir, rel)
|
|
inDir, ok := names[abs]
|
|
if !ok {
|
|
inDir, err = licenseFiles(abs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
names[abs] = inDir
|
|
}
|
|
for _, name := range inDir {
|
|
files = append(files, path.Join(filepath.ToSlash(rel), name))
|
|
}
|
|
if rel == "." {
|
|
return files, nil
|
|
}
|
|
rel = filepath.Dir(rel)
|
|
}
|
|
}
|
|
|
|
// licenseFiles returns the names of the license and notice files in dir.
|
|
func licenseFiles(dir string) ([]string, error) {
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var names []string
|
|
for _, e := range entries {
|
|
if e.Type().IsRegular() && (isLicenseFile(e.Name()) || isNoticeFile(e.Name())) {
|
|
names = append(names, e.Name())
|
|
}
|
|
}
|
|
return names, nil
|
|
}
|
|
|
|
// isLicenseFile reports whether a file name, in any letter case, is that of
|
|
// a license: LICENSE, LICENCE, COPYING or UNLICENSE, perhaps with a suffix
|
|
// such as -MIT, and with no extension or that of a text file.
|
|
func isLicenseFile(name string) bool {
|
|
stem := textStem(name)
|
|
for _, base := range []string{"license", "licence", "copying", "unlicense"} {
|
|
if stem == base || strings.HasPrefix(stem, base+"-") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isNoticeFile reports whether a file name, in any letter case, is that of
|
|
// the NOTICE file whose text the Apache License requires to be passed on.
|
|
func isNoticeFile(name string) bool {
|
|
return textStem(name) == "notice"
|
|
}
|
|
|
|
// textStem returns a file name in lower case, without its extension if that
|
|
// is the extension of a text file.
|
|
func textStem(name string) string {
|
|
lower := strings.ToLower(name)
|
|
switch ext := path.Ext(lower); ext {
|
|
case ".md", ".txt", ".rst":
|
|
return strings.TrimSuffix(lower, ext)
|
|
}
|
|
return lower
|
|
}
|
|
|
|
// nearestFirst orders slash-separated paths by depth, then by name, so a
|
|
// module's own license comes before those of code it vendors.
|
|
func nearestFirst(a, b string) int {
|
|
if c := cmp.Compare(strings.Count(a, "/"), strings.Count(b, "/")); c != 0 {
|
|
return c
|
|
}
|
|
return strings.Compare(a, b)
|
|
}
|