2021-02-04 10:04:42 +01:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FilteredLogConsumer filters logs for given services
|
|
|
|
func FilteredLogConsumer(consumer compose.LogConsumer, services []string) compose.LogConsumer {
|
|
|
|
if len(services) == 0 {
|
|
|
|
return consumer
|
|
|
|
}
|
|
|
|
allowed := map[string]bool{}
|
|
|
|
for _, s := range services {
|
|
|
|
allowed[s] = true
|
|
|
|
}
|
|
|
|
return &allowListLogConsumer{
|
|
|
|
allowList: allowed,
|
|
|
|
delegate: consumer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type allowListLogConsumer struct {
|
|
|
|
allowList map[string]bool
|
|
|
|
delegate compose.LogConsumer
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:56:26 +01:00
|
|
|
func (a *allowListLogConsumer) Log(container, service, message string) {
|
2021-02-22 11:40:26 +01:00
|
|
|
if a.allowList[service] {
|
2021-03-16 14:56:26 +01:00
|
|
|
a.delegate.Log(container, service, message)
|
2021-02-04 10:04:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:56:26 +01:00
|
|
|
func (a *allowListLogConsumer) Status(container, message string) {
|
|
|
|
if a.allowList[container] {
|
|
|
|
a.delegate.Status(container, message)
|
2021-02-09 16:51:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:56:26 +01:00
|
|
|
func (a *allowListLogConsumer) Register(name string) {
|
2021-02-16 10:43:35 +01:00
|
|
|
if a.allowList[name] {
|
2021-03-16 14:56:26 +01:00
|
|
|
a.delegate.Register(name)
|
2021-02-16 10:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWriter creates a io.Writer that will actually split by line and format by LogConsumer
|
2021-03-24 15:37:08 +01:00
|
|
|
func GetWriter(container, service string, events compose.ContainerEventListener) io.WriteCloser {
|
2021-02-16 10:43:35 +01:00
|
|
|
return &splitBuffer{
|
|
|
|
buffer: bytes.Buffer{},
|
|
|
|
service: service,
|
|
|
|
container: container,
|
|
|
|
consumer: events,
|
2021-02-12 10:02:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 10:04:42 +01:00
|
|
|
type splitBuffer struct {
|
2021-02-16 10:43:35 +01:00
|
|
|
buffer bytes.Buffer
|
2021-02-04 10:04:42 +01:00
|
|
|
service string
|
|
|
|
container string
|
2021-02-16 10:43:35 +01:00
|
|
|
consumer compose.ContainerEventListener
|
2021-02-04 10:04:42 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 10:43:35 +01:00
|
|
|
// 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
|
2021-02-04 10:04:42 +01:00
|
|
|
}
|
2021-02-16 10:43:35 +01:00
|
|
|
line := s.buffer.Next(index + 1)
|
|
|
|
s.consumer(compose.ContainerEvent{
|
2021-03-16 14:56:26 +01:00
|
|
|
Type: compose.ContainerEventLog,
|
|
|
|
Service: s.service,
|
|
|
|
Container: s.container,
|
|
|
|
Line: string(line[:len(line)-1]),
|
2021-02-16 10:43:35 +01:00
|
|
|
})
|
2021-02-04 10:04:42 +01:00
|
|
|
}
|
2021-02-16 10:43:35 +01:00
|
|
|
return n, nil
|
2021-02-04 10:04:42 +01:00
|
|
|
}
|
2021-03-24 15:37:08 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|