Skip to content

Commit f041a03

Browse files
committed
Split reading a VSIX from adding it
This allows future changes like reading a VSIX from stdin but it also removes the overlap and duplication in tests.
1 parent e413855 commit f041a03

File tree

6 files changed

+211
-219
lines changed

6 files changed

+211
-219
lines changed

api/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
type fakeStorage struct{}
2828

29-
func (s *fakeStorage) AddExtension(ctx context.Context, source string) (*storage.Extension, error) {
29+
func (s *fakeStorage) AddExtension(ctx context.Context, vsix []byte) (*storage.Extension, error) {
3030
return nil, errors.New("not implemented")
3131
}
3232

cli/add.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,15 @@ func add() *cobra.Command {
4646
return err
4747
}
4848

49+
// Read in the extension. In the future we might support stdin as well.
50+
vsix, err := storage.ReadVSIX(ctx, args[0])
51+
if err != nil {
52+
return err
53+
}
54+
4955
// Always local storage for now.
5056
store := storage.NewLocalStorage(ctx, extdir, logger)
51-
ext, err := store.AddExtension(ctx, args[0])
57+
ext, err := store.AddExtension(ctx, vsix)
5258
if err != nil {
5359
return err
5460
}

database/database_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
type memoryStorage struct{}
2222

23-
func (s *memoryStorage) AddExtension(ctx context.Context, source string) (*storage.Extension, error) {
23+
func (s *memoryStorage) AddExtension(ctx context.Context, vsix []byte) (*storage.Extension, error) {
2424
return nil, errors.New("not implemented")
2525
}
2626

storage/local.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,8 @@ func NewLocalStorage(_ context.Context, extdir string, logger slog.Logger) *Loca
3333
Logger: logger,
3434
}
3535
}
36-
37-
func (s *Local) AddExtension(ctx context.Context, source string) (*Extension, error) {
38-
vsixBytes, err := readVSIX(ctx, source)
39-
if err != nil {
40-
return nil, err
41-
}
42-
43-
mr, err := GetZipFileReader(vsixBytes, "extension.vsixmanifest")
36+
func (s *Local) AddExtension(ctx context.Context, vsix []byte) (*Extension, error) {
37+
mr, err := GetZipFileReader(vsix, "extension.vsixmanifest")
4438
if err != nil {
4539
return nil, err
4640
}
@@ -59,7 +53,7 @@ func (s *Local) AddExtension(ctx context.Context, source string) (*Extension, er
5953
// Extract the zip to the correct path.
6054
identity := manifest.Metadata.Identity
6155
dir := filepath.Join(s.ExtDir, identity.Publisher, identity.ID, identity.Version)
62-
err = ExtractZip(vsixBytes, func(name string) (io.WriteCloser, error) {
56+
err = ExtractZip(vsix, func(name string) (io.WriteCloser, error) {
6357
path := filepath.Join(dir, name)
6458
err := os.MkdirAll(filepath.Dir(path), 0o755)
6559
if err != nil {
@@ -79,7 +73,7 @@ func (s *Local) AddExtension(ctx context.Context, source string) (*Extension, er
7973
return nil, err
8074
}
8175
defer dst.Close()
82-
_, err = io.Copy(dst, bytes.NewReader(vsixBytes))
76+
_, err = io.Copy(dst, bytes.NewReader(vsix))
8377
if err != nil {
8478
return nil, err
8579
}

storage/storage.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ type Extension struct {
9898

9999
// TODO: Add Artifactory implementation of Storage.
100100
type Storage interface {
101-
// AddExtension adds the extension found at the specified source by copying it
102-
// into the extension storage directory and returns details about the added
103-
// extension. The source may be an URI or a local file path.
104-
AddExtension(ctx context.Context, source string) (*Extension, error)
101+
// AddExtension adds the provided VSIX by copying it into the extension
102+
// storage directory and returns details about the added extension. The
103+
// source may be an URI or a local file path.
104+
AddExtension(ctx context.Context, vsix []byte) (*Extension, error)
105105
// RemoveExtension removes the extension by id (publisher, name, and version)
106106
// or all versions if all is true (in which case the id should omit the
107107
// version) and returns the IDs removed.
@@ -160,9 +160,9 @@ func validateManifest(manifest *VSIXManifest) error {
160160
return nil
161161
}
162162

163-
// readVSIX reads the bytes of a VSIX from the specified source. The source
163+
// ReadVSIX reads the bytes of a VSIX from the specified source. The source
164164
// might be a URI or a local file path.
165-
func readVSIX(ctx context.Context, source string) ([]byte, error) {
165+
func ReadVSIX(ctx context.Context, source string) ([]byte, error) {
166166
if !strings.HasPrefix(source, "http://") && !strings.HasPrefix(source, "https://") {
167167
// Assume it is a local file path.
168168
return os.ReadFile(source)

0 commit comments

Comments
 (0)