diff --git a/cmd/gignore/create_test.go b/cmd/gignore/create_test.go index 5ba7dd9..202e746 100644 --- a/cmd/gignore/create_test.go +++ b/cmd/gignore/create_test.go @@ -31,12 +31,12 @@ func (m mockFileWriter) Write(content []byte) (int, error) { } func TestCreateSuccess(t *testing.T) { + var templateBuffer bytes.Buffer client := gignore.New( gignore.WithTemplateDirectory(gignore.DefaultTemplateDirectory), + gignore.WithFileWriter(mockFileWriter{out: &templateBuffer}), ) ctx := context.WithValue(context.Background(), clientKey, client) - var templateBuffer bytes.Buffer - client.Writer = mockFileWriter{out: &templateBuffer} var outBuffer bytes.Buffer err := createTemplate(ctx, &outBuffer, "go") diff --git a/gignore.go b/gignore.go index 3916f5e..728b84c 100644 --- a/gignore.go +++ b/gignore.go @@ -16,7 +16,7 @@ const DefaultTemplateDirectory = "gitignoreio" // Client is a client for managing .gitignore templates. type Client struct { registry *registry.TemplateRegistry - Writer io.Writer + writer io.Writer } // New creates a new Client with the provided options. @@ -85,7 +85,7 @@ func (c *Client) Create(template string) error { return err } - _, err = c.Writer.Write(content) + _, err = c.writer.Write(content) if err != nil { return err } diff --git a/option.go b/option.go index 9f11d1c..d07b086 100644 --- a/option.go +++ b/option.go @@ -1,5 +1,7 @@ package gignore +import "io" + // Option is a function that configures a GignoreClient. type Option func(*Client) @@ -9,3 +11,10 @@ func WithTemplateDirectory(directory string) Option { c.registry.Directory = directory } } + +// WithFileWriter sets the file writer for the GignoreClient. +func WithFileWriter(writer io.Writer) Option { + return func(c *Client) { + c.writer = writer + } +}