2020-08-18 11:38:23 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-08-18 11:38:23 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-08-17 17:48:52 +02:00
|
|
|
package ecs
|
2020-05-13 12:14:13 +02:00
|
|
|
|
|
|
|
import (
|
2020-08-11 10:43:11 +02:00
|
|
|
"bytes"
|
2020-05-13 12:14:13 +02:00
|
|
|
"context"
|
2020-05-27 16:50:04 +02:00
|
|
|
"fmt"
|
2020-08-11 10:43:11 +02:00
|
|
|
"io"
|
2020-05-27 16:50:04 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-05-13 12:14:13 +02:00
|
|
|
)
|
|
|
|
|
2020-08-20 16:42:37 +02:00
|
|
|
func (b *ecsAPIService) Logs(ctx context.Context, project string, w io.Writer) error {
|
2020-08-18 09:13:58 +02:00
|
|
|
consumer := logConsumer{
|
2020-08-18 16:56:42 +02:00
|
|
|
colors: map[string]colorFunc{},
|
2020-05-27 16:50:04 +02:00
|
|
|
width: 0,
|
2020-08-20 16:42:37 +02:00
|
|
|
writer: w,
|
2020-08-18 09:13:58 +02:00
|
|
|
}
|
2020-10-12 16:21:48 +02:00
|
|
|
err := b.aws.GetLogs(ctx, project, consumer.Log)
|
2020-08-21 15:56:31 +02:00
|
|
|
return err
|
2020-05-27 16:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logConsumer) Log(service, container, message string) {
|
|
|
|
cf, ok := l.colors[service]
|
|
|
|
if !ok {
|
2020-08-18 16:56:42 +02:00
|
|
|
cf = <-loop
|
2020-05-27 16:50:04 +02:00
|
|
|
l.colors[service] = cf
|
|
|
|
l.computeWidth()
|
|
|
|
}
|
|
|
|
prefix := fmt.Sprintf("%-"+strconv.Itoa(l.width)+"s |", service)
|
2020-08-11 10:43:11 +02:00
|
|
|
|
2020-05-27 16:50:04 +02:00
|
|
|
for _, line := range strings.Split(message, "\n") {
|
2020-08-11 10:43:11 +02:00
|
|
|
buf := bytes.NewBufferString(fmt.Sprintf("%s %s\n", cf(prefix), line))
|
2020-08-18 11:38:23 +02:00
|
|
|
l.writer.Write(buf.Bytes()) // nolint:errcheck
|
2020-05-27 16:50:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logConsumer) computeWidth() {
|
|
|
|
width := 0
|
|
|
|
for n := range l.colors {
|
|
|
|
if len(n) > width {
|
|
|
|
width = len(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.width = width + 3
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:22:12 +02:00
|
|
|
type logConsumer struct {
|
2020-08-18 16:56:42 +02:00
|
|
|
colors map[string]colorFunc
|
2020-06-11 19:22:12 +02:00
|
|
|
width int
|
2020-08-11 10:43:11 +02:00
|
|
|
writer io.Writer
|
2020-05-13 12:14:13 +02:00
|
|
|
}
|