Browse Source

Merge pull request #1760 from ndeloof/split_writer

code cleanup: splitWriter does not use service/container name
Nicolas De loof 4 years ago
parent
commit
45a2c422e1
6 changed files with 79 additions and 69 deletions
  1. 2 2
      kube/client/client.go
  2. 11 4
      local/compose/attach.go
  3. 2 2
      local/compose/logs.go
  4. 0 57
      utils/logs.go
  5. 62 0
      utils/writer.go
  6. 2 4
      utils/writer_test.go

+ 2 - 2
kube/client/client.go

@@ -211,8 +211,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 {

+ 11 - 4
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
 }

+ 2 - 2
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)

+ 0 - 57
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
-}

+ 62 - 0
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
+}

+ 2 - 4
utils/logs_test.go → 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