From 909040f06a12b2e52d8131e362eaed819e3184b9 Mon Sep 17 00:00:00 2001 From: Yusuf Birader Date: Wed, 6 Sep 2023 18:32:08 +0100 Subject: [PATCH] refactor: inject context --- archiver.go | 6 +++--- archiver_test.go | 15 ++++++++------- cli.go | 3 ++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/archiver.go b/archiver.go index 38e0f09..a1648c8 100644 --- a/archiver.go +++ b/archiver.go @@ -72,9 +72,9 @@ func NewArchiver(archive *os.File) (*Archiver, error) { return a, nil } -func (a *Archiver) Archive(filePaths []string) error { - a.fileProcessPool.Start(context.Background()) - a.fileWriterPool.Start(context.Background()) +func (a *Archiver) Archive(ctx context.Context, filePaths []string) error { + a.fileProcessPool.Start(ctx) + a.fileWriterPool.Start(ctx) for _, path := range filePaths { info, err := os.Lstat(path) diff --git a/archiver_test.go b/archiver_test.go index 288992d..ba7bd89 100644 --- a/archiver_test.go +++ b/archiver_test.go @@ -3,6 +3,7 @@ package pzip import ( "archive/zip" "bytes" + "context" "encoding/binary" "fmt" "path/filepath" @@ -29,7 +30,7 @@ func TestArchive(t *testing.T) { archiver, err := NewArchiver(archive) assert.NoError(t, err) - archiver.Archive([]string{helloTxtFileFixture}) + archiver.Archive(context.Background(), []string{helloTxtFileFixture}) archiver.Close() archiveReader := testutils.GetArchiveReader(t, archive.Name()) @@ -52,7 +53,7 @@ func TestArchive(t *testing.T) { archiver, err := NewArchiver(archive) assert.NoError(t, err) - archiver.Archive([]string{helloTxtFileFixture}) + archiver.Archive(context.Background(), []string{helloTxtFileFixture}) archiver.Close() archiveReader := testutils.GetArchiveReader(t, archive.Name()) @@ -74,7 +75,7 @@ func TestArchive(t *testing.T) { archiver, err := NewArchiver(archive) assert.NoError(t, err) - archiver.Archive([]string{helloTxtFileFixture, helloMarkdownFileFixture}) + archiver.Archive(context.Background(), []string{helloTxtFileFixture, helloMarkdownFileFixture}) archiver.Close() archiveReader := testutils.GetArchiveReader(t, archive.Name()) @@ -89,7 +90,7 @@ func TestArchive(t *testing.T) { archiver, err := NewArchiver(archive) assert.NoError(t, err) - err = archiver.Archive([]string{helloDirectoryFixture}) + err = archiver.Archive(context.Background(), []string{helloDirectoryFixture}) assert.NoError(t, err) archiver.Close() @@ -105,9 +106,9 @@ func TestArchive(t *testing.T) { archiver, err := NewArchiver(archive) assert.NoError(t, err) - err = archiver.Archive([]string{helloTxtFileFixture}) + err = archiver.Archive(context.Background(), []string{helloTxtFileFixture}) assert.NoError(t, err) - err = archiver.Archive([]string{helloMarkdownFileFixture}) + err = archiver.Archive(context.Background(), []string{helloMarkdownFileFixture}) assert.NoError(t, err) archiver.Close() @@ -222,6 +223,6 @@ func BenchmarkArchive(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - archiver.Archive([]string{helloTxtFileFixture, helloMarkdownFileFixture}) + archiver.Archive(context.Background(), []string{helloTxtFileFixture, helloMarkdownFileFixture}) } } diff --git a/cli.go b/cli.go index c9dff78..8d08819 100644 --- a/cli.go +++ b/cli.go @@ -1,6 +1,7 @@ package pzip import ( + "context" "os" "github.com/pkg/errors" @@ -24,7 +25,7 @@ func (c *CLI) Archive() error { } defer archiver.Close() - err = archiver.Archive(c.Files) + err = archiver.Archive(context.Background(), c.Files) if err != nil { return errors.Wrapf(err, "ERROR: could not archive files") }