MCPcopy
hub / github.com/dagger/container-use / FileRead

Method FileRead

environment/filesystem.go:12–40  ·  view source on GitHub ↗
(ctx context.Context, targetFile string, shouldReadEntireFile bool, startLineOneIndexedInclusive int, endLineOneIndexedInclusive int)

Source from the content-addressed store, hash-verified

10)
11
12func (env *Environment) FileRead(ctx context.Context, targetFile string, shouldReadEntireFile bool, startLineOneIndexedInclusive int, endLineOneIndexedInclusive int) (string, error) {
13 file, err := env.container().File(targetFile).Contents(ctx)
14 if err != nil {
15 return "", err
16 }
17 if shouldReadEntireFile {
18 return file, err
19 }
20
21 lines := strings.Split(file, "\n")
22 start := startLineOneIndexedInclusive - 1
23 start = max(start, 0)
24 if start >= len(lines) {
25 start = len(lines) - 1
26 }
27 if start < 0 {
28 return "", fmt.Errorf("error reading file: start_line_one_indexed_inclusive (%d) cannot be less than 1", startLineOneIndexedInclusive)
29 }
30 end := endLineOneIndexedInclusive
31
32 if end >= len(lines) {
33 end = len(lines) - 1
34 }
35 if end < start {
36 return "", fmt.Errorf("error reading file: end_line_one_indexed_inclusive (%d) must be greater than start_line_one_indexed_inclusive (%d)", endLineOneIndexedInclusive, startLineOneIndexedInclusive)
37 }
38
39 return strings.Join(lines[start:end], "\n"), nil
40}
41
42func (env *Environment) FileWrite(ctx context.Context, explanation, targetFile, contents string) error {
43 err := env.apply(ctx, env.container().WithNewFile(targetFile, contents))

Callers 1

Calls 1

containerMethod · 0.95

Tested by

no test coverage detected