Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 45a2c42

Browse files
authored
Merge pull request #1760 from ndeloof/split_writer
code cleanup: splitWriter does not use service/container name
2 parents 2edec5a + 9d5026e commit 45a2c42

File tree

6 files changed

+79
-69
lines changed

6 files changed

+79
-69
lines changed

kube/client/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ func (kc *KubeClient) GetLogs(ctx context.Context, projectName string, consumer
211211
for _, pod := range pods.Items {
212212
request := kc.client.CoreV1().Pods(kc.namespace).GetLogs(pod.Name, &corev1.PodLogOptions{Follow: follow})
213213
service := pod.Labels[compose.ServiceTag]
214-
w := utils.GetWriter(pod.Name, service, func(event compose.ContainerEvent) {
215-
consumer.Log(event.Container, event.Service, event.Line)
214+
w := utils.GetWriter(func(line string) {
215+
consumer.Log(pod.Name, service, line)
216216
})
217217

218218
eg.Go(func() error {

local/compose/attach.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,26 @@ func (s *composeService) attach(ctx context.Context, project *types.Project, lis
5858

5959
func (s *composeService) attachContainer(ctx context.Context, container moby.Container, listener compose.ContainerEventListener, project *types.Project) error {
6060
serviceName := container.Labels[serviceLabel]
61-
w := utils.GetWriter(getContainerNameWithoutProject(container), serviceName, listener)
62-
61+
containerName := getContainerNameWithoutProject(container)
6362
service, err := project.GetService(serviceName)
6463
if err != nil {
6564
return err
6665
}
6766

6867
listener(compose.ContainerEvent{
6968
Type: compose.ContainerEventAttach,
70-
Container: getContainerNameWithoutProject(container),
71-
Service: container.Labels[serviceLabel],
69+
Container: containerName,
70+
Service: serviceName,
7271
})
7372

73+
w := utils.GetWriter(func(line string) {
74+
listener(compose.ContainerEvent{
75+
Type: compose.ContainerEventLog,
76+
Container: containerName,
77+
Service: serviceName,
78+
Line: line,
79+
})
80+
})
7481
_, err = s.attachContainerStreams(ctx, container.ID, service.Tty, nil, w)
7582
return err
7683
}

local/compose/logs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
5757
defer r.Close() // nolint errcheck
5858

5959
name := getContainerNameWithoutProject(c)
60-
w := utils.GetWriter(name, service, func(event compose.ContainerEvent) {
61-
consumer.Log(name, service, event.Line)
60+
w := utils.GetWriter(func(line string) {
61+
consumer.Log(name, service, line)
6262
})
6363
if container.Config.Tty {
6464
_, err = io.Copy(w, r)

utils/logs.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package utils
1818

1919
import (
20-
"bytes"
21-
"io"
22-
2320
"github.com/docker/compose-cli/api/compose"
2421
)
2522

@@ -60,57 +57,3 @@ func (a *allowListLogConsumer) Register(name string) {
6057
a.delegate.Register(name)
6158
}
6259
}
63-
64-
// GetWriter creates a io.Writer that will actually split by line and format by LogConsumer
65-
func GetWriter(container, service string, events compose.ContainerEventListener) io.WriteCloser {
66-
return &splitBuffer{
67-
buffer: bytes.Buffer{},
68-
service: service,
69-
container: container,
70-
consumer: events,
71-
}
72-
}
73-
74-
type splitBuffer struct {
75-
buffer bytes.Buffer
76-
service string
77-
container string
78-
consumer compose.ContainerEventListener
79-
}
80-
81-
// Write implements io.Writer. joins all input, splits on the separator and yields each chunk
82-
func (s *splitBuffer) Write(b []byte) (int, error) {
83-
n, err := s.buffer.Write(b)
84-
if err != nil {
85-
return n, err
86-
}
87-
for {
88-
b = s.buffer.Bytes()
89-
index := bytes.Index(b, []byte{'\n'})
90-
if index < 0 {
91-
break
92-
}
93-
line := s.buffer.Next(index + 1)
94-
s.consumer(compose.ContainerEvent{
95-
Type: compose.ContainerEventLog,
96-
Service: s.service,
97-
Container: s.container,
98-
Line: string(line[:len(line)-1]),
99-
})
100-
}
101-
return n, nil
102-
}
103-
104-
func (s *splitBuffer) Close() error {
105-
b := s.buffer.Bytes()
106-
if len(b) == 0 {
107-
return nil
108-
}
109-
s.consumer(compose.ContainerEvent{
110-
Type: compose.ContainerEventLog,
111-
Service: s.service,
112-
Container: s.container,
113-
Line: string(b),
114-
})
115-
return nil
116-
}

utils/writer.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"bytes"
21+
"io"
22+
)
23+
24+
// GetWriter creates a io.Writer that will actually split by line and format by LogConsumer
25+
func GetWriter(consumer func(string)) io.WriteCloser {
26+
return &splitWriter{
27+
buffer: bytes.Buffer{},
28+
consumer: consumer,
29+
}
30+
}
31+
32+
type splitWriter struct {
33+
buffer bytes.Buffer
34+
consumer func(string)
35+
}
36+
37+
// Write implements io.Writer. joins all input, splits on the separator and yields each chunk
38+
func (s *splitWriter) Write(b []byte) (int, error) {
39+
n, err := s.buffer.Write(b)
40+
if err != nil {
41+
return n, err
42+
}
43+
for {
44+
b = s.buffer.Bytes()
45+
index := bytes.Index(b, []byte{'\n'})
46+
if index < 0 {
47+
break
48+
}
49+
line := s.buffer.Next(index + 1)
50+
s.consumer(string(line[:len(line)-1]))
51+
}
52+
return n, nil
53+
}
54+
55+
func (s *splitWriter) Close() error {
56+
b := s.buffer.Bytes()
57+
if len(b) == 0 {
58+
return nil
59+
}
60+
s.consumer(string(b))
61+
return nil
62+
}

utils/logs_test.go renamed to utils/writer_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ import (
2020
"testing"
2121

2222
"gotest.tools/v3/assert"
23-
24-
"github.com/docker/compose-cli/api/compose"
2523
)
2624

2725
func TestSplitWriter(t *testing.T) {
2826
var lines []string
29-
w := GetWriter("container", "service", func(event compose.ContainerEvent) {
30-
lines = append(lines, event.Line)
27+
w := GetWriter(func(line string) {
28+
lines = append(lines, line)
3129
})
3230
w.Write([]byte("h")) //nolint: errcheck
3331
w.Write([]byte("e")) //nolint: errcheck

0 commit comments

Comments
 (0)