MCPcopy Index your code
hub / github.com/coder/coder / ReadFile

Method ReadFile

codersdk/workspacesdk/agentconn.go:994–1018  ·  view source on GitHub ↗

ReadFile reads from a file from the workspace, returning a file reader and the mime type.

(ctx context.Context, path string, offset, limit int64)

Source from the content-addressed store, hash-verified

992// ReadFile reads from a file from the workspace, returning a file reader and
993// the mime type.
994func (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.
1021func (c *agentConn) WriteFile(ctx context.Context, path string, reader io.Reader) error {

Callers

nothing calls this directly

Calls 6

apiRequestMethod · 0.95
StartSpanFunction · 0.92
ReadBodyAsErrorFunction · 0.92
agentAPIPathFunction · 0.85
GetMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected