@@ -13,26 +13,24 @@ import (
13
13
"github.com/puzpuzpuz/xsync"
14
14
15
15
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 "
17
17
)
18
18
19
19
// Config contains configuration for the file upload client.
20
20
type Config struct {
21
21
BaseURL string
22
- OrgID uuid. UUID
22
+ OrgID OrgID
23
23
}
24
24
25
25
// Client provides high-level file upload functionality.
26
26
type Client struct {
27
27
httpClient * http.Client
28
- lowlevel lowlevel .SealableClient
28
+ lowlevel uploadrevision .SealableClient
29
29
cfg Config
30
30
filters Filters
31
31
}
32
32
33
33
// 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.
36
34
func NewClient (httpClient * http.Client , cfg Config , opts ... Option ) * Client {
37
35
client := & Client {
38
36
httpClient : httpClient ,
@@ -48,9 +46,9 @@ func NewClient(httpClient *http.Client, cfg Config, opts ...Option) *Client {
48
46
}
49
47
50
48
if client .lowlevel == nil {
51
- client .lowlevel = lowlevel .NewClient (lowlevel .Config {
49
+ client .lowlevel = uploadrevision .NewClient (uploadrevision .Config {
52
50
BaseURL : cfg .BaseURL ,
53
- }, lowlevel .WithHTTPClient (httpClient ))
51
+ }, uploadrevision .WithHTTPClient (httpClient ))
54
52
}
55
53
56
54
return client
@@ -71,9 +69,10 @@ func (c *Client) getDeeproxyFilters(ctx context.Context) (FiltersResponse, error
71
69
if err != nil {
72
70
return filtersResp , fmt .Errorf ("error making deeproxy filters request: %w" , err )
73
71
}
72
+ defer resp .Body .Close ()
74
73
75
74
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 )
77
76
}
78
77
79
78
if err := json .NewDecoder (resp .Body ).Decode (& filtersResp ); err != nil {
@@ -108,11 +107,7 @@ func (c *Client) loadFilters(ctx context.Context) error {
108
107
109
108
// createFileFilter creates a filter function based on the current filtering configuration.
110
109
// 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 ) {
116
111
if err := c .loadFilters (ctx ); err != nil {
117
112
return nil , fmt .Errorf ("failed to load deeproxy filters: %w" , err )
118
113
}
@@ -127,7 +122,7 @@ func (c *Client) createFileFilter(ctx context.Context, opts UploadOptions) (func
127
122
}
128
123
129
124
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 )
131
126
defer func () {
132
127
for _ , file := range files {
133
128
file .File .Close ()
@@ -137,40 +132,40 @@ func (c *Client) uploadPaths(ctx context.Context, revID RevisionID, rootPath str
137
132
for _ , pth := range paths {
138
133
relPth , err := filepath .Rel (rootPath , pth )
139
134
if err != nil {
140
- return err
135
+ return fmt . Errorf ( "failed to get relative path for %s: %w" , pth , err )
141
136
}
142
137
143
138
f , err := os .Open (pth )
144
139
if err != nil {
145
- return err
140
+ return fmt . Errorf ( "failed to open file %s: %w" , pth , err )
146
141
}
147
142
148
- files = append (files , lowlevel .UploadFile {
143
+ files = append (files , uploadrevision .UploadFile {
149
144
Path : relPth ,
150
145
File : f ,
151
146
})
152
147
}
153
148
154
149
err := c .lowlevel .UploadFiles (ctx , c .cfg .OrgID , revID , files )
155
150
if err != nil {
156
- return err
151
+ return fmt . Errorf ( "failed to upload files: %w" , err )
157
152
}
158
153
159
154
return nil
160
155
}
161
156
162
157
// addPathsToRevision adds multiple file paths to an existing revision.
163
158
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
-
169
159
var chunks <- chan []string
170
- if filter != nil {
171
- chunks = chunkChanFiltered (pathsChan , c .lowlevel .GetLimits ().FileCountLimit , filter )
172
- } else {
160
+
161
+ if opts .SkipFiltering {
173
162
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 )
174
169
}
175
170
176
171
for chunk := range chunks {
@@ -205,7 +200,7 @@ func (c *Client) AddFileToRevision(ctx context.Context, revisionID RevisionID, f
205
200
func (c * Client ) AddDirToRevision (ctx context.Context , revisionID RevisionID , dirPath string , opts UploadOptions ) error {
206
201
sources , err := listsources .ForPath (dirPath , nil , runtime .NumCPU ())
207
202
if err != nil {
208
- return err
203
+ return fmt . Errorf ( "failed to list files in directory %s: %w" , dirPath , err )
209
204
}
210
205
211
206
return c .addPathsToRevision (ctx , revisionID , dirPath , sources , opts )
0 commit comments