Skip to content

Commit

Permalink
Use functional options instead of builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Feb 14, 2022
1 parent 022db7c commit 7ea3aa6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
3 changes: 1 addition & 2 deletions ecr-login/cli/docker-credential-ecr-login/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"

ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/config"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/version"
"github.com/docker/docker-credential-helpers/credentials"
Expand All @@ -42,5 +41,5 @@ func main() {
}

config.SetupLogger()
credentials.Serve(ecr.NewECRHelper(api.DefaultClientFactory{}))
credentials.Serve(ecr.NewECRHelper())
}
28 changes: 21 additions & 7 deletions ecr-login/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,30 @@ type ECRHelper struct {
logger *logrus.Logger
}

func NewECRHelper(cf api.ClientFactory) ECRHelper {
return ECRHelper{
clientFactory: cf,
logger: logrus.StandardLogger(),
type Option func(*ECRHelper)

func WithClientFactory(clientFactory api.ClientFactory) Option {
return func(e *ECRHelper) {
e.clientFactory = clientFactory
}
}

func WithLogOutput(w io.Writer) Option {
return func(e *ECRHelper) {
e.logger.Out = w
}
}

func (self ECRHelper) WithLogOutput(w io.Writer) ECRHelper {
self.logger.Out = w
return self
func NewECRHelper(opts ...Option) *ECRHelper {
e := &ECRHelper{
clientFactory: api.DefaultClientFactory{},
logger: logrus.StandardLogger(),
}
for _, o := range opts {
o(e)
}

return e
}

// ensure ECRHelper adheres to the credentials.Helper interface
Expand Down
10 changes: 5 additions & 5 deletions ecr-login/ecr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestGetSuccess(t *testing.T) {
factory := &mock_api.MockClientFactory{}
client := &mock_api.MockClient{}

helper := NewECRHelper(factory)
helper := NewECRHelper(WithClientFactory(factory))

factory.NewClientFromRegionFn = func(_ string) ecr.Client { return client }
client.GetCredentialsFn = func(serverURL string) (*ecr.Auth, error) {
Expand All @@ -60,7 +60,7 @@ func TestGetError(t *testing.T) {
factory := &mock_api.MockClientFactory{}
client := &mock_api.MockClient{}

helper := NewECRHelper(factory)
helper := NewECRHelper(WithClientFactory(factory))

factory.NewClientFromRegionFn = func(_ string) ecr.Client { return client }
client.GetCredentialsFn = func(serverURL string) (*ecr.Auth, error) {
Expand All @@ -74,7 +74,7 @@ func TestGetError(t *testing.T) {
}

func TestGetNoMatch(t *testing.T) {
helper := NewECRHelper(nil)
helper := NewECRHelper(WithClientFactory(nil))

username, password, err := helper.Get("not-ecr-server-url")
assert.True(t, credentials.IsErrCredentialsNotFound(err))
Expand All @@ -86,7 +86,7 @@ func TestListSuccess(t *testing.T) {
factory := &mock_api.MockClientFactory{}
client := &mock_api.MockClient{}

helper := NewECRHelper(factory)
helper := NewECRHelper(WithClientFactory(factory))

factory.NewClientWithDefaultsFn = func() ecr.Client { return client }
client.ListCredentialsFn = func() ([]*ecr.Auth, error) {
Expand All @@ -107,7 +107,7 @@ func TestListFailure(t *testing.T) {
factory := &mock_api.MockClientFactory{}
client := &mock_api.MockClient{}

helper := NewECRHelper(factory)
helper := NewECRHelper(WithClientFactory(factory))

factory.NewClientWithDefaultsFn = func() ecr.Client { return client }
client.ListCredentialsFn = func() ([]*ecr.Auth, error) {
Expand Down

0 comments on commit 7ea3aa6

Please sign in to comment.