Skip to content

Commit f623543

Browse files
committed
Refactor ExtractZip
It appears to be more flexible to call the function with a reader rather than expect the function to return a writer.
1 parent dfb4085 commit f623543

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

storage/local.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ func (s *Local) AddExtension(ctx context.Context, manifest *VSIXManifest, vsix [
3333
// Extract the zip to the correct path.
3434
identity := manifest.Metadata.Identity
3535
dir := filepath.Join(s.extdir, identity.Publisher, identity.ID, identity.Version)
36-
err := ExtractZip(vsix, func(name string) (io.WriteCloser, error) {
36+
err := ExtractZip(vsix, func(name string, r io.Reader) error {
3737
path := filepath.Join(dir, name)
3838
err := os.MkdirAll(filepath.Dir(path), 0o755)
3939
if err != nil {
40-
return nil, err
40+
return err
4141
}
42-
return os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
42+
w, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
43+
if err != nil {
44+
return err
45+
}
46+
defer w.Close()
47+
_, err = io.Copy(w, r)
48+
return err
4349
})
4450
if err != nil {
4551
return "", err

storage/zip.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,17 @@ func GetZipFileReader(rawZip []byte, filename string) (io.ReadCloser, error) {
4848
return reader, nil
4949
}
5050

51-
// ExtractZip extracts a zip's files to the specified directory. The writer is
52-
// expected to create any necessary directories and return a writer for writing
53-
// a file.
54-
func ExtractZip(rawZip []byte, writer func(name string) (io.WriteCloser, error)) error {
51+
// ExtractZip applies a function with a reader for every file in the zip. If
52+
// the function returns an error the walk is aborted.
53+
func ExtractZip(rawZip []byte, fn func(name string, reader io.Reader) error) error {
5554
_, err := WalkZip(rawZip, func(zf *zip.File) (stop bool, err error) {
5655
if !zf.FileInfo().IsDir() {
57-
dst, err := writer(zf.Name)
58-
if err != nil {
59-
return false, err
60-
}
61-
defer dst.Close()
62-
src, err := zf.Open()
63-
if err != nil {
64-
return false, err
65-
}
66-
defer src.Close()
67-
_, err = io.Copy(dst, src)
56+
zr, err := zf.Open()
6857
if err != nil {
6958
return false, err
7059
}
60+
defer zr.Close()
61+
return false, fn(zf.Name, zr)
7162
}
7263
return false, nil
7364
})

storage/zip_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var files = []struct {
1616
{"alpha.txt", "Alpha content."},
1717
{"beta.txt", "Beta content."},
1818
{"charlie.txt", "Charlie content."},
19+
{"delta/delta.txt", "Delta content."},
1920
}
2021

2122
func createZip() ([]byte, error) {
@@ -55,32 +56,26 @@ func TestGetZipFileReader(t *testing.T) {
5556
require.Error(t, err)
5657
}
5758

58-
type nopCloser struct {
59-
io.Writer
60-
}
61-
62-
func (nopCloser) Close() error { return nil }
63-
6459
func TestExtract(t *testing.T) {
6560
t.Parallel()
6661

6762
buffer, err := createZip()
6863
require.NoError(t, err)
6964

7065
t.Run("Error", func(t *testing.T) {
71-
err := ExtractZip(buffer, func(name string) (io.WriteCloser, error) {
72-
return nil, errors.New("error")
66+
err := ExtractZip(buffer, func(name string, reader io.Reader) error {
67+
return errors.New("error")
7368
})
7469
require.Error(t, err)
7570
})
7671

7772
t.Run("OK", func(t *testing.T) {
7873
called := []string{}
79-
err := ExtractZip(buffer, func(name string) (io.WriteCloser, error) {
74+
err := ExtractZip(buffer, func(name string, reader io.Reader) error {
8075
called = append(called, name)
81-
return nopCloser{io.Discard}, nil
76+
return nil
8277
})
8378
require.NoError(t, err)
84-
require.Equal(t, []string{"alpha.txt", "beta.txt", "charlie.txt"}, called)
79+
require.Equal(t, []string{"alpha.txt", "beta.txt", "charlie.txt", "delta/delta.txt"}, called)
8580
})
8681
}

0 commit comments

Comments
 (0)