Repository navigation
feat(client): add WithHTTPClient option - #762
Conversation
Lets callers pass an existing client.Client to the pass commands instead of having one built from the socket path and timeout options.
Lets callers pass their own *http.Client to client.New. Drops the WithClient option from the pass commands.
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟡 NEEDS ATTENTION
|
|
||
| func WithClient(c client.Client) ClientOption { | ||
| return func(o *clientOpts) error { | ||
| if c == nil { |
There was a problem hiding this comment.
[medium] WithClient nil check misses typed-nil interface values
client.Client is an interface. The guard if c == nil only catches a bare (untyped) nil — when both the type descriptor and the value pointer are nil. If a caller passes a typed nil, e.g.:
var c *myConcreteClient = nil
WithClient(c) // passes the nil check, opts.client is now non-nil…the interface value is not nil (its type descriptor is populated), so the check passes silently. newClient then returns this non-nil interface wrapping a nil pointer, and any method call on the returned client will panic at runtime.
Fix: use a reflection-based check (and add "reflect" to imports):
if c == nil || reflect.ValueOf(c).IsNil() {
return errors.New("no client provided")
}Alternatively, document clearly that WithClient does not support typed-nil values, so callers know they must guard before calling.
| Confidence | Score |
|---|---|
| 🟢 strong | 97/100 |
Adds
client.WithHTTPClient(*http.Client)so callers can giveclient.Newtheir own HTTP client instead of the one it builds by default.WithSocketPathorWithDialContextreturnscannot set http client and socket path or dial.WithTimeoutandWithResponseTimeoutdo not apply to it.WithHTTPClient(nil)returnsno http client provided.ConnectError/ErrSecretsEngineNotRunning.Tests in
TestWithHTTPClientcover requests through the client, the error cases, and an unreachable engine.