Skip to content

Commit

Permalink
feat: add WithUserAgent opt (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg authored Mar 31, 2022
1 parent 46c62b8 commit bd89dc5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,18 @@ type Dialer struct {
func NewDialer(ctx context.Context, opts ...Option) (*Dialer, error) {
cfg := &dialerConfig{
refreshTimeout: 30 * time.Second,
sqladminOpts: []option.ClientOption{option.WithUserAgent(userAgent)},
dialFunc: proxy.Dial,
useragents: []string{userAgent},
}
for _, opt := range opts {
opt(cfg)
if cfg.err != nil {
return nil, cfg.err
}
}
// Add this to the end to make sure it's not overridden
cfg.sqladminOpts = append(cfg.sqladminOpts, option.WithUserAgent(strings.Join(cfg.useragents, " ")))

// If callers have not provided a token source, either explicitly with
// WithTokenSource or implicitly with WithCredentialsJSON etc, then use the
// default token source.
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type dialerConfig struct {
refreshTimeout time.Duration
useIAMAuthN bool
tokenSource oauth2.TokenSource
useragents []string
// err tracks any dialer options that may have failed.
err error
}
Expand Down Expand Up @@ -83,6 +84,13 @@ func WithCredentialsJSON(b []byte) Option {
}
}

// WithUserAgent returns an Option that sets the User-Agent.
func WithUserAgent(ua string) Option {
return func(d *dialerConfig) {
d.useragents = append(d.useragents, ua)
}
}

// WithDefaultDialOptions returns an Option that specifies the default
// DialOptions used.
func WithDefaultDialOptions(opts ...DialOption) Option {
Expand Down

0 comments on commit bd89dc5

Please sign in to comment.