зеркало из https://github.com/docker/compose-cli.git
code cleanup: splitWriter does not use service/container name
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Родитель
165686838e
Коммит
9d5026e966
|
@ -96,8 +96,8 @@ func (kc *KubeClient) GetLogs(ctx context.Context, projectName string, consumer
|
|||
for _, pod := range pods.Items {
|
||||
request := kc.client.CoreV1().Pods(kc.namespace).GetLogs(pod.Name, &corev1.PodLogOptions{Follow: follow})
|
||||
service := pod.Labels[compose.ServiceTag]
|
||||
w := utils.GetWriter(pod.Name, service, func(event compose.ContainerEvent) {
|
||||
consumer.Log(event.Container, event.Service, event.Line)
|
||||
w := utils.GetWriter(func(line string) {
|
||||
consumer.Log(pod.Name, service, line)
|
||||
})
|
||||
|
||||
eg.Go(func() error {
|
||||
|
|
|
@ -58,8 +58,7 @@ func (s *composeService) attach(ctx context.Context, project *types.Project, lis
|
|||
|
||||
func (s *composeService) attachContainer(ctx context.Context, container moby.Container, listener compose.ContainerEventListener, project *types.Project) error {
|
||||
serviceName := container.Labels[serviceLabel]
|
||||
w := utils.GetWriter(getContainerNameWithoutProject(container), serviceName, listener)
|
||||
|
||||
containerName := getContainerNameWithoutProject(container)
|
||||
service, err := project.GetService(serviceName)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -67,10 +66,18 @@ func (s *composeService) attachContainer(ctx context.Context, container moby.Con
|
|||
|
||||
listener(compose.ContainerEvent{
|
||||
Type: compose.ContainerEventAttach,
|
||||
Container: getContainerNameWithoutProject(container),
|
||||
Service: container.Labels[serviceLabel],
|
||||
Container: containerName,
|
||||
Service: serviceName,
|
||||
})
|
||||
|
||||
w := utils.GetWriter(func(line string) {
|
||||
listener(compose.ContainerEvent{
|
||||
Type: compose.ContainerEventLog,
|
||||
Container: containerName,
|
||||
Service: serviceName,
|
||||
Line: line,
|
||||
})
|
||||
})
|
||||
_, err = s.attachContainerStreams(ctx, container.ID, service.Tty, nil, w)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -57,8 +57,8 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
|
|||
defer r.Close() // nolint errcheck
|
||||
|
||||
name := getContainerNameWithoutProject(c)
|
||||
w := utils.GetWriter(name, service, func(event compose.ContainerEvent) {
|
||||
consumer.Log(name, service, event.Line)
|
||||
w := utils.GetWriter(func(line string) {
|
||||
consumer.Log(name, service, line)
|
||||
})
|
||||
if container.Config.Tty {
|
||||
_, err = io.Copy(w, r)
|
||||
|
|
|
@ -17,9 +17,6 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/docker/compose-cli/api/compose"
|
||||
)
|
||||
|
||||
|
@ -60,57 +57,3 @@ func (a *allowListLogConsumer) Register(name string) {
|
|||
a.delegate.Register(name)
|
||||
}
|
||||
}
|
||||
|
||||
// GetWriter creates a io.Writer that will actually split by line and format by LogConsumer
|
||||
func GetWriter(container, service string, events compose.ContainerEventListener) io.WriteCloser {
|
||||
return &splitBuffer{
|
||||
buffer: bytes.Buffer{},
|
||||
service: service,
|
||||
container: container,
|
||||
consumer: events,
|
||||
}
|
||||
}
|
||||
|
||||
type splitBuffer struct {
|
||||
buffer bytes.Buffer
|
||||
service string
|
||||
container string
|
||||
consumer compose.ContainerEventListener
|
||||
}
|
||||
|
||||
// Write implements io.Writer. joins all input, splits on the separator and yields each chunk
|
||||
func (s *splitBuffer) Write(b []byte) (int, error) {
|
||||
n, err := s.buffer.Write(b)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
for {
|
||||
b = s.buffer.Bytes()
|
||||
index := bytes.Index(b, []byte{'\n'})
|
||||
if index < 0 {
|
||||
break
|
||||
}
|
||||
line := s.buffer.Next(index + 1)
|
||||
s.consumer(compose.ContainerEvent{
|
||||
Type: compose.ContainerEventLog,
|
||||
Service: s.service,
|
||||
Container: s.container,
|
||||
Line: string(line[:len(line)-1]),
|
||||
})
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (s *splitBuffer) Close() error {
|
||||
b := s.buffer.Bytes()
|
||||
if len(b) == 0 {
|
||||
return nil
|
||||
}
|
||||
s.consumer(compose.ContainerEvent{
|
||||
Type: compose.ContainerEventLog,
|
||||
Service: s.service,
|
||||
Container: s.container,
|
||||
Line: string(b),
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
Copyright 2020 Docker Compose CLI authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
// GetWriter creates a io.Writer that will actually split by line and format by LogConsumer
|
||||
func GetWriter(consumer func(string)) io.WriteCloser {
|
||||
return &splitWriter{
|
||||
buffer: bytes.Buffer{},
|
||||
consumer: consumer,
|
||||
}
|
||||
}
|
||||
|
||||
type splitWriter struct {
|
||||
buffer bytes.Buffer
|
||||
consumer func(string)
|
||||
}
|
||||
|
||||
// Write implements io.Writer. joins all input, splits on the separator and yields each chunk
|
||||
func (s *splitWriter) Write(b []byte) (int, error) {
|
||||
n, err := s.buffer.Write(b)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
for {
|
||||
b = s.buffer.Bytes()
|
||||
index := bytes.Index(b, []byte{'\n'})
|
||||
if index < 0 {
|
||||
break
|
||||
}
|
||||
line := s.buffer.Next(index + 1)
|
||||
s.consumer(string(line[:len(line)-1]))
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (s *splitWriter) Close() error {
|
||||
b := s.buffer.Bytes()
|
||||
if len(b) == 0 {
|
||||
return nil
|
||||
}
|
||||
s.consumer(string(b))
|
||||
return nil
|
||||
}
|
|
@ -20,14 +20,12 @@ import (
|
|||
"testing"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
|
||||
"github.com/docker/compose-cli/api/compose"
|
||||
)
|
||||
|
||||
func TestSplitWriter(t *testing.T) {
|
||||
var lines []string
|
||||
w := GetWriter("container", "service", func(event compose.ContainerEvent) {
|
||||
lines = append(lines, event.Line)
|
||||
w := GetWriter(func(line string) {
|
||||
lines = append(lines, line)
|
||||
})
|
||||
w.Write([]byte("h")) //nolint: errcheck
|
||||
w.Write([]byte("e")) //nolint: errcheck
|
Загрузка…
Ссылка в новой задаче