Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Add the 'Cause' family of functions to the context wrapper library (#1725)
Miccahc77c1172c8b0
diff --git a/pkg/context/context.go b/pkg/context/context.goindex 1819f6163..060a3ed4c 100644--- a/pkg/context/context.go+++ b/pkg/context/context.go@@ -26,9 +26,10 @@ type Context interface {Logger() logr.Logger}-// CancelFunc is a type alias to context.CancelFunc to allow use as if they are-// the same types.+// CancelFunc and CancelCauseFunc are type aliases to allow use as if they are+// the same types as the standard library variants.type CancelFunc = context.CancelFunc+type CancelCauseFunc = context.CancelCauseFunc// logCtx implements Context.type logCtx struct {@@ -68,6 +69,16 @@ func WithCancel(parent Context) (Context, context.CancelFunc) {return lCtx, cancel}+// WithCancelCause returns context.WithCancelCause with the log object propagated.+func WithCancelCause(parent Context) (Context, context.CancelCauseFunc) {+ ctx, cancel := context.WithCancelCause(parent)+ lCtx := logCtx{+ log: parent.Logger(),+ Context: ctx,+ }+ return lCtx, cancel+}+// WithDeadline returns context.WithDeadline with the log object propagated and// the deadline added to the structured log values.func WithDeadline(parent Context, d time.Time) (Context, context.CancelFunc) {@@ -79,6 +90,17 @@ func WithDeadline(parent Context, d time.Time) (Context, context.CancelFunc) {return lCtx, cancel}+// WithDeadlineCause returns context.WithDeadlineCause with the log object+// propagated and the deadline added to the structured log values.+func WithDeadlineCause(parent Context, d time.Time, cause error) (Context, context.CancelFunc) {+ ctx, cancel := context.WithDeadlineCause(parent, d, cause)+ lCtx := logCtx{+ log: parent.Logger().WithValues("deadline", d),+ Context: ctx,+ }+ return lCtx, cancel+}+// WithTimeout returns context.WithTimeout with the log object propagated and// the timeout added to the structured log values.func WithTimeout(parent Context, timeout time.Duration) (Context, context.CancelFunc) {@@ -90,6 +112,22 @@ func WithTimeout(parent Context, timeout time.Duration) (Context, context.Cancelreturn lCtx, cancel}+// WithTimeoutCause returns context.WithTimeoutCause with the log object+// propagated and the timeout added to the structured log values.+func WithTimeoutCause(parent Context, timeout time.Duration, cause error) (Context, context.CancelFunc) {+ ctx, cancel := context.WithTimeoutCause(parent, timeout, cause)+ lCtx := logCtx{+ log: parent.Logger().WithValues("timeout", timeout),+ Context: ctx,+ }+ return lCtx, cancel+}++// Cause returns the context.Cause of the context.+func Cause(ctx context.Context) error {+ return context.Cause(ctx)+}+// WithValue returns context.WithValue with the log object propagated and// the value added to the structured log values (if the key is a string).func WithValue(parent Context, key, val any) Context {diff --git a/pkg/context/context_test.go b/pkg/context/context_test.goindex e6e8d9d00..d735727e5 100644--- a/pkg/context/context_test.go+++ b/pkg/context/context_test.go@@ -3,6 +3,7 @@ package contextimport ("bytes""context"+ "fmt""strings""testing""time"@@ -177,3 +178,10 @@ func TestRace(t *testing.T) {cancel()_ = ctx.Err()}++func TestCause(t *testing.T) {+ ctx, cancel := WithCancelCause(Background())+ err := fmt.Errorf("oh no")+ cancel(err)+ assert.Equal(t, err, Cause(ctx))+}