Skip to content

Commit

Permalink
Handle possibility of nil context, clients, or headers (#524)
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Jan 17, 2024
1 parent af946df commit dd2398b
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 43 deletions.
24 changes: 18 additions & 6 deletions awsimdsfs/awsimdsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,34 @@ func (f awsimdsFS) URL() string {
return f.base.String()
}

func (f awsimdsFS) WithContext(ctx context.Context) fs.FS {
fsys := f
func (f *awsimdsFS) WithContext(ctx context.Context) fs.FS {
if ctx == nil {
return f
}

fsys := *f
fsys.ctx = ctx

return &fsys
}

func (f awsimdsFS) WithHTTPClient(client *http.Client) fs.FS {
fsys := f
func (f *awsimdsFS) WithHTTPClient(client *http.Client) fs.FS {
if client == nil {
return f
}

fsys := *f
fsys.httpclient = client

return &fsys
}

func (f awsimdsFS) WithIMDSClient(imdsclient IMDSClient) fs.FS {
fsys := f
func (f *awsimdsFS) WithIMDSClient(imdsclient IMDSClient) fs.FS {
if imdsclient == nil {
return f
}

fsys := *f
fsys.imdsclient = imdsclient

return &fsys
Expand Down
24 changes: 18 additions & 6 deletions awssmfs/awssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,34 @@ func (f awssmFS) URL() string {
return f.base.String()
}

func (f awssmFS) WithContext(ctx context.Context) fs.FS {
fsys := f
func (f *awssmFS) WithContext(ctx context.Context) fs.FS {
if ctx == nil {
return f
}

fsys := *f
fsys.ctx = ctx

return &fsys
}

func (f awssmFS) WithHTTPClient(client *http.Client) fs.FS {
fsys := f
func (f *awssmFS) WithHTTPClient(client *http.Client) fs.FS {
if client == nil {
return f
}

fsys := *f
fsys.httpclient = client

return &fsys
}

func (f awssmFS) WithSMClient(smclient SecretsManagerClient) fs.FS {
fsys := f
func (f *awssmFS) WithSMClient(smclient SecretsManagerClient) fs.FS {
if smclient == nil {
return f
}

fsys := *f
fsys.smclient = smclient

return &fsys
Expand Down
24 changes: 18 additions & 6 deletions awssmpfs/awssmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,34 @@ func (f awssmpFS) URL() string {
return f.base.String()
}

func (f awssmpFS) WithContext(ctx context.Context) fs.FS {
fsys := f
func (f *awssmpFS) WithContext(ctx context.Context) fs.FS {
if ctx == nil {
return f
}

fsys := *f
fsys.ctx = ctx

return &fsys
}

func (f awssmpFS) WithHTTPClient(client *http.Client) fs.FS {
fsys := f
func (f *awssmpFS) WithHTTPClient(client *http.Client) fs.FS {
if client == nil {
return f
}

fsys := *f
fsys.httpclient = client

return &fsys
}

func (f awssmpFS) WithClient(ssmclient SSMClient) fs.FS {
fsys := f
func (f *awssmpFS) WithClient(ssmclient SSMClient) fs.FS {
if ssmclient == nil {
return f
}

fsys := *f
fsys.ssmclient = ssmclient

return &fsys
Expand Down
16 changes: 12 additions & 4 deletions blobfs/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,23 @@ func (f blobFS) URL() string {
return f.base.String()
}

func (f blobFS) WithContext(ctx context.Context) fs.FS {
fsys := f
func (f *blobFS) WithContext(ctx context.Context) fs.FS {
if ctx == nil {
return f
}

fsys := *f
fsys.ctx = ctx

return &fsys
}

func (f blobFS) WithHTTPClient(client *http.Client) fs.FS {
fsys := f
func (f *blobFS) WithHTTPClient(client *http.Client) fs.FS {
if client == nil {
return f
}

fsys := *f
fsys.hclient = client

return &fsys
Expand Down
24 changes: 18 additions & 6 deletions consulfs/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,23 @@ func (f consulFS) URL() string {
return f.base.String()
}

func (f consulFS) WithContext(ctx context.Context) fs.FS {
fsys := f
func (f *consulFS) WithContext(ctx context.Context) fs.FS {
if ctx == nil {
return f
}

fsys := *f
fsys.ctx = ctx

return &fsys
}

func (f consulFS) WithHeader(header http.Header) fs.FS {
fsys := f
func (f *consulFS) WithHeader(header http.Header) fs.FS {
if header == nil {
return f
}

fsys := *f
fsys.header = header

if fsys.client != nil {
Expand All @@ -92,8 +100,12 @@ func (f consulFS) WithHeader(header http.Header) fs.FS {
return &fsys
}

func (f consulFS) WithConfig(config *api.Config) fs.FS {
fsys := f
func (f *consulFS) WithConfig(config *api.Config) fs.FS {
if config == nil {
return f
}

fsys := *f
fsys.client = nil
fsys.config = config

Expand Down
16 changes: 12 additions & 4 deletions gitfs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,23 @@ func (f gitFS) URL() string {
return f.repo.String()
}

func (f gitFS) WithAuthenticator(auth Authenticator) fs.FS {
fsys := f
func (f *gitFS) WithAuthenticator(auth Authenticator) fs.FS {
if auth == nil {
return f
}

fsys := *f
fsys.auth = auth

return &fsys
}

func (f gitFS) WithContext(ctx context.Context) fs.FS {
fsys := f
func (f *gitFS) WithContext(ctx context.Context) fs.FS {
if ctx == nil {
return f
}

fsys := *f
fsys.ctx = ctx

return &fsys
Expand Down
26 changes: 19 additions & 7 deletions httpfs/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,27 @@ func (f httpFS) URL() string {
return f.base.String()
}

func (f httpFS) WithContext(ctx context.Context) fs.FS {
fsys := f
func (f *httpFS) WithContext(ctx context.Context) fs.FS {
if ctx == nil {
return f
}

fsys := *f
fsys.ctx = ctx

return &fsys
}

func (f httpFS) WithHeader(headers http.Header) fs.FS {
fsys := f
func (f *httpFS) WithHeader(headers http.Header) fs.FS {
if headers == nil {
return f
}

fsys := *f
if len(fsys.headers) == 0 {
fsys.headers = headers
} else {
for k, vs := range fsys.headers {
for k, vs := range headers {
for _, v := range vs {
fsys.headers.Add(k, v)
}
Expand All @@ -76,8 +84,12 @@ func (f httpFS) WithHeader(headers http.Header) fs.FS {
return &fsys
}

func (f httpFS) WithHTTPClient(client *http.Client) fs.FS {
fsys := f
func (f *httpFS) WithHTTPClient(client *http.Client) fs.FS {
if client == nil {
return f
}

fsys := *f
fsys.client = client

return &fsys
Expand Down
16 changes: 12 additions & 4 deletions vaultfs/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,23 @@ func (f vaultFS) URL() string {
return f.base.String()
}

func (f vaultFS) WithContext(ctx context.Context) fs.FS {
fsys := f
func (f *vaultFS) WithContext(ctx context.Context) fs.FS {
if ctx == nil {
return f
}

fsys := *f
fsys.ctx = ctx

return &fsys
}

func (f vaultFS) WithHeader(headers http.Header) fs.FS {
fsys := f
func (f *vaultFS) WithHeader(headers http.Header) fs.FS {
if headers == nil {
return f
}

fsys := *f

for k, vs := range headers {
for _, v := range vs {
Expand Down

0 comments on commit dd2398b

Please sign in to comment.