(rw http.ResponseWriter, r *http.Request)
| 300 | } |
| 301 | |
| 302 | func (api *API) HandleWriteFile(rw http.ResponseWriter, r *http.Request) { |
| 303 | ctx := r.Context() |
| 304 | |
| 305 | query := r.URL.Query() |
| 306 | parser := httpapi.NewQueryParamParser().RequiredNotEmpty("path") |
| 307 | path := parser.String(query, "", "path") |
| 308 | parser.ErrorExcessParams(query) |
| 309 | if len(parser.Errors) > 0 { |
| 310 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 311 | Message: "Query parameters have invalid values.", |
| 312 | Validations: parser.Errors, |
| 313 | }) |
| 314 | return |
| 315 | } |
| 316 | |
| 317 | status, err := api.writeFile(ctx, r, path) |
| 318 | if err != nil { |
| 319 | httpapi.Write(ctx, rw, status, codersdk.Response{ |
| 320 | Message: err.Error(), |
| 321 | }) |
| 322 | return |
| 323 | } |
| 324 | |
| 325 | // Track edited path for git watch. |
| 326 | if api.pathStore != nil { |
| 327 | if chatContext, ok := agentchat.FromContext(ctx); ok { |
| 328 | api.pathStore.AddPaths(append([]uuid.UUID{chatContext.ID}, chatContext.AncestorIDs...), []string{path}) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{ |
| 333 | Message: fmt.Sprintf("Successfully wrote to %q", path), |
| 334 | }) |
| 335 | } |
| 336 | |
| 337 | func (api *API) writeFile(ctx context.Context, r *http.Request, path string) (HTTPResponseCode, error) { |
| 338 | if !filepath.IsAbs(path) { |
nothing calls this directly
no test coverage detected