diff --git a/kube/client/client.go b/kube/client/client.go index 6c4d6eb01..a99e8e3b0 100644 --- a/kube/client/client.go +++ b/kube/client/client.go @@ -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 { diff --git a/local/compose/attach.go b/local/compose/attach.go index fc8f67398..1114be9bb 100644 --- a/local/compose/attach.go +++ b/local/compose/attach.go @@ -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 } diff --git a/local/compose/logs.go b/local/compose/logs.go index c708280e7..5cb553369 100644 --- a/local/compose/logs.go +++ b/local/compose/logs.go @@ -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) diff --git a/utils/logs.go b/utils/logs.go index 1c918d751..793ece9d0 100644 --- a/utils/logs.go +++ b/utils/logs.go @@ -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 -} diff --git a/utils/writer.go b/utils/writer.go new file mode 100644 index 000000000..83f0bf5c3 --- /dev/null +++ b/utils/writer.go @@ -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 +} diff --git a/utils/logs_test.go b/utils/writer_test.go similarity index 87% rename from utils/logs_test.go rename to utils/writer_test.go index f0eaf835c..7eedb844f 100644 --- a/utils/logs_test.go +++ b/utils/writer_test.go @@ -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