Skip to content

Commit 414c0c9

Browse files
authored
Add optional depth parameter to envd's ListDir (#524)
1 parent 13a41c8 commit 414c0c9

8 files changed

Lines changed: 389 additions & 258 deletions

File tree

packages/envd/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ generate:
4343
echo "buf is not installed. Do you want to install it? (Y/n): "; \
4444
read choice; \
4545
if [ "$$choice" = "Y" ]; then \
46-
go install github.com/bufbuild/buf/cmd/buf@latest && \
47-
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && \
48-
go install connectrpc.com/connect/cmd/protoc-gen-connect-go@latest; \
46+
go install github.com/bufbuild/buf/cmd/buf@v1.50.1 && \
47+
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.1 && \
48+
go install connectrpc.com/connect/cmd/protoc-gen-connect-go@v1.18.1; \
4949
else \
5050
exit 1; \
5151
fi; \

packages/envd/internal/services/filesystem/dir.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"os"
77
"path"
8+
"path/filepath"
9+
"strings"
810

911
"github.com/e2b-dev/infra/packages/envd/internal/permissions"
1012
rpc "github.com/e2b-dev/infra/packages/envd/internal/services/spec/filesystem"
@@ -13,6 +15,11 @@ import (
1315
)
1416

1517
func (Service) ListDir(ctx context.Context, req *connect.Request[rpc.ListDirRequest]) (*connect.Response[rpc.ListDirResponse], error) {
18+
depth := req.Msg.GetDepth()
19+
if depth == 0 {
20+
depth = 1 // default depth to current directory
21+
}
22+
1623
u, err := permissions.GetAuthUser(ctx)
1724
if err != nil {
1825
return nil, err
@@ -36,7 +43,31 @@ func (Service) ListDir(ctx context.Context, req *connect.Request[rpc.ListDirRequ
3643
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("path is not a directory: %s", dirPath))
3744
}
3845

39-
entries, err := os.ReadDir(dirPath)
46+
var entries []os.DirEntry
47+
err = filepath.WalkDir(dirPath, func(path string, d os.DirEntry, err error) error {
48+
if err != nil {
49+
return err
50+
}
51+
52+
// Skip the root directory itself
53+
if path == dirPath {
54+
return nil
55+
}
56+
57+
// Calculate current depth
58+
relPath, err := filepath.Rel(dirPath, path)
59+
if err != nil {
60+
return err
61+
}
62+
currentDepth := len(strings.Split(relPath, string(os.PathSeparator)))
63+
64+
if currentDepth > int(depth) {
65+
return filepath.SkipDir
66+
}
67+
68+
entries = append(entries, d)
69+
return nil
70+
})
4071
if err != nil {
4172
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("error reading directory: %w", err))
4273
}

packages/envd/internal/services/spec/filesystem/filesystem.pb.go

Lines changed: 130 additions & 121 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/envd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const (
3232

3333
var (
3434
// These vars are automatically set by goreleaser.
35-
Version = "0.1.5"
35+
Version = "0.1.6"
3636

3737
commitSHA string
3838

packages/envd/spec/filesystem/filesystem.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ enum FileType {
6262

6363
message ListDirRequest {
6464
string path = 1;
65+
uint32 depth = 2;
6566
}
6667

6768
message ListDirResponse {

tests/integration/README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
# Integration Tests
2+
23
Package for defining integration tests. Currently, there is a setup for API and Orchestrator testing.
34

45
## Run locally
5-
1) Setup env variables in the .env file
6-
2) If necessary, run `make connect-orchestrator` to create a tunnel to one orchestrator client VM in GCP
7-
3) Run `make test`
6+
7+
1. Setup env variables in the root folder `infra/.env` file
8+
2. If you made changes to the `api` or `envd` protobuf spec, run `make generate` from this folder (and don't forget to generate it in `envd` if changes apply there too).
9+
3. If necessary, run `make connect-orchestrator` to create a tunnel to one orchestrator client VM in GCP (you may need to run `make setup-ssh` the first time)
10+
4. Run `make test` in this folder or `make test-integration` from the root `infra/` folder.
811

912
## Usage of clients (api, orchestrator, envd)
13+
1014
All tests are in the folder internal/tests. You can see the usage of different clients in the tests. Here are just basics.
1115

1216
### API
17+
1318
HTTP client. In order to pass the API key, use the `setup.WithAPIKey()` option.
19+
1420
```go
1521
client := setup.GetAPIClient()
1622

@@ -21,19 +27,24 @@ resp, err := client.PostSandboxesWithResponse(ctx, api.NewSandbox{
2127
```
2228

2329
### Orchestrator
30+
2431
GRPC client. There is no authentication needed as it runs behind API in production.
32+
2533
```go
2634
client := setup.GetOrchestratorClient(t, ctx)
2735
resp, err := client.List(ctx, &emptypb.Empty{})
2836
```
2937

3038
### Envd
39+
3140
Envd client is used for interacting with the sandbox.
3241
There are two API types—HTTP and GRPC.
3342
Each of them provides different methods for interacting with the sandbox; you need to check which ones you need.
3443

3544
#### HTTP
45+
3646
In order to access correct sandbox URL, you need to pass `setup.WithSandbox(...)` with the required arguments.
47+
3748
```go
3849
client := setup.GetEnvdClient(t, ctx)
3950
resp, err := client.HTTPClient.PostFilesWithBodyWithResponse(
@@ -44,10 +55,12 @@ resp, err := client.HTTPClient.PostFilesWithBodyWithResponse(
4455
```
4556

4657
#### GRPC
58+
4759
In order to access correct sandbox URL, you need to call `setup.SetSandboxHeader(...)` with the required arguments.
4860

4961
All methods also expect a user (`user`/`root`) to be set in the header.
5062
You can achieve it using `setup.SetUserHeader(...)`.
63+
5164
```go
5265
client := setup.GetEnvdClient(t, ctx)
5366
req := connect.NewRequest(&filesystem.ListDirRequest{

0 commit comments

Comments
 (0)