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>
This commit is contained in:
1 parent
ee65e72c44
commit
0c88b4ffd5
19 files changed
+4969
-23
No files matched your search
+5
-3
@@ -1,6 +1,6 @@
|
|||||||
# The image is built from the Go sources and the embedded OpenAPI document
|
# The image is built from the Go sources and the embedded OpenAPI document,
|
||||||
# alone; everything else, the local config.toml with its API secret
|
# and carries the license files; everything else, the local config.toml
|
||||||
# included, stays out of the build context.
|
# with its API secret included, stays out of the build context.
|
||||||
*
|
*
|
||||||
!go.mod
|
!go.mod
|
||||||
!go.sum
|
!go.sum
|
||||||
@@ -8,3 +8,5 @@
|
|||||||
!internal
|
!internal
|
||||||
!docs/embed.go
|
!docs/embed.go
|
||||||
!docs/swagger.json
|
!docs/swagger.json
|
||||||
|
!LICENSE.txt
|
||||||
|
!THIRD-PARTY-LICENSES.md
|
||||||
@@ -58,12 +58,14 @@ jobs:
|
|||||||
- name: staticcheck
|
- name: staticcheck
|
||||||
run: go tool staticcheck ./...
|
run: go tool staticcheck ./...
|
||||||
|
|
||||||
# docs/swagger.json is generated from the route table and its types; a
|
# docs/swagger.json is generated from the route table and its types,
|
||||||
# change to either must come with the regenerated document.
|
# and THIRD-PARTY-LICENSES.md and its embedded copy from the modules
|
||||||
- name: OpenAPI document up to date
|
# linked into the binary. A change to any of those must come with the
|
||||||
|
# regenerated files.
|
||||||
|
- name: Generated files up to date
|
||||||
run: |
|
run: |
|
||||||
go generate ./...
|
go generate ./...
|
||||||
git diff --exit-code -- docs/swagger.json
|
git diff --exit-code -- docs/swagger.json THIRD-PARTY-LICENSES.md internal/app/uberbringer/licenses_embed.md
|
||||||
|
|
||||||
# The limit is per package: a hung test fails with every goroutine's
|
# The limit is per package: a hung test fails with every goroutine's
|
||||||
# stack instead of being killed by the runner.
|
# stack instead of being killed by the runner.
|
||||||
|
|||||||
+3
-1
@@ -42,7 +42,7 @@ builds:
|
|||||||
archives:
|
archives:
|
||||||
- formats: [tar]
|
- formats: [tar]
|
||||||
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
|
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
|
||||||
# Listing files replaces goreleaser's defaults, so the license, README,
|
# Listing files replaces goreleaser's defaults, so the licenses, README,
|
||||||
# API reference, OpenAPI document and changelog are listed too. Each
|
# API reference, OpenAPI document and changelog are listed too. Each
|
||||||
# must exist; paths are kept, and install.sh takes
|
# must exist; paths are kept, and install.sh takes
|
||||||
# packaging/uberbringer.service from the unpacked archive.
|
# packaging/uberbringer.service from the unpacked archive.
|
||||||
@@ -50,6 +50,8 @@ archives:
|
|||||||
- src: LICENSE.txt
|
- src: LICENSE.txt
|
||||||
info: &commit-mtime
|
info: &commit-mtime
|
||||||
mtime: "{{ .CommitDate }}"
|
mtime: "{{ .CommitDate }}"
|
||||||
|
- src: THIRD-PARTY-LICENSES.md
|
||||||
|
info: *commit-mtime
|
||||||
- src: README.md
|
- src: README.md
|
||||||
info: *commit-mtime
|
info: *commit-mtime
|
||||||
- src: docs/api.md
|
- src: docs/api.md
|
||||||
|
|||||||
@@ -84,6 +84,12 @@ version may break callers; such changes are listed under **Breaking**.
|
|||||||
or decoded prints as `[image unavailable]` instead of failing the job,
|
or decoded prints as `[image unavailable]` instead of failing the job,
|
||||||
and one whose `width` is wider than the paper shrinks to it.
|
and one whose `width` is wider than the paper shrinks to it.
|
||||||
- `box` takes `"title_bold": true` to print its title bold.
|
- `box` takes `"title_bold": true` to print its title bold.
|
||||||
|
- Third-party licenses. `THIRD-PARTY-LICENSES.md` holds the license and
|
||||||
|
notice texts of the Go standard library and runtime and of every Go
|
||||||
|
module built into the binary. It comes with the release archives and in
|
||||||
|
the Docker image, which also gains `LICENSE.txt`; both are in
|
||||||
|
`/usr/share/licenses/uberbringer/`. `uberbringer licenses` prints the
|
||||||
|
same text from the binary.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,12 @@ root `Dockerfile`.
|
|||||||
its routes from the same table. Never edit the file by hand; regenerate it
|
its routes from the same table. Never edit the file by hand; regenerate it
|
||||||
in the same commit as any change to a route or to a request or response
|
in the same commit as any change to a route or to a request or response
|
||||||
type.
|
type.
|
||||||
|
- `THIRD-PARTY-LICENSES.md` and its copy `internal/app/uberbringer/licenses_embed.md`,
|
||||||
|
which the binary embeds and `uberbringer licenses` prints, are generated
|
||||||
|
(`go generate ./...`, which runs `tools/genlicenses`) from the license and
|
||||||
|
notice files of the Go distribution and of the modules that
|
||||||
|
`./cmd/uberbringer` links on each release platform. Never edit them by
|
||||||
|
hand; regenerate them in the same commit as any dependency change.
|
||||||
- `docs/design.md` explains why the daemon is shaped as it is and lists what
|
- `docs/design.md` explains why the daemon is shaped as it is and lists what
|
||||||
is deferred. Change it in the same commit as any code that departs from it.
|
is deferred. Change it in the same commit as any code that departs from it.
|
||||||
- `docs/plans/<feature>.md` holds a feature until it ships. A decision made
|
- `docs/plans/<feature>.md` holds a feature until it ships. A decision made
|
||||||
@@ -37,7 +43,10 @@ the wiring, and `server/` holds the HTTP API. `internal/pkg/*` holds the
|
|||||||
libraries. README § Development lists them. `tools/genopenapi` writes
|
libraries. README § Development lists them. `tools/genopenapi` writes
|
||||||
`docs/swagger.json`; the `docs` package embeds it, and `internal/app` hands
|
`docs/swagger.json`; the `docs` package embeds it, and `internal/app` hands
|
||||||
it to the server in its config, so the generator never builds the package
|
it to the server in its config, so the generator never builds the package
|
||||||
that embeds its own output. The module path is the bare
|
that embeds its own output. `tools/genlicenses` writes
|
||||||
|
`THIRD-PARTY-LICENSES.md` and the copy beside `internal/app/uberbringer`'s
|
||||||
|
`licenses.go`; it imports nothing from the module and learns the linked
|
||||||
|
modules from `go list`. The module path is the bare
|
||||||
`uberbringer`, so imports read `uberbringer/internal/...`. Imports point down:
|
`uberbringer`, so imports read `uberbringer/internal/...`. Imports point down:
|
||||||
|
|
||||||
- server → modules, printqueue, receipt, printer, escpos
|
- server → modules, printqueue, receipt, printer, escpos
|
||||||
@@ -62,7 +71,7 @@ works from PowerShell and fails from Git Bash.
|
|||||||
go mod tidy -diff
|
go mod tidy -diff
|
||||||
go vet ./...
|
go vet ./...
|
||||||
go tool staticcheck ./...
|
go tool staticcheck ./...
|
||||||
go generate ./... && git diff --exit-code -- docs/swagger.json # the OpenAPI document is up to date
|
go generate ./... && git diff --exit-code -- docs/swagger.json THIRD-PARTY-LICENSES.md internal/app/uberbringer/licenses_embed.md # generated files are up to date
|
||||||
go test -race -timeout 5m ./... *> "$env:TEMP\uberbringer-test.txt"; "exit $LASTEXITCODE"
|
go test -race -timeout 5m ./... *> "$env:TEMP\uberbringer-test.txt"; "exit $LASTEXITCODE"
|
||||||
go tool govulncheck ./... # after any dependency or toolchain change, and before a tag
|
go tool govulncheck ./... # after any dependency or toolchain change, and before a tag
|
||||||
goreleaser release --snapshot --clean # into dist/; after a change to .goreleaser.yaml or packaging/
|
goreleaser release --snapshot --clean # into dist/; after a change to .goreleaser.yaml or packaging/
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ RUN apk add --no-cache ca-certificates tzdata \
|
|||||||
&& chown uberbringer:uberbringer /var/lib/uberbringer \
|
&& chown uberbringer:uberbringer /var/lib/uberbringer \
|
||||||
&& chmod 0700 /var/lib/uberbringer
|
&& chmod 0700 /var/lib/uberbringer
|
||||||
COPY --from=build /out/uberbringer /usr/local/bin/uberbringer
|
COPY --from=build /out/uberbringer /usr/local/bin/uberbringer
|
||||||
|
# Where Alpine's own packages keep their license files.
|
||||||
|
COPY LICENSE.txt THIRD-PARTY-LICENSES.md /usr/share/licenses/uberbringer/
|
||||||
USER uberbringer
|
USER uberbringer
|
||||||
WORKDIR /etc/uberbringer
|
WORKDIR /etc/uberbringer
|
||||||
# Plain HTTP for a reverse proxy in front, which terminates TLS. Mount
|
# Plain HTTP for a reverse proxy in front, which terminates TLS. Mount
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
.PHONY: build test lint fmt fmt-check vet tidy tidy-check vuln openapi openapi-check release-check snapshot check run
|
.PHONY: build test lint fmt fmt-check vet tidy tidy-check vuln generate generate-check release-check snapshot check run
|
||||||
|
|
||||||
# Without the leading v, as goreleaser's {{ .Version }} reports it.
|
# Without the leading v, as goreleaser's {{ .Version }} reports it.
|
||||||
VERSION ?= $(patsubst v%,%,$(shell git describe --tags --always --dirty 2>/dev/null || echo dev))
|
VERSION ?= $(patsubst v%,%,$(shell git describe --tags --always --dirty 2>/dev/null || echo dev))
|
||||||
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo none)
|
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo none)
|
||||||
LDFLAGS := -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT)
|
LDFLAGS := -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT)
|
||||||
|
|
||||||
|
# The files go generate writes.
|
||||||
|
GENERATED := docs/swagger.json THIRD-PARTY-LICENSES.md internal/app/uberbringer/licenses_embed.md
|
||||||
|
|
||||||
build:
|
build:
|
||||||
go build -trimpath -ldflags "$(LDFLAGS)" -o bin/ ./cmd/...
|
go build -trimpath -ldflags "$(LDFLAGS)" -o bin/ ./cmd/...
|
||||||
|
|
||||||
@@ -37,13 +40,15 @@ tidy-check:
|
|||||||
vuln:
|
vuln:
|
||||||
go tool govulncheck ./...
|
go tool govulncheck ./...
|
||||||
|
|
||||||
# Runs every go:generate directive, which writes docs/swagger.json.
|
# Runs every go:generate directive, which writes docs/swagger.json,
|
||||||
openapi:
|
# THIRD-PARTY-LICENSES.md and the copy of it that the binary embeds.
|
||||||
|
generate:
|
||||||
go generate ./...
|
go generate ./...
|
||||||
|
|
||||||
# Fails when docs/swagger.json differs from what the route table produces.
|
# Fails when a generated file differs from what the route table and the
|
||||||
openapi-check: openapi
|
# linked modules produce.
|
||||||
git diff --exit-code -- docs/swagger.json
|
generate-check: generate
|
||||||
|
git diff --exit-code -- $(GENERATED)
|
||||||
|
|
||||||
# Validates .goreleaser.yaml without building anything.
|
# Validates .goreleaser.yaml without building anything.
|
||||||
release-check:
|
release-check:
|
||||||
@@ -57,7 +62,7 @@ snapshot:
|
|||||||
|
|
||||||
# The gate .gitea/workflows/test.yml runs, in the same order.
|
# The gate .gitea/workflows/test.yml runs, in the same order.
|
||||||
# .gitea/workflows/security.yml runs vuln.
|
# .gitea/workflows/security.yml runs vuln.
|
||||||
check: fmt-check tidy-check vet lint openapi-check test release-check
|
check: fmt-check tidy-check vet lint generate-check test release-check
|
||||||
|
|
||||||
run:
|
run:
|
||||||
go run ./cmd/uberbringer --config config.toml
|
go run ./cmd/uberbringer --config config.toml
|
||||||
@@ -496,6 +496,7 @@ uberbringer preview [file.json|-] show the lines a job would print
|
|||||||
uberbringer preview --module note [file|-] show the receipt a module builds
|
uberbringer preview --module note [file|-] show the receipt a module builds
|
||||||
uberbringer config get <key> print one setting, e.g. webserver.port
|
uberbringer config get <key> print one setting, e.g. webserver.port
|
||||||
uberbringer version print the version
|
uberbringer version print the version
|
||||||
|
uberbringer licenses print the licenses of the third-party software
|
||||||
```
|
```
|
||||||
|
|
||||||
`token` signs a token with `api.secret`. Without flags, it may use every
|
`token` signs a token with `api.secret`. Without flags, it may use every
|
||||||
@@ -547,7 +548,7 @@ make check # the CI gate, below
|
|||||||
make build # bin/uberbringer
|
make build # bin/uberbringer
|
||||||
make fmt # gofmt + goimports
|
make fmt # gofmt + goimports
|
||||||
make vuln # govulncheck
|
make vuln # govulncheck
|
||||||
make openapi # regenerate docs/swagger.json (go generate ./...)
|
make generate # regenerate docs/swagger.json and the license notices (go generate ./...)
|
||||||
make snapshot # release archives in dist/; publishes nothing
|
make snapshot # release archives in dist/; publishes nothing
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -558,8 +559,8 @@ order:
|
|||||||
2. `tidy-check`: `go mod tidy -diff`.
|
2. `tidy-check`: `go mod tidy -diff`.
|
||||||
3. `go vet`.
|
3. `go vet`.
|
||||||
4. staticcheck.
|
4. staticcheck.
|
||||||
5. `openapi-check`: `go generate ./...` must leave `docs/swagger.json`
|
5. `generate-check`: `go generate ./...` must leave `docs/swagger.json`,
|
||||||
unchanged.
|
`THIRD-PARTY-LICENSES.md` and its embedded copy unchanged.
|
||||||
6. `go test -race -timeout 5m`.
|
6. `go test -race -timeout 5m`.
|
||||||
7. `release-check`: `goreleaser check`, which validates the release config
|
7. `release-check`: `goreleaser check`, which validates the release config
|
||||||
without building.
|
without building.
|
||||||
@@ -577,6 +578,19 @@ name: their `json` and `jsonschema` tags and their doc comments. Never edit
|
|||||||
the file by hand: change the route table or the types, run
|
the file by hand: change the route table or the types, run
|
||||||
`go generate ./...`, and commit the document with the change.
|
`go generate ./...`, and commit the document with the change.
|
||||||
|
|
||||||
|
`THIRD-PARTY-LICENSES.md` is generated by `tools/genlicenses`. It holds the
|
||||||
|
license and notice files of the Go distribution and of every module that
|
||||||
|
`./cmd/uberbringer` links on the release platforms. A module's files come
|
||||||
|
from its root and from the directories of its linked packages, which cover
|
||||||
|
code it vendors under licenses of their own. Modules that only the tools and
|
||||||
|
tests use are left out. The generator writes the same text to
|
||||||
|
`internal/app/uberbringer/licenses_embed.md`, which the binary embeds and
|
||||||
|
`uberbringer licenses` prints. After adding, bumping or dropping a
|
||||||
|
dependency, run `go generate ./...` and commit both files with `go.mod`.
|
||||||
|
The generator fails on a module that ships no license file, or whose
|
||||||
|
license it does not recognize; teach `licenseMarkers` in
|
||||||
|
`tools/genlicenses/notices.go` its wording once the license is acceptable.
|
||||||
|
|
||||||
The linters are pinned as Go tools in `go.mod`, so `go tool staticcheck`,
|
The linters are pinned as Go tools in `go.mod`, so `go tool staticcheck`,
|
||||||
`go tool govulncheck` and `go tool goimports` need no separate install. The
|
`go tool govulncheck` and `go tool goimports` need no separate install. The
|
||||||
module path is `uberbringer`, so packages import as
|
module path is `uberbringer`, so packages import as
|
||||||
@@ -598,6 +612,7 @@ internal/pkg/printqueue/ Job queue, worker and status history
|
|||||||
configs/ Example configuration
|
configs/ Example configuration
|
||||||
examples/ Example jobs, such as the block calibration receipt
|
examples/ Example jobs, such as the block calibration receipt
|
||||||
packaging/ systemd unit
|
packaging/ systemd unit
|
||||||
|
tools/genlicenses/ Generates THIRD-PARTY-LICENSES.md and its embedded copy from the linked modules
|
||||||
tools/genopenapi/ Generates docs/swagger.json from the route table
|
tools/genopenapi/ Generates docs/swagger.json from the route table
|
||||||
docs/ API reference, OpenAPI document, design notes and feature plans
|
docs/ API reference, OpenAPI document, design notes and feature plans
|
||||||
```
|
```
|
||||||
@@ -611,7 +626,7 @@ CI runs on the Gitea runner from `.gitea/workflows/`. The repository has no
|
|||||||
`.github/` directory.
|
`.github/` directory.
|
||||||
|
|
||||||
- `test.yml` runs the `make check` gate on every branch push. It lints,
|
- `test.yml` runs the `make check` gate on every branch push. It lints,
|
||||||
checks that `docs/swagger.json` is up to date, tests and validates the
|
checks that the generated files are up to date, tests and validates the
|
||||||
release config, but builds no binaries, and it does not run on pull
|
release config, but builds no binaries, and it does not run on pull
|
||||||
requests or tags. Binaries are built only by `release.yml`.
|
requests or tags. Binaries are built only by `release.yml`.
|
||||||
- `security.yml` runs govulncheck on pushes to main and every Monday at
|
- `security.yml` runs govulncheck on pushes to main and every Monday at
|
||||||
@@ -621,8 +636,8 @@ CI runs on the Gitea runner from `.gitea/workflows/`. The repository has no
|
|||||||
- It runs only for tags whose commit is on main, and refuses any other tag.
|
- It runs only for tags whose commit is on main, and refuses any other tag.
|
||||||
- `vX.Y.Z-rc.N` tags are published as prereleases, so they never become the
|
- `vX.Y.Z-rc.N` tags are published as prereleases, so they never become the
|
||||||
latest release that `install.sh` installs.
|
latest release that `install.sh` installs.
|
||||||
- Each archive holds the binary, `LICENSE.txt`, `README.md`,
|
- Each archive holds the binary, `LICENSE.txt`, `THIRD-PARTY-LICENSES.md`,
|
||||||
`docs/api.md`, `docs/swagger.json`, `CHANGELOG.md`,
|
`README.md`, `docs/api.md`, `docs/swagger.json`, `CHANGELOG.md`,
|
||||||
`configs/config.example.toml` and `packaging/uberbringer.service`.
|
`configs/config.example.toml` and `packaging/uberbringer.service`.
|
||||||
- The release notes link to `CHANGELOG.md` at the tag.
|
- The release notes link to `CHANGELOG.md` at the tag.
|
||||||
|
|
||||||
@@ -634,3 +649,10 @@ Licensed under the European Union Public Licence v. 1.2 (EUPL-1.2). The full
|
|||||||
text is in [LICENSE.txt](LICENSE.txt), and translations into every official EU
|
text is in [LICENSE.txt](LICENSE.txt), and translations into every official EU
|
||||||
language are equally valid. Releases up to and including 0.1.2 were published
|
language are equally valid. Releases up to and including 0.1.2 were published
|
||||||
under the GNU Affero General Public License v3.0.
|
under the GNU Affero General Public License v3.0.
|
||||||
|
|
||||||
|
The binary contains third-party software under licenses of its own: the Go
|
||||||
|
standard library and runtime, and Go modules.
|
||||||
|
[THIRD-PARTY-LICENSES.md](THIRD-PARTY-LICENSES.md) lists them with their
|
||||||
|
license and notice texts. The file comes with every release archive, and the Docker
|
||||||
|
image keeps it with `LICENSE.txt` in `/usr/share/licenses/uberbringer/`.
|
||||||
|
`uberbringer licenses` prints the same text.
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -292,6 +292,11 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue (
|
|||||||
- The Docker image is a microservice with no proxy inside: it runs behind
|
- The Docker image is a microservice with no proxy inside: it runs behind
|
||||||
the operator's own reverse proxy, whose address goes in
|
the operator's own reverse proxy, whose address goes in
|
||||||
`webserver.trusted_proxies`.
|
`webserver.trusted_proxies`.
|
||||||
|
- Third-party notices are generated from what `go list -deps` says the
|
||||||
|
binary links on each release platform, not from `go.mod`, so modules
|
||||||
|
that only tools and tests use stay out. The binary embeds them and
|
||||||
|
prints them with `uberbringer licenses`, so a host that kept only the
|
||||||
|
binary still has them. They are not served over HTTP.
|
||||||
|
|
||||||
- **ntfy subscriptions run in the daemon.** ntfy is an input: its
|
- **ntfy subscriptions run in the daemon.** ntfy is an input: its
|
||||||
notifications are printed (owner, 2026-11-29). The owner's server is
|
notifications are printed (owner, 2026-11-29). The owner's server is
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ func newRootCommand(build BuildInfo) *cobra.Command {
|
|||||||
newTokenCommand(&configPath),
|
newTokenCommand(&configPath),
|
||||||
newPreviewCommand(&configPath),
|
newPreviewCommand(&configPath),
|
||||||
newConfigCommand(&configPath),
|
newConfigCommand(&configPath),
|
||||||
|
newLicensesCommand(),
|
||||||
)
|
)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,6 +79,21 @@ func TestVersionCommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLicensesCommand(t *testing.T) {
|
||||||
|
out, err := runCommand(t, "licenses")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if out != thirdPartyLicenses {
|
||||||
|
t.Error("licenses output differs from the embedded notices")
|
||||||
|
}
|
||||||
|
for _, want := range []string{"# Third-party licenses", "## Go standard library and runtime", "## github.com/spf13/cobra "} {
|
||||||
|
if !strings.Contains(out, want) {
|
||||||
|
t.Errorf("licenses output lacks %q", want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSecretCommand(t *testing.T) {
|
func TestSecretCommand(t *testing.T) {
|
||||||
out, err := runCommand(t, "secret")
|
out, err := runCommand(t, "secret")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package uberbringer
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed" // for the go:embed directive below
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// thirdPartyLicenses is a copy of THIRD-PARTY-LICENSES.md, written beside
|
||||||
|
// this file by tools/genlicenses: the licenses and notices of the Go
|
||||||
|
// distribution and the modules linked into the binary. It must not be
|
||||||
|
// modified.
|
||||||
|
//
|
||||||
|
//go:embed licenses_embed.md
|
||||||
|
var thirdPartyLicenses string
|
||||||
|
|
||||||
|
func newLicensesCommand() *cobra.Command {
|
||||||
|
return &cobra.Command{
|
||||||
|
Use: "licenses",
|
||||||
|
Short: "Prints the licenses of the third-party software in the binary.",
|
||||||
|
Long: "Prints the licenses and notices of the Go standard library and the Go modules\n" +
|
||||||
|
"built into the binary, as Markdown. Überbringer itself is licensed under the\n" +
|
||||||
|
"EUPL-1.2.",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
_, err := io.WriteString(cmd.OutOrStdout(), thirdPartyLicenses)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// `go generate ./...` regenerates THIRD-PARTY-LICENSES.md and the copy the
|
||||||
|
// binary embeds. go generate runs each directive in its own package's
|
||||||
|
// directory, so the directive builds this package as ".".
|
||||||
|
//
|
||||||
|
//go:generate go run .
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
// 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
|
||||||
|
}
|
||||||
@@ -0,0 +1,205 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cmp"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// header opens the notices.
|
||||||
|
const header = `# Third-party licenses
|
||||||
|
|
||||||
|
Überbringer is licensed under the European Union Public Licence v. 1.2
|
||||||
|
(EUPL-1.2). Its text is in LICENSE.txt, which comes with every release.
|
||||||
|
|
||||||
|
The uberbringer binary also contains the Go standard library and runtime and
|
||||||
|
the Go modules listed below. Their license and notice files follow, as each
|
||||||
|
distribution ships them. A file in a subdirectory of a module covers the
|
||||||
|
code in that directory.
|
||||||
|
|
||||||
|
This file is generated by tools/genlicenses (go generate ./...) from the
|
||||||
|
modules linked into the binary. Do not edit it by hand.
|
||||||
|
`
|
||||||
|
|
||||||
|
// component is a part of the binary under licenses of its own.
|
||||||
|
type component struct {
|
||||||
|
name, version string
|
||||||
|
|
||||||
|
// licenses are SPDX identifiers, in the order the files give them.
|
||||||
|
licenses []string
|
||||||
|
|
||||||
|
files []licenseText
|
||||||
|
}
|
||||||
|
|
||||||
|
// licenseText is a license or notice file as its distribution ships it.
|
||||||
|
type licenseText struct {
|
||||||
|
path, body string
|
||||||
|
}
|
||||||
|
|
||||||
|
// readComponent reads the license and notice files, named relative to dir,
|
||||||
|
// that cover a component, and tells its licenses from them.
|
||||||
|
func readComponent(name, version, dir string, files []string) (component, error) {
|
||||||
|
c := component{name: name, version: version}
|
||||||
|
for _, f := range files {
|
||||||
|
b, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(f)))
|
||||||
|
if err != nil {
|
||||||
|
return component{}, fmt.Errorf("%s: %w", name, err)
|
||||||
|
}
|
||||||
|
body := normalize(string(b))
|
||||||
|
c.files = append(c.files, licenseText{path: f, body: body})
|
||||||
|
if isNoticeFile(path.Base(f)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ids := classify(body)
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return component{}, fmt.Errorf("%s: %s holds no license that licenseMarkers knows; add its wording there", name, f)
|
||||||
|
}
|
||||||
|
for _, id := range ids {
|
||||||
|
if !slices.Contains(c.licenses, id) {
|
||||||
|
c.licenses = append(c.licenses, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(c.licenses) == 0 {
|
||||||
|
return component{}, fmt.Errorf("%s ships no license file", name)
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalize gives a file LF line endings and drops the blank lines around
|
||||||
|
// it. Git keeps the generated files with LF endings, so a license file with
|
||||||
|
// CRLF endings would otherwise never match its committed copy.
|
||||||
|
func normalize(s string) string {
|
||||||
|
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||||
|
s = strings.TrimLeft(s, "\n")
|
||||||
|
return strings.TrimRight(s, " \t\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// licenseMarkers are phrases, in lower case and with single spaces, that
|
||||||
|
// the text of each license holds. "BSD" stands for BSD-2-Clause and
|
||||||
|
// BSD-3-Clause, which classify tells apart.
|
||||||
|
var licenseMarkers = []struct{ id, phrase string }{
|
||||||
|
{"Apache-2.0", "apache license version 2.0"},
|
||||||
|
{"Apache-2.0", "apache license, version 2.0"},
|
||||||
|
{"BSD", "redistribution and use in source and binary forms"},
|
||||||
|
{"ISC", "permission to use, copy, modify, and/or distribute this software for any purpose"},
|
||||||
|
{"MIT", "permission is hereby granted, free of charge, to any person obtaining a copy"},
|
||||||
|
{"MPL-2.0", "mozilla public license version 2.0"},
|
||||||
|
{"MPL-2.0", "mozilla public license, version 2.0"},
|
||||||
|
{"Unlicense", "this is free and unencumbered software released into the public domain"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// bsdThirdClause is in BSD-3-Clause and not in BSD-2-Clause.
|
||||||
|
const bsdThirdClause = "endorse or promote products derived from this software"
|
||||||
|
|
||||||
|
// classify returns the SPDX identifiers of the licenses a license file
|
||||||
|
// holds, in the order they appear, or nil when it holds none it knows. A
|
||||||
|
// file may hold several, such as a module's own license followed by those
|
||||||
|
// of code it includes.
|
||||||
|
func classify(body string) []string {
|
||||||
|
text := strings.Join(strings.Fields(strings.ToLower(body)), " ")
|
||||||
|
type hit struct {
|
||||||
|
at int
|
||||||
|
id string
|
||||||
|
}
|
||||||
|
var hits []hit
|
||||||
|
for _, m := range licenseMarkers {
|
||||||
|
for from := 0; ; {
|
||||||
|
i := strings.Index(text[from:], m.phrase)
|
||||||
|
if i < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
hits = append(hits, hit{from + i, m.id})
|
||||||
|
from += i + len(m.phrase)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slices.SortFunc(hits, func(a, b hit) int { return cmp.Compare(a.at, b.at) })
|
||||||
|
|
||||||
|
var ids []string
|
||||||
|
for i, h := range hits {
|
||||||
|
id := h.id
|
||||||
|
if id == "BSD" {
|
||||||
|
end := len(text)
|
||||||
|
if i+1 < len(hits) {
|
||||||
|
end = hits[i+1].at
|
||||||
|
}
|
||||||
|
id = "BSD-2-Clause"
|
||||||
|
if strings.Contains(text[h.at:end], bsdThirdClause) {
|
||||||
|
id = "BSD-3-Clause"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !slices.Contains(ids, id) {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
|
// render writes the notices: the header, a table of the components, and
|
||||||
|
// each component's files in full.
|
||||||
|
func render(components []component) []byte {
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString(header)
|
||||||
|
b.WriteString("\n| Component | Version | License |\n| --- | --- | --- |\n")
|
||||||
|
for _, c := range components {
|
||||||
|
fmt.Fprintf(&b, "| %s | %s | %s |\n", c.name, c.version, strings.Join(c.licenses, ", "))
|
||||||
|
}
|
||||||
|
for _, c := range components {
|
||||||
|
b.WriteString("\n## " + c.name)
|
||||||
|
if c.version != "" {
|
||||||
|
b.WriteString(" " + c.version)
|
||||||
|
}
|
||||||
|
label := "License"
|
||||||
|
if len(c.licenses) > 1 {
|
||||||
|
label = "Licenses"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&b, "\n\n%s: %s\n", label, strings.Join(c.licenses, ", "))
|
||||||
|
for _, f := range c.files {
|
||||||
|
fence := fence(f.body)
|
||||||
|
fmt.Fprintf(&b, "\n### %s\n\n%s\n%s\n%s\n", f.path, fence, f.body, fence)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []byte(b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// fence returns a code fence longer than any run of backticks in body, so
|
||||||
|
// that no text can close its block early.
|
||||||
|
func fence(body string) string {
|
||||||
|
longest, run := 0, 0
|
||||||
|
for _, r := range body {
|
||||||
|
if r != '`' {
|
||||||
|
run = 0
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
run++
|
||||||
|
longest = max(longest, run)
|
||||||
|
}
|
||||||
|
return strings.Repeat("`", max(3, longest+1))
|
||||||
|
}
|
||||||
@@ -0,0 +1,203 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
mitText = `Copyright (c) 2020 A. Author
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal`
|
||||||
|
|
||||||
|
bsd2Text = `Copyright (c) 2020 A. Author
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
1. Redistributions of source code must retain the above copyright notice.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice.`
|
||||||
|
|
||||||
|
bsd3Text = bsd2Text + `
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.`
|
||||||
|
|
||||||
|
apacheText = ` Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/`
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestClassify(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
body string
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{"MIT", mitText, []string{"MIT"}},
|
||||||
|
{"MIT on one line", strings.Join(strings.Fields(mitText), " "), []string{"MIT"}},
|
||||||
|
{"BSD-2-Clause", bsd2Text, []string{"BSD-2-Clause"}},
|
||||||
|
{"BSD-3-Clause", bsd3Text, []string{"BSD-3-Clause"}},
|
||||||
|
{"Apache-2.0 in full", apacheText, []string{"Apache-2.0"}},
|
||||||
|
{"Apache-2.0 by reference", `Licensed under the Apache License, Version 2.0 (the "License");`, []string{"Apache-2.0"}},
|
||||||
|
{"ISC", "Permission to use, copy, modify, and/or distribute this software for any\npurpose with or without fee is hereby granted", []string{"ISC"}},
|
||||||
|
{"MPL-2.0", "Mozilla Public License Version 2.0\n==================================", []string{"MPL-2.0"}},
|
||||||
|
{"Unlicense", "This is free and unencumbered software released into the public domain.", []string{"Unlicense"}},
|
||||||
|
{"in the order given", mitText + "\n\n" + apacheText, []string{"MIT", "Apache-2.0"}},
|
||||||
|
{"each named once", mitText + "\n\n" + bsd3Text + "\n\n" + mitText, []string{"MIT", "BSD-3-Clause"}},
|
||||||
|
{"BSD clauses read up to the next license", bsd2Text + "\n\n" + apacheText + "\n\n" + bsd3Text, []string{"BSD-2-Clause", "Apache-2.0", "BSD-3-Clause"}},
|
||||||
|
{"unknown", "All rights reserved.", nil},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := classify(tt.body); !slices.Equal(got, tt.want) {
|
||||||
|
t.Errorf("classify = %q, want %q", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileNames(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
license, notice bool
|
||||||
|
}{
|
||||||
|
{"LICENSE", true, false},
|
||||||
|
{"License.md", true, false},
|
||||||
|
{"LICENCE.txt", true, false},
|
||||||
|
{"LICENSE-MIT", true, false},
|
||||||
|
{"LICENSE-APACHE-2.0.txt", true, false},
|
||||||
|
{"COPYING", true, false},
|
||||||
|
{"UNLICENSE", true, false},
|
||||||
|
{"NOTICE", false, true},
|
||||||
|
{"notice.txt", false, true},
|
||||||
|
{"license.go", false, false},
|
||||||
|
{"licenses_test.go", false, false},
|
||||||
|
{"README.md", false, false},
|
||||||
|
{"PATENTS", false, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isLicenseFile(tt.name); got != tt.license {
|
||||||
|
t.Errorf("isLicenseFile = %v, want %v", got, tt.license)
|
||||||
|
}
|
||||||
|
if got := isNoticeFile(tt.name); got != tt.notice {
|
||||||
|
t.Errorf("isNoticeFile = %v, want %v", got, tt.notice)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalize(t *testing.T) {
|
||||||
|
got := normalize("\n\nCopyright\r\n\r\n indented\r\n\r\n")
|
||||||
|
if want := "Copyright\n\n indented"; got != want {
|
||||||
|
t.Errorf("normalize = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRenderFencesBackticks(t *testing.T) {
|
||||||
|
body := "Run ```go build``` and ````that````."
|
||||||
|
out := string(render([]component{{
|
||||||
|
name: "example.com/m", version: "v1.0.0", licenses: []string{"MIT"},
|
||||||
|
files: []licenseText{{path: "LICENSE", body: body}},
|
||||||
|
}}))
|
||||||
|
if want := "\n`````\n" + body + "\n`````\n"; !strings.Contains(out, want) {
|
||||||
|
t.Errorf("the text is not fenced by five backticks:\n%s", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadComponent(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
write := func(name, body string) {
|
||||||
|
t.Helper()
|
||||||
|
path := filepath.Join(dir, filepath.FromSlash(name))
|
||||||
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write("LICENSE", apacheText)
|
||||||
|
write("NOTICE", "Copyright 2020 A. Author")
|
||||||
|
write("vendored/LICENSE", mitText)
|
||||||
|
write("odd/LICENSE", "All rights reserved.")
|
||||||
|
|
||||||
|
t.Run("licenses from every license file", func(t *testing.T) {
|
||||||
|
c, err := readComponent("m", "v1.0.0", dir, []string{"LICENSE", "NOTICE", "vendored/LICENSE"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if want := []string{"Apache-2.0", "MIT"}; !slices.Equal(c.licenses, want) {
|
||||||
|
t.Errorf("licenses = %q, want %q", c.licenses, want)
|
||||||
|
}
|
||||||
|
if len(c.files) != 3 {
|
||||||
|
t.Errorf("got %d files, want 3", len(c.files))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("unknown license", func(t *testing.T) {
|
||||||
|
if _, err := readComponent("m", "v1.0.0", dir, []string{"LICENSE", "odd/LICENSE"}); err == nil {
|
||||||
|
t.Error("readComponent took a file with no license it knows")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("notice alone", func(t *testing.T) {
|
||||||
|
if _, err := readComponent("m", "v1.0.0", dir, []string{"NOTICE"}); err == nil {
|
||||||
|
t.Error("readComponent took a module with no license file")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// generated runs the generator once for the tests that need its output,
|
||||||
|
// which lists the linked modules with the go command.
|
||||||
|
var generated = sync.OnceValues(func() ([]byte, error) {
|
||||||
|
root, err := moduleRoot()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return generate(root)
|
||||||
|
})
|
||||||
|
|
||||||
|
// TestCommittedNoticesUpToDate fails when either committed copy of the
|
||||||
|
// notices differs from what the linked modules produce: regenerate with
|
||||||
|
// `go generate ./...`.
|
||||||
|
func TestCommittedNoticesUpToDate(t *testing.T) {
|
||||||
|
out, err := generated()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
root, err := moduleRoot()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, rel := range []string{noticesPath, embeddedPath} {
|
||||||
|
committed, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(rel)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(committed, out) {
|
||||||
|
t.Errorf("%s is out of date; run: go generate ./...", rel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modules that only the tools and the tests use are not linked into the
|
||||||
|
// binary, so they have no notices.
|
||||||
|
func TestNoticesLeaveOutUnlinkedModules(t *testing.T) {
|
||||||
|
out, err := generated()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, path := range []string{"golang.org/x/tools", "golang.org/x/vuln", "honnef.co/go/tools", "github.com/invopop/jsonschema"} {
|
||||||
|
if bytes.Contains(out, []byte("\n## "+path+" ")) {
|
||||||
|
t.Errorf("the notices include %s", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !bytes.Contains(out, []byte("\n## github.com/gofiber/fiber/v3 ")) {
|
||||||
|
t.Error("the notices lack github.com/gofiber/fiber/v3")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user