docs: document every exported identifier and enforce it with godoclint

This commit is contained in:
Julien Neuhart
2026-10-30 17:24:21 +01:00
parent 3d62c3ec80
commit a052f017b3
16 changed files with 144 additions and 2 deletions

View File

@@ -32,10 +32,26 @@ linters:
- sloglint - sloglint
- usetesting - usetesting
- gocritic - gocritic
- godoclint
- wastedassign - wastedassign
- whitespace - whitespace
settings:
godoclint:
default: basic
enable:
- require-doc
- require-pkg-doc
- start-with-name
options:
require-doc:
ignore-unexported: true
exclusions: exclusions:
generated: lax generated: lax
rules:
# A verbatim copy of otelhttp's generated internal package.
- path: pkg/gotenberg/semconv/
linters:
- godoclint
presets: presets:
- comments - comments
- common-false-positives - common-false-positives

3
cmd/doc.go Normal file
View File

@@ -0,0 +1,3 @@
// Package gotenbergcmd runs Gotenberg with the modules its importer
// registers, see [Run].
package gotenbergcmd

View File

@@ -0,0 +1,2 @@
// Command gotenberg-chromium runs Gotenberg without LibreOffice.
package main

View File

@@ -0,0 +1,2 @@
// Command gotenberg-libreoffice runs Gotenberg without Chromium.
package main

2
cmd/gotenberg/doc.go Normal file
View File

@@ -0,0 +1,2 @@
// Command gotenberg runs Gotenberg with every standard module.
package main

View File

@@ -12,6 +12,9 @@ type gotenbergHandler struct {
loggerName string loggerName string
} }
// NewGotenbergHandler wraps next to add the logger name to each record and,
// when prefix is set, to prefix the keys of its fields, the correlation and
// trace fields aside.
func NewGotenbergHandler(next slog.Handler, prefix string) slog.Handler { func NewGotenbergHandler(next slog.Handler, prefix string) slog.Handler {
return &gotenbergHandler{Handler: next, fieldsPrefix: prefix} return &gotenbergHandler{Handler: next, fieldsPrefix: prefix}
} }
@@ -104,6 +107,8 @@ type multiHandler struct {
handlers []slog.Handler handlers []slog.Handler
} }
// FanOut returns a [slog.Handler] that hands each record to every handler that
// accepts its level.
func FanOut(handlers ...slog.Handler) slog.Handler { func FanOut(handlers ...slog.Handler) slog.Handler {
return &multiHandler{handlers: handlers} return &multiHandler{handlers: handlers}
} }
@@ -150,6 +155,8 @@ type levelHandler struct {
level slog.Level level slog.Level
} }
// LevelFilter returns a [slog.Handler] that drops the records of next below
// level.
func LevelFilter(next slog.Handler, level slog.Level) slog.Handler { func LevelFilter(next slog.Handler, level slog.Level) slog.Handler {
return &levelHandler{Handler: next, level: level} return &levelHandler{Handler: next, level: level}
} }

View File

@@ -49,12 +49,12 @@ func (h traceContextHandler) WithGroup(name string) slog.Handler {
return traceContextHandler{Handler: h.Handler.WithGroup(name)} return traceContextHandler{Handler: h.Handler.WithGroup(name)}
} }
// NewStdHandler returns a [slog.Handler] instance for the standard output.
// upperLevelCase is the value of the level-case setting that keeps the level // upperLevelCase is the value of the level-case setting that keeps the level
// field uppercase in the standard output. It mirrors gotenberg.UpperLevelCase, // field uppercase in the standard output. It mirrors gotenberg.UpperLevelCase,
// duplicated here because the internal log package cannot import gotenberg. // duplicated here because the internal log package cannot import gotenberg.
const upperLevelCase = "upper" const upperLevelCase = "upper"
// NewStdHandler returns a [slog.Handler] instance for the standard output.
func NewStdHandler(level slog.Level, format string, fieldsPrefix string, enableGcpFields bool, levelCase string) (slog.Handler, error) { func NewStdHandler(level slog.Level, format string, fieldsPrefix string, enableGcpFields bool, levelCase string) (slog.Handler, error) {
// #nosec: G115 // #nosec: G115
isTerminal := term.IsTerminal(int(os.Stdout.Fd())) isTerminal := term.IsTerminal(int(os.Stdout.Fd()))

View File

@@ -11,6 +11,7 @@ type ModuleMock struct {
DescriptorMock func() ModuleDescriptor DescriptorMock func() ModuleDescriptor
} }
// Descriptor calls DescriptorMock.
func (mod *ModuleMock) Descriptor() ModuleDescriptor { func (mod *ModuleMock) Descriptor() ModuleDescriptor {
return mod.DescriptorMock() return mod.DescriptorMock()
} }
@@ -20,6 +21,7 @@ type ProvisionerMock struct {
ProvisionMock func(*Context) error ProvisionMock func(*Context) error
} }
// Provision calls ProvisionMock.
func (mod *ProvisionerMock) Provision(ctx *Context) error { func (mod *ProvisionerMock) Provision(ctx *Context) error {
return mod.ProvisionMock(ctx) return mod.ProvisionMock(ctx)
} }
@@ -29,14 +31,17 @@ type ValidatorMock struct {
ValidateMock func() error ValidateMock func() error
} }
// Validate calls ValidateMock.
func (mod *ValidatorMock) Validate() error { func (mod *ValidatorMock) Validate() error {
return mod.ValidateMock() return mod.ValidateMock()
} }
// DebuggableMock is a mock for the [Debuggable] interface.
type DebuggableMock struct { type DebuggableMock struct {
DebugMock func() map[string]any DebugMock func() map[string]any
} }
// Debug calls DebugMock.
func (mod *DebuggableMock) Debug() map[string]any { func (mod *DebuggableMock) Debug() map[string]any {
return mod.DebugMock() return mod.DebugMock()
} }
@@ -48,14 +53,17 @@ type ProcessMock struct {
HealthyMock func(logger *slog.Logger) bool HealthyMock func(logger *slog.Logger) bool
} }
// Start calls StartMock.
func (p *ProcessMock) Start(ctx context.Context, logger *slog.Logger) error { func (p *ProcessMock) Start(ctx context.Context, logger *slog.Logger) error {
return p.StartMock(ctx, logger) return p.StartMock(ctx, logger)
} }
// Stop calls StopMock.
func (p *ProcessMock) Stop(logger *slog.Logger) error { func (p *ProcessMock) Stop(logger *slog.Logger) error {
return p.StopMock(logger) return p.StopMock(logger)
} }
// Healthy calls HealthyMock.
func (p *ProcessMock) Healthy(logger *slog.Logger) bool { func (p *ProcessMock) Healthy(logger *slog.Logger) bool {
return p.HealthyMock(logger) return p.HealthyMock(logger)
} }
@@ -68,18 +76,22 @@ type ProcessSupervisorMock struct {
RunMock func(ctx context.Context, logger *slog.Logger, task func() error) error RunMock func(ctx context.Context, logger *slog.Logger, task func() error) error
} }
// Launch calls LaunchMock.
func (s *ProcessSupervisorMock) Launch(ctx context.Context) error { func (s *ProcessSupervisorMock) Launch(ctx context.Context) error {
return s.LaunchMock(ctx) return s.LaunchMock(ctx)
} }
// Shutdown calls ShutdownMock.
func (s *ProcessSupervisorMock) Shutdown() error { func (s *ProcessSupervisorMock) Shutdown() error {
return s.ShutdownMock() return s.ShutdownMock()
} }
// Healthy calls HealthyMock.
func (s *ProcessSupervisorMock) Healthy() bool { func (s *ProcessSupervisorMock) Healthy() bool {
return s.HealthyMock() return s.HealthyMock()
} }
// Run calls RunMock.
func (s *ProcessSupervisorMock) Run(ctx context.Context, logger *slog.Logger, task func() error) error { func (s *ProcessSupervisorMock) Run(ctx context.Context, logger *slog.Logger, task func() error) error {
return s.RunMock(ctx, logger, task) return s.RunMock(ctx, logger, task)
} }
@@ -89,6 +101,7 @@ type MkdirAllMock struct {
MkdirAllMock func(path string, perm os.FileMode) error MkdirAllMock func(path string, perm os.FileMode) error
} }
// MkdirAll calls MkdirAllMock.
func (mkdirAll *MkdirAllMock) MkdirAll(path string, perm os.FileMode) error { func (mkdirAll *MkdirAllMock) MkdirAll(path string, perm os.FileMode) error {
return mkdirAll.MkdirAllMock(path, perm) return mkdirAll.MkdirAllMock(path, perm)
} }
@@ -98,6 +111,7 @@ type PathRenameMock struct {
RenameMock func(oldpath, newpath string) error RenameMock func(oldpath, newpath string) error
} }
// Rename calls RenameMock.
func (rename *PathRenameMock) Rename(oldpath, newpath string) error { func (rename *PathRenameMock) Rename(oldpath, newpath string) error {
return rename.RenameMock(oldpath, newpath) return rename.RenameMock(oldpath, newpath)
} }

View File

@@ -25,14 +25,18 @@ const (
// https://github.com/gotenberg/gotenberg/issues/1287. // https://github.com/gotenberg/gotenberg/issues/1287.
type AlphanumericSort []string type AlphanumericSort []string
// Len implements [sort.Interface].
func (s AlphanumericSort) Len() int { func (s AlphanumericSort) Len() int {
return len(s) return len(s)
} }
// Swap implements [sort.Interface].
func (s AlphanumericSort) Swap(i, j int) { func (s AlphanumericSort) Swap(i, j int) {
s[i], s[j] = s[j], s[i] s[i], s[j] = s[j], s[i]
} }
// Less implements [sort.Interface]. It orders by number first, see
// [AlphanumericSort].
func (s AlphanumericSort) Less(i, j int) bool { func (s AlphanumericSort) Less(i, j int) bool {
numI, restI, locI := extractNumber(s[i]) numI, restI, locI := extractNumber(s[i])
numJ, restJ, locJ := extractNumber(s[j]) numJ, restJ, locJ := extractNumber(s[j])

View File

@@ -16,12 +16,14 @@ import (
internalotel "github.com/gotenberg/gotenberg/v9/pkg/gotenberg/internal/otel" internalotel "github.com/gotenberg/gotenberg/v9/pkg/gotenberg/internal/otel"
) )
// The formats of the standard logger, see [TelemetryConfig].
const ( const (
AutoLoggingFormat = "auto" AutoLoggingFormat = "auto"
JsonLoggingFormat = "json" JsonLoggingFormat = "json"
TextLoggingFormat = "text" TextLoggingFormat = "text"
) )
// The logging levels, see [TelemetryConfig].
const ( const (
ErrorLoggingLevel = "error" ErrorLoggingLevel = "error"
WarnLoggingLevel = "warn" WarnLoggingLevel = "warn"
@@ -29,6 +31,7 @@ const (
DebugLoggingLevel = "debug" DebugLoggingLevel = "debug"
) )
// The cases of the level the standard logger writes, see [TelemetryConfig].
const ( const (
LowerLevelCase = "lower" LowerLevelCase = "lower"
UpperLevelCase = "upper" UpperLevelCase = "upper"

View File

@@ -121,8 +121,12 @@ type MiddlewareProvider interface {
type MiddlewareStack uint32 type MiddlewareStack uint32
const ( const (
// DefaultStack runs the middleware after routing, on every route.
DefaultStack MiddlewareStack = iota DefaultStack MiddlewareStack = iota
// PreRouterStack runs the middleware before routing, on every request.
PreRouterStack PreRouterStack
// MultipartStack runs the middleware on the multipart/form-data routes
// only, after the [Context] is built.
MultipartStack MultipartStack
) )
@@ -130,6 +134,7 @@ const (
// middlewares provided by the [MiddlewareProvider] modules in a stack. // middlewares provided by the [MiddlewareProvider] modules in a stack.
type MiddlewarePriority uint32 type MiddlewarePriority uint32
// The priorities, from the last middleware of a stack to run to the first.
const ( const (
VeryLowPriority MiddlewarePriority = iota VeryLowPriority MiddlewarePriority = iota
LowPriority LowPriority

View File

@@ -0,0 +1,72 @@
package api_test
import (
"fmt"
"net/http"
"github.com/labstack/echo/v5"
"github.com/gotenberg/gotenberg/v9/pkg/gotenberg"
"github.com/gotenberg/gotenberg/v9/pkg/modules/api"
)
// Stamp is a module adding the multipart/form-data route /forms/stamp. A
// custom build registers it in an init function, then imports its package,
// next to github.com/gotenberg/gotenberg/v9/pkg/standard, in a main calling
// github.com/gotenberg/gotenberg/v9/cmd.Run.
type Stamp struct{}
// Descriptor returns the module's descriptor.
func (Stamp) Descriptor() gotenberg.ModuleDescriptor {
return gotenberg.ModuleDescriptor{
ID: "stamp",
New: func() gotenberg.Module { return new(Stamp) },
}
}
// Routes returns the module's routes.
func (Stamp) Routes() ([]api.Route, error) {
return []api.Route{
{
Method: http.MethodPost,
Path: "/forms/stamp",
IsMultipart: true,
Handler: func(c *echo.Context) error {
// The API builds the context of a multipart route: the
// uploaded files, the form fields, and the working directory.
ctx := c.Get("context").(*api.Context)
var inputPaths []string
var text string
err := ctx.FormData().
MandatoryPaths([]string{".pdf"}, &inputPaths).
String("text", &text).
Validate()
if err != nil {
return fmt.Errorf("validate form data: %w", err)
}
// Write the results under ctx.GeneratePath(".pdf") and register
// them: the API sends them back, zipped if many, or to the
// webhook.
return ctx.AddOutputPaths(inputPaths...)
},
},
}, nil
}
// This example writes a module adding a route to the API. The interface
// guards make the compiler check that the module is what the application
// looks for.
func Example_router() {
var (
_ gotenberg.Module = (*Stamp)(nil)
_ api.Router = (*Stamp)(nil)
)
routes, _ := Stamp{}.Routes()
for _, route := range routes {
fmt.Println(route.Method, route.Path)
}
// Output: POST /forms/stamp
}

View File

@@ -109,6 +109,7 @@ type RouterMock struct {
RoutesMock func() ([]Route, error) RoutesMock func() ([]Route, error)
} }
// Routes calls RoutesMock.
func (router *RouterMock) Routes() ([]Route, error) { func (router *RouterMock) Routes() ([]Route, error) {
return router.RoutesMock() return router.RoutesMock()
} }
@@ -118,6 +119,7 @@ type MiddlewareProviderMock struct {
MiddlewaresMock func() ([]Middleware, error) MiddlewaresMock func() ([]Middleware, error)
} }
// Middlewares calls MiddlewaresMock.
func (provider *MiddlewareProviderMock) Middlewares() ([]Middleware, error) { func (provider *MiddlewareProviderMock) Middlewares() ([]Middleware, error) {
return provider.MiddlewaresMock() return provider.MiddlewaresMock()
} }
@@ -128,10 +130,12 @@ type HealthCheckerMock struct {
ReadyMock func(ctx context.Context) error ReadyMock func(ctx context.Context) error
} }
// Checks calls ChecksMock.
func (mod *HealthCheckerMock) Checks() ([]health.CheckerOption, error) { func (mod *HealthCheckerMock) Checks() ([]health.CheckerOption, error) {
return mod.ChecksMock() return mod.ChecksMock()
} }
// Ready calls ReadyMock.
func (mod *HealthCheckerMock) Ready(ctx context.Context) error { func (mod *HealthCheckerMock) Ready(ctx context.Context) error {
return mod.ReadyMock(ctx) return mod.ReadyMock(ctx)
} }

View File

@@ -14,10 +14,12 @@ type ApiMock struct {
ExtensionsMock func() []string ExtensionsMock func() []string
} }
// Pdf calls PdfMock.
func (api *ApiMock) Pdf(ctx context.Context, logger *slog.Logger, inputPath, outputPath string, options Options) error { func (api *ApiMock) Pdf(ctx context.Context, logger *slog.Logger, inputPath, outputPath string, options Options) error {
return api.PdfMock(ctx, logger, inputPath, outputPath, options) return api.PdfMock(ctx, logger, inputPath, outputPath, options)
} }
// Extensions calls ExtensionsMock.
func (api *ApiMock) Extensions() []string { func (api *ApiMock) Extensions() []string {
return api.ExtensionsMock() return api.ExtensionsMock()
} }
@@ -27,6 +29,7 @@ type ProviderMock struct {
LibreOfficeMock func() (Converter, error) LibreOfficeMock func() (Converter, error)
} }
// LibreOffice calls LibreOfficeMock.
func (provider *ProviderMock) LibreOffice() (Converter, error) { func (provider *ProviderMock) LibreOffice() (Converter, error) {
return provider.LibreOfficeMock() return provider.LibreOfficeMock()
} }

View File

@@ -46,13 +46,17 @@ func PruneOrphanedNetworks(ctx context.Context) (int, error) {
} }
var ( var (
// GotenbergDockerRepository is the repository of the image, such as
// "gotenberg/gotenberg".
GotenbergDockerRepository string GotenbergDockerRepository string
// GotenbergVersion is the version the image reports, such as in the // GotenbergVersion is the version the image reports, such as in the
// Gotenberg-Version header. // Gotenberg-Version header.
GotenbergVersion string GotenbergVersion string
// GotenbergVariant selects a variant image, such as "chromium", tagged // GotenbergVariant selects a variant image, such as "chromium", tagged
// <version>-<variant>. Empty selects the full image, tagged <version>. // <version>-<variant>. Empty selects the full image, tagged <version>.
GotenbergVariant string GotenbergVariant string
// GotenbergContainerPlatform is the platform of the image, such as
// "linux/amd64".
GotenbergContainerPlatform string GotenbergContainerPlatform string
) )

View File

@@ -51,6 +51,7 @@ func (s *scenario) reset(ctx context.Context) error {
return nil return nil
} }
// InitializeScenario registers the steps and hooks of a scenario.
func InitializeScenario(ctx *godog.ScenarioContext) { func InitializeScenario(ctx *godog.ScenarioContext) {
s := &scenario{} s := &scenario{}
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) { ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {