Skip to content

Middleware

Type Signature Purpose
Middleware[T] func(ctx context.Context, cfg *T, info CommandMetadata, next func() error) error wrap handler execution
ContextMiddleware[T] func(ctx context.Context, cfg *T, info CommandMetadata, next func(context.Context) error) error derive a context for the inner chain
CommandMetadata Name, FullPath, Phase, HasRunE identifies the command and phase
Phase PhaseRun, PhasePreRun, PhasePostRun execution stage reported in metadata

Middleware must call next exactly once. To short-circuit, return before calling it.

cli, err := v5.NewCLI[AppConfig]("app", "My app", AppConfig{},
v5.WithContextMiddleware[AppConfig](v5.TimeoutMiddleware[AppConfig](30*time.Second)),
v5.WithMiddleware[AppConfig](v5.RecoveryMiddleware[AppConfig]()),
)

Plain middleware run in registration order: the first registered is outermost. Context middleware wrap the entire chain, so a context derived by a ContextMiddleware reaches every plain middleware and the handler. That ordering is what makes timeout and request-scoped values work.

func Audit(logger *slog.Logger) v5.Middleware[AppConfig] {
return func(ctx context.Context, cfg *AppConfig, info v5.CommandMetadata, next func() error) error {
logger.Info("command start", "command", info.FullPath, "phase", info.Phase)
err := next()
logger.Info("command end", "command", info.FullPath, "err", err)
return err
}
}

info.HasRunE reports whether the command has a handler, and info.FullPath is the full command path such as app tasks list. A middleware that wants the config reads it from cfg.

package main
import (
"context"
"fmt"
"os"
"time"
"github.com/larsartmann/cmdguard/v5/pkg/cmdguard/v5"
)
type AppConfig struct {
Verbose bool `flag:"verbose" short:"v" help:"Verbose output"`
}
type GreetFlags struct {
Name string `flag:"name" short:"n" default:"World" help:"Name to greet"`
}
func main() {
cli, err := v5.NewCLI[AppConfig]("greeter", "Greet people", AppConfig{},
v5.WithMiddleware[AppConfig](
v5.RecoveryMiddleware[AppConfig](),
v5.TimingMiddleware[AppConfig](func(name string, d time.Duration, err error) {
fmt.Fprintf(os.Stderr, "%s took %s (err=%v)\n", name, d, err)
}),
),
)
if err != nil {
fmt.Fprintf(os.Stderr, "creating CLI: %v\n", err)
os.Exit(1)
}
greet, err := v5.NewCommand("greet", GreetFlags{},
func(_ context.Context, cfg *AppConfig, flags GreetFlags) error {
fmt.Printf("Hello, %s! (verbose=%v)\n", flags.Name, cfg.Verbose)
return nil
},
v5.WithShort("Greet someone"),
)
b := v5.NewBuilder(cli)
v5.Try(b, greet, err)
if buildErr := b.Build(); buildErr != nil {
fmt.Fprintf(os.Stderr, "registering commands: %v\n", buildErr)
os.Exit(1)
}
cli.ExecuteAndExit(context.Background())
}

A context middleware can attach request-scoped values or deadlines that the handler sees:

type requestIDKey struct{}
func WithRequestID() v5.ContextMiddleware[AppConfig] {
return func(ctx context.Context, _ *AppConfig, _ v5.CommandMetadata, next func(context.Context) error) error {
return next(context.WithValue(ctx, requestIDKey{}, "req-123"))
}
}

Inside a handler, read the value with ctx.Value(requestIDKey{}). This propagation is the reason ContextMiddleware exists: plain Middleware receives a next func() error bound to the original context.

Constructor Type Behavior
TimingMiddleware[T](log) Middleware[T] measures next() and reports name, duration, and error to log
TimeoutMiddleware[T](d) ContextMiddleware[T] derives a deadline; maps context.DeadlineExceeded to ErrCommandTimeout; d <= 0 disables the limit
RecoveryMiddleware[T]() Middleware[T] converts panics into ErrCommandPanic errors with a stack trace
timing := v5.TimingMiddleware[AppConfig](func(name string, d time.Duration, err error) {
slog.Info("command finished", "command", name, "duration", d, "err", err)
})
timeout := v5.TimeoutMiddleware[AppConfig](5 * time.Second)

TimeoutMiddleware guarantees the derived context and the error mapping, not forcible cancellation: a handler that ignores its context cannot be interrupted.

Module Constructor Notes
cmdguard/spinner spinner.Middleware[T](title) or spinner.MiddlewareWithConfig[T](cfg) writes to stderr, silently skipped when stderr is not a terminal
cmdguard/telemetry telemetry.Middleware[T](tracer) or telemetry.WithTelemetry[T](tracer) one OpenTelemetry span per phase, recording errors
import (
"github.com/larsartmann/cmdguard/spinner"
"github.com/larsartmann/cmdguard/telemetry"
)
cli, err := v5.NewCLI[AppConfig]("app", "My app", AppConfig{},
v5.WithMiddleware[AppConfig](
spinner.Middleware[AppConfig]("Working..."),
telemetry.Middleware[AppConfig](tracer),
),
)

telemetry.Middleware is a plain middleware, so its span context is not propagated into the handler. Wire a ContextMiddleware when downstream code needs the derived context.