Skip to content

Commit a42a462

Browse files
authored
Commands to list services and output logs (#257)
Related to #236
1 parent 7ea4a6f commit a42a462

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ docker logs -f $(docker ps | grep contender | cut -d' ' -f1)
126126

127127
To stop the playground, press `Ctrl+C`.
128128

129+
## Logs
130+
131+
```bash
132+
$ builder-playground list
133+
mev-boost-relay
134+
beacon
135+
el
136+
validator
137+
$ builder-playground logs validator
138+
```
139+
129140
## Inspect
130141

131142
Builder-playground supports inspecting the connection of a service to a specific port.

main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,34 @@ var inspectCmd = &cobra.Command{
105105
},
106106
}
107107

108+
var logsCmd = &cobra.Command{
109+
Use: "logs",
110+
Short: "Show logs for a service",
111+
RunE: func(cmd *cobra.Command, args []string) error {
112+
// one argument, the name of the service
113+
if len(args) != 1 {
114+
return fmt.Errorf("please specify a service name")
115+
}
116+
serviceName := args[0]
117+
118+
ctx := mainctx.Get()
119+
cmd.SilenceUsage = true
120+
if err := playground.Logs(ctx, serviceName); err != nil && !strings.Contains(err.Error(), "signal") {
121+
return fmt.Errorf("failed to show logs: %w", err)
122+
}
123+
return nil
124+
},
125+
}
126+
127+
var listCmd = &cobra.Command{
128+
Use: "list",
129+
Short: "List all running services",
130+
RunE: func(cmd *cobra.Command, args []string) error {
131+
ctx := mainctx.Get()
132+
return playground.List(ctx)
133+
},
134+
}
135+
108136
var versionCmd = &cobra.Command{
109137
Use: "version",
110138
Short: "Print the version",
@@ -158,6 +186,8 @@ func main() {
158186

159187
rootCmd.AddCommand(startCmd)
160188
rootCmd.AddCommand(inspectCmd)
189+
rootCmd.AddCommand(logsCmd)
190+
rootCmd.AddCommand(listCmd)
161191
rootCmd.AddCommand(versionCmd)
162192

163193
rootCmd.AddCommand(stopCmd)

playground/cmd_list.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package playground
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/docker/docker/api/types/container"
8+
)
9+
10+
func List(ctx context.Context) error {
11+
client, err := newDockerClient()
12+
if err != nil {
13+
return fmt.Errorf("failed to create docker client: %w", err)
14+
}
15+
16+
// TODO: Filter by session ID when we introduce multiple sessions soon.
17+
containers, err := client.ContainerList(ctx, container.ListOptions{
18+
All: true,
19+
})
20+
if err != nil {
21+
return fmt.Errorf("error getting container list: %w", err)
22+
}
23+
24+
for _, container := range containers {
25+
if container.Labels["playground"] != "true" {
26+
continue
27+
}
28+
fmt.Println(container.Labels["com.docker.compose.service"])
29+
}
30+
31+
return nil
32+
}

playground/cmd_logs.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package playground
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
9+
"github.com/docker/docker/api/types/container"
10+
)
11+
12+
func Logs(ctx context.Context, serviceName string) error {
13+
client, err := newDockerClient()
14+
if err != nil {
15+
return fmt.Errorf("failed to create docker client: %w", err)
16+
}
17+
18+
// TODO: Filter by session ID when we introduce multiple sessions soon.
19+
containers, err := client.ContainerList(ctx, container.ListOptions{
20+
All: true,
21+
})
22+
if err != nil {
23+
return fmt.Errorf("error getting container list: %w", err)
24+
}
25+
26+
for _, container := range containers {
27+
if container.Labels["playground"] == "true" &&
28+
container.Labels["com.docker.compose.service"] == serviceName {
29+
cmd := exec.CommandContext(ctx, "docker", "logs", "-f", "--tail", "50", container.ID)
30+
cmd.Stdout = os.Stdout
31+
cmd.Stderr = os.Stderr
32+
return cmd.Run()
33+
}
34+
}
35+
36+
return fmt.Errorf("no container found for service %s", serviceName)
37+
}

0 commit comments

Comments
 (0)