ReadFile reads from a file from the workspace, returning a file reader and the mime type.
(ctx context.Context, path string, offset, limit int64)
| 992 | // ReadFile reads from a file from the workspace, returning a file reader and |
| 993 | // the mime type. |
| 994 | func (c *agentConn) ReadFile(ctx context.Context, path string, offset, limit int64) (io.ReadCloser, string, error) { |
| 995 | ctx, span := tracing.StartSpan(ctx) |
| 996 | defer span.End() |
| 997 | |
| 998 | //nolint:bodyclose // we want to return the body so the caller can stream. |
| 999 | res, err := c.apiRequest(ctx, http.MethodGet, agentAPIPath("/api/v0/read-file", neturl.Values{ |
| 1000 | "path": []string{path}, |
| 1001 | "offset": []string{strconv.FormatInt(offset, 10)}, |
| 1002 | "limit": []string{strconv.FormatInt(limit, 10)}, |
| 1003 | }), nil) |
| 1004 | if err != nil { |
| 1005 | return nil, "", xerrors.Errorf("do request: %w", err) |
| 1006 | } |
| 1007 | if res.StatusCode != http.StatusOK { |
| 1008 | // codersdk.ReadBodyAsError will close the body. |
| 1009 | return nil, "", codersdk.ReadBodyAsError(res) |
| 1010 | } |
| 1011 | |
| 1012 | mimeType := res.Header.Get("Content-Type") |
| 1013 | if mimeType == "" { |
| 1014 | mimeType = "application/octet-stream" |
| 1015 | } |
| 1016 | |
| 1017 | return res.Body, mimeType, nil |
| 1018 | } |
| 1019 | |
| 1020 | // WriteFile writes to a file in the workspace. |
| 1021 | func (c *agentConn) WriteFile(ctx context.Context, path string, reader io.Reader) error { |
nothing calls this directly
no test coverage detected