Skip to content

Commit

Permalink
add ability to explicitly disable logger
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Callaway <bcallaway@google.com>
  • Loading branch information
bobcallaway committed Oct 5, 2022
1 parent 3de0e8a commit cba5641
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
9 changes: 9 additions & 0 deletions pkg/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package client

import (
"log"
"net/http"
"os"

"github.com/hashicorp/go-retryablehttp"
)
Expand All @@ -34,10 +36,17 @@ const (
DefaultRetryCount = 3
)

var DefaultLogger retryablehttp.Logger

func init() {
DefaultLogger = log.New(os.Stderr, "", log.LstdFlags)
}

func makeOptions(opts ...Option) *options {
o := &options{
UserAgent: "",
RetryCount: DefaultRetryCount,
Logger: DefaultLogger,
}

for _, opt := range opts {
Expand Down
16 changes: 12 additions & 4 deletions pkg/client/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,42 @@
package client

import (
"log"
"net/http"
"os"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestMakeOptions(t *testing.T) {
customLogger := log.New(os.Stdout, "", log.LstdFlags)

tests := []struct {
desc string

opts []Option
want *options
}{{
desc: "no opts",
want: &options{RetryCount: DefaultRetryCount},
want: &options{RetryCount: DefaultRetryCount, Logger: DefaultLogger},
}, {
desc: "WithUserAgent",
opts: []Option{WithUserAgent("test user agent")},
want: &options{UserAgent: "test user agent", RetryCount: DefaultRetryCount},
want: &options{UserAgent: "test user agent", RetryCount: DefaultRetryCount, Logger: DefaultLogger},
}, {
desc: "WithRetryCount",
opts: []Option{WithRetryCount(2)},
want: &options{UserAgent: "", RetryCount: 2},
want: &options{UserAgent: "", RetryCount: 2, Logger: DefaultLogger},
}, {
desc: "WithLogger",
opts: []Option{WithLogger(customLogger)},
want: &options{UserAgent: "", RetryCount: DefaultRetryCount, Logger: customLogger},
}}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
got := makeOptions(tc.opts...)
if d := cmp.Diff(tc.want, got); d != "" {
if d := cmp.Diff(tc.want, got, cmp.Comparer(func(a, b *log.Logger) bool { return a == b })); d != "" {
t.Errorf("makeOptions(%v) returned unexpected result (-want +got): %s", tc.desc, d)
}
})
Expand Down
4 changes: 1 addition & 3 deletions pkg/client/rekor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func GetRekorClient(rekorServerURL string, opts ...Option) (*client.Rekor, error

retryableClient := retryablehttp.NewClient()
retryableClient.RetryMax = int(o.RetryCount)
if o.Logger != nil {
retryableClient.Logger = o.Logger
}
retryableClient.Logger = o.Logger

rt := httptransport.NewWithClient(url.Host, client.DefaultBasePath, []string{url.Scheme}, retryableClient.StandardClient())
rt.Consumers["application/json"] = runtime.JSONConsumer()
Expand Down

0 comments on commit cba5641

Please sign in to comment.