Files
57_WolveandClaude Opus 5.5 0c88b4ffd5
Security / Vulnerability check (push) Successful in 44s
Test / Lint and test (push) Successful in 2m5s
Add third-party license notices, generated and embedded in the binary
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>
2026-11-30 21:58:39 -06:00

130 lines
3.6 KiB
Go

// Command genlicenses writes THIRD-PARTY-LICENSES.md, the licenses and
// notices of the third-party software in the uberbringer binary, and the copy
// of it that the binary embeds. That software is the Go standard library and
// runtime, and every module ./cmd/uberbringer links on the platforms it is
// released for. A module's texts are its license and notice files in the
// directories of its linked packages and their parents, up to the module
// root, so code that a module vendors under a license of its own is covered
// too. Modules used only by tools and tests are not linked, so they are left
// out. Nothing in the file is written by hand, and a test fails when a
// committed copy differs from what this produces.
//
// go run ./tools/genlicenses (or: go generate ./...)
package main
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// The generated files, relative to the repository root. go:embed cannot
// reach above the embedding package's directory, so the embedded copy sits
// beside that package.
const (
noticesPath = "THIRD-PARTY-LICENSES.md"
embeddedPath = "internal/app/uberbringer/licenses_embed.md"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "genlicenses:", err)
os.Exit(1)
}
}
func run() error {
root, err := moduleRoot()
if err != nil {
return err
}
out, err := generate(root)
if err != nil {
return err
}
for _, rel := range []string{noticesPath, embeddedPath} {
if err := writeIfChanged(root, rel, out); err != nil {
return err
}
}
return nil
}
// generate returns the notices for the binary built from the module at root.
func generate(root string) ([]byte, error) {
goroot, err := goOutput(root, nil, "env", "GOROOT")
if err != nil {
return nil, err
}
goroot = strings.TrimSpace(goroot)
goFiles, err := licenseFiles(goroot)
if err != nil {
return nil, err
}
goComponent, err := readComponent("Go standard library and runtime", "", goroot, goFiles)
if err != nil {
return nil, err
}
components := []component{goComponent}
modules, err := linkedModules(root)
if err != nil {
return nil, err
}
for _, m := range modules {
c, err := readComponent(m.path, m.version, m.dir, m.files)
if err != nil {
return nil, err
}
components = append(components, c)
}
return render(components), nil
}
// moduleRoot returns the directory of the main module, which holds go.mod.
func moduleRoot() (string, error) {
out, err := goOutput("", nil, "list", "-m", "-f", "{{.Dir}}")
if err != nil {
return "", err
}
return strings.TrimSpace(out), nil
}
// goOutput runs the go command in dir, with env added to the environment,
// and returns what it writes to standard output.
func goOutput(dir string, env []string, args ...string) (string, error) {
cmd := exec.Command("go", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), env...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("go %s: %w\n%s", strings.Join(args, " "), err, stderr.Bytes())
}
return string(out), nil
}
// writeIfChanged writes out to the file rel under root unless it already
// holds out, and says which it did.
func writeIfChanged(root, rel string, out []byte) error {
path := filepath.Join(root, filepath.FromSlash(rel))
current, err := os.ReadFile(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
if bytes.Equal(current, out) {
fmt.Printf("genlicenses: %s is up to date\n", rel)
return nil
}
if err := os.WriteFile(path, out, 0o644); err != nil {
return err
}
fmt.Printf("genlicenses: wrote %s\n", rel)
return nil
}