-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.go
More file actions
32 lines (27 loc) · 776 Bytes
/
server.go
File metadata and controls
32 lines (27 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package executor
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
// executeShowDatabases executes a show dbs/databases command.
func executeShowDatabases(ctx context.Context, client *mongo.Client) (*Result, error) {
names, err := client.ListDatabaseNames(ctx, bson.D{})
if err != nil {
return nil, fmt.Errorf("list databases failed: %w", err)
}
rows := make([]string, 0, len(names))
for _, name := range names {
doc := bson.M{"name": name}
jsonBytes, err := bson.MarshalExtJSONIndent(doc, false, false, "", " ")
if err != nil {
return nil, fmt.Errorf("marshal failed: %w", err)
}
rows = append(rows, string(jsonBytes))
}
return &Result{
Rows: rows,
RowCount: len(rows),
}, nil
}