From 0fff894b5f24f9b3426aa434d267b13ab3c79c2f Mon Sep 17 00:00:00 2001 From: David Thorpe Date: Sun, 3 May 2026 08:03:19 +0200 Subject: [PATCH] Added child context for globals --- interfaces.go | 3 +++ pkg/cmd/global.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/interfaces.go b/interfaces.go index d8ea29f..c3d3305 100644 --- a/interfaces.go +++ b/interfaces.go @@ -79,6 +79,9 @@ type Cmd interface { // HTTPTimeout returns the HTTP read/write timeout. HTTPTimeout() time.Duration + + // WithContext returns a copy of the Cmd with the provided context. + WithContext(context.Context) Cmd } /////////////////////////////////////////////////////////////////////////////// diff --git a/pkg/cmd/global.go b/pkg/cmd/global.go index 311d703..9b0ddb6 100644 --- a/pkg/cmd/global.go +++ b/pkg/cmd/global.go @@ -59,6 +59,11 @@ type global struct { defaults } +type child struct { + server.Cmd + ctx context.Context +} + // Ensure *globals implements server.Cmd var _ server.Cmd = (*global)(nil) @@ -81,6 +86,10 @@ func (g *global) Context() context.Context { return g.ctx } +func (c *child) Context() context.Context { + return c.ctx +} + func (g *global) Logger() *slog.Logger { return g.logger } @@ -166,3 +175,11 @@ func (g *global) HTTPPrefix() string { func (g *global) HTTPTimeout() time.Duration { return g.HTTP.Timeout } + +// WithContext returns a copy of the Cmd with the provided context. +func (g *global) WithContext(ctx context.Context) server.Cmd { + return types.Ptr(child{ + Cmd: g, + ctx: ctx, + }) +}