Skip to content

Commit 61ac7a4

Browse files
refactor: rename lowlevel package to uploadrevision
1 parent 67d94e8 commit 61ac7a4

File tree

14 files changed

+179
-158
lines changed

14 files changed

+179
-158
lines changed

internal/fileupload/client.go

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@ import (
1313
"github.com/puzpuzpuz/xsync"
1414

1515
listsources "github.com/snyk/cli-extension-os-flows/internal/files"
16-
"github.com/snyk/cli-extension-os-flows/internal/fileupload/lowlevel"
16+
"github.com/snyk/cli-extension-os-flows/internal/fileupload/uploadrevision"
1717
)
1818

1919
// Config contains configuration for the file upload client.
2020
type Config struct {
2121
BaseURL string
22-
OrgID uuid.UUID
22+
OrgID OrgID
2323
}
2424

2525
// Client provides high-level file upload functionality.
2626
type Client struct {
2727
httpClient *http.Client
28-
lowlevel lowlevel.SealableClient
28+
lowlevel uploadrevision.SealableClient
2929
cfg Config
3030
filters Filters
3131
}
3232

3333
// NewClient creates a new high-level file upload client.
34-
// For production use, consumers only need to provide Config.
35-
// For testing, use WithLowLevelClient option to inject a mock.
3634
func NewClient(httpClient *http.Client, cfg Config, opts ...Option) *Client {
3735
client := &Client{
3836
httpClient: httpClient,
@@ -48,9 +46,9 @@ func NewClient(httpClient *http.Client, cfg Config, opts ...Option) *Client {
4846
}
4947

5048
if client.lowlevel == nil {
51-
client.lowlevel = lowlevel.NewClient(lowlevel.Config{
49+
client.lowlevel = uploadrevision.NewClient(uploadrevision.Config{
5250
BaseURL: cfg.BaseURL,
53-
}, lowlevel.WithHTTPClient(httpClient))
51+
}, uploadrevision.WithHTTPClient(httpClient))
5452
}
5553

5654
return client
@@ -71,9 +69,10 @@ func (c *Client) getDeeproxyFilters(ctx context.Context) (FiltersResponse, error
7169
if err != nil {
7270
return filtersResp, fmt.Errorf("error making deeproxy filters request: %w", err)
7371
}
72+
defer resp.Body.Close()
7473

7574
if resp.StatusCode < 200 || resp.StatusCode > 299 {
76-
return filtersResp, fmt.Errorf("unexpected response code: %s (%s)", resp.Status, resp.Body)
75+
return filtersResp, fmt.Errorf("unexpected response code: %s", resp.Status)
7776
}
7877

7978
if err := json.NewDecoder(resp.Body).Decode(&filtersResp); err != nil {
@@ -108,11 +107,7 @@ func (c *Client) loadFilters(ctx context.Context) error {
108107

109108
// createFileFilter creates a filter function based on the current filtering configuration.
110109
// Returns nil if filtering should be skipped, otherwise returns a function that evaluates files.
111-
func (c *Client) createFileFilter(ctx context.Context, opts UploadOptions) (func(string) bool, error) {
112-
if opts.SkipFiltering {
113-
return nil, nil
114-
}
115-
110+
func (c *Client) createFileFilter(ctx context.Context) (func(string) bool, error) {
116111
if err := c.loadFilters(ctx); err != nil {
117112
return nil, fmt.Errorf("failed to load deeproxy filters: %w", err)
118113
}
@@ -127,7 +122,7 @@ func (c *Client) createFileFilter(ctx context.Context, opts UploadOptions) (func
127122
}
128123

129124
func (c *Client) uploadPaths(ctx context.Context, revID RevisionID, rootPath string, paths []string) error {
130-
files := make([]lowlevel.UploadFile, 0, c.lowlevel.GetLimits().FileCountLimit)
125+
files := make([]uploadrevision.UploadFile, 0, c.lowlevel.GetLimits().FileCountLimit)
131126
defer func() {
132127
for _, file := range files {
133128
file.File.Close()
@@ -137,40 +132,40 @@ func (c *Client) uploadPaths(ctx context.Context, revID RevisionID, rootPath str
137132
for _, pth := range paths {
138133
relPth, err := filepath.Rel(rootPath, pth)
139134
if err != nil {
140-
return err
135+
return fmt.Errorf("failed to get relative path for %s: %w", pth, err)
141136
}
142137

143138
f, err := os.Open(pth)
144139
if err != nil {
145-
return err
140+
return fmt.Errorf("failed to open file %s: %w", pth, err)
146141
}
147142

148-
files = append(files, lowlevel.UploadFile{
143+
files = append(files, uploadrevision.UploadFile{
149144
Path: relPth,
150145
File: f,
151146
})
152147
}
153148

154149
err := c.lowlevel.UploadFiles(ctx, c.cfg.OrgID, revID, files)
155150
if err != nil {
156-
return err
151+
return fmt.Errorf("failed to upload files: %w", err)
157152
}
158153

159154
return nil
160155
}
161156

162157
// addPathsToRevision adds multiple file paths to an existing revision.
163158
func (c *Client) addPathsToRevision(ctx context.Context, revisionID RevisionID, rootPath string, pathsChan <-chan string, opts UploadOptions) error {
164-
filter, err := c.createFileFilter(ctx, opts)
165-
if err != nil {
166-
return err
167-
}
168-
169159
var chunks <-chan []string
170-
if filter != nil {
171-
chunks = chunkChanFiltered(pathsChan, c.lowlevel.GetLimits().FileCountLimit, filter)
172-
} else {
160+
161+
if opts.SkipFiltering {
173162
chunks = chunkChan(pathsChan, c.lowlevel.GetLimits().FileCountLimit)
163+
} else {
164+
filter, err := c.createFileFilter(ctx)
165+
if err != nil {
166+
return err
167+
}
168+
chunks = chunkChanFiltered(pathsChan, c.lowlevel.GetLimits().FileCountLimit, filter)
174169
}
175170

176171
for chunk := range chunks {
@@ -205,7 +200,7 @@ func (c *Client) AddFileToRevision(ctx context.Context, revisionID RevisionID, f
205200
func (c *Client) AddDirToRevision(ctx context.Context, revisionID RevisionID, dirPath string, opts UploadOptions) error {
206201
sources, err := listsources.ForPath(dirPath, nil, runtime.NumCPU())
207202
if err != nil {
208-
return err
203+
return fmt.Errorf("failed to list files in directory %s: %w", dirPath, err)
209204
}
210205

211206
return c.addPathsToRevision(ctx, revisionID, dirPath, sources, opts)

0 commit comments

Comments
 (0)