@@ -170,40 +170,51 @@ func (a *agent) writeFile(ctx context.Context, r *http.Request, path string) (HT
170
170
return 0 , nil
171
171
}
172
172
173
- func (a * agent ) HandleEditFile (rw http.ResponseWriter , r * http.Request ) {
173
+ func (a * agent ) HandleEditFiles (rw http.ResponseWriter , r * http.Request ) {
174
174
ctx := r .Context ()
175
175
176
- query := r .URL .Query ()
177
- parser := httpapi .NewQueryParamParser ().RequiredNotEmpty ("path" )
178
- path := parser .String (query , "" , "path" )
179
- parser .ErrorExcessParams (query )
180
- if len (parser .Errors ) > 0 {
176
+ var req workspacesdk.FileEditRequest
177
+ if ! httpapi .Read (ctx , rw , r , & req ) {
178
+ return
179
+ }
180
+
181
+ if len (req .Files ) == 0 {
181
182
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
182
- Message : "Query parameters have invalid values." ,
183
- Validations : parser .Errors ,
183
+ Message : "must specify at least one file" ,
184
184
})
185
185
return
186
186
}
187
187
188
- var edits workspacesdk.FileEditRequest
189
- if ! httpapi .Read (ctx , rw , r , & edits ) {
190
- return
188
+ var combinedErr error
189
+ status := http .StatusOK
190
+ for _ , edit := range req .Files {
191
+ s , err := a .editFile (r .Context (), edit .Path , edit .Edits )
192
+ // Keep the highest response status, so 500 will be preferred over 400, etc.
193
+ if s > status {
194
+ status = s
195
+ }
196
+ if err != nil {
197
+ combinedErr = errors .Join (combinedErr , err )
198
+ }
191
199
}
192
200
193
- status , err := a .editFile (r .Context (), path , edits .Edits )
194
- if err != nil {
201
+ if combinedErr != nil {
195
202
httpapi .Write (ctx , rw , status , codersdk.Response {
196
- Message : err .Error (),
203
+ Message : combinedErr .Error (),
197
204
})
198
205
return
199
206
}
200
207
201
208
httpapi .Write (ctx , rw , http .StatusOK , codersdk.Response {
202
- Message : fmt . Sprintf ( "Successfully edited %q" , path ) ,
209
+ Message : "Successfully edited file(s)" ,
203
210
})
204
211
}
205
212
206
213
func (a * agent ) editFile (ctx context.Context , path string , edits []workspacesdk.FileEdit ) (int , error ) {
214
+ if path == "" {
215
+ return http .StatusBadRequest , xerrors .New ("\" path\" is required" )
216
+ }
217
+
207
218
if ! filepath .IsAbs (path ) {
208
219
return http .StatusBadRequest , xerrors .Errorf ("file path must be absolute: %q" , path )
209
220
}
0 commit comments