add optional function WithFileWriter

This commit is contained in:
onyx-and-iris 2025-05-01 08:53:18 +01:00
parent 891057ff86
commit 7df58273f5
3 changed files with 13 additions and 4 deletions

View File

@ -31,12 +31,12 @@ func (m mockFileWriter) Write(content []byte) (int, error) {
} }
func TestCreateSuccess(t *testing.T) { func TestCreateSuccess(t *testing.T) {
var templateBuffer bytes.Buffer
client := gignore.New( client := gignore.New(
gignore.WithTemplateDirectory(gignore.DefaultTemplateDirectory), gignore.WithTemplateDirectory(gignore.DefaultTemplateDirectory),
gignore.WithFileWriter(mockFileWriter{out: &templateBuffer}),
) )
ctx := context.WithValue(context.Background(), clientKey, client) ctx := context.WithValue(context.Background(), clientKey, client)
var templateBuffer bytes.Buffer
client.Writer = mockFileWriter{out: &templateBuffer}
var outBuffer bytes.Buffer var outBuffer bytes.Buffer
err := createTemplate(ctx, &outBuffer, "go") err := createTemplate(ctx, &outBuffer, "go")

View File

@ -16,7 +16,7 @@ const DefaultTemplateDirectory = "gitignoreio"
// Client is a client for managing .gitignore templates. // Client is a client for managing .gitignore templates.
type Client struct { type Client struct {
registry *registry.TemplateRegistry registry *registry.TemplateRegistry
Writer io.Writer writer io.Writer
} }
// New creates a new Client with the provided options. // New creates a new Client with the provided options.
@ -85,7 +85,7 @@ func (c *Client) Create(template string) error {
return err return err
} }
_, err = c.Writer.Write(content) _, err = c.writer.Write(content)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,5 +1,7 @@
package gignore package gignore
import "io"
// Option is a function that configures a GignoreClient. // Option is a function that configures a GignoreClient.
type Option func(*Client) type Option func(*Client)
@ -9,3 +11,10 @@ func WithTemplateDirectory(directory string) Option {
c.registry.Directory = directory 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
}
}