-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (110 loc) · 2.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bufio"
"context"
"fmt"
"math/rand"
"sync"
"github.com/PerformLine/go-stockutil/colorutil"
flags "github.com/jessevdk/go-flags"
"github.com/jwalton/gchalk"
"github.com/ruckc/dockerlogs/pkg"
)
type Options struct {
Tail string `short:"t" long:"tail" description:"Number of lines to show from the end of the logs" default:"all"`
Containers struct {
Names []string `positional-arg-name:"containers"`
} `positional-args:"yes"`
}
func main() {
opts := Options{}
parser := flags.NewParser(&opts, flags.Default)
parser.ShortDescription = "tails multiple containers concurrently"
parser.LongDescription = "tails multiple containers concurrently"
_, err := parser.Parse()
if err != nil {
return
}
ctx := context.Background()
dp := pkg.NewLocalDockerProvider()
dp.Refresh(ctx)
sources := dp.GetSources()
prefixLength := 0
for _, v := range sources {
prefixLength = Max(prefixLength, len(v.Name()))
}
wg := sync.WaitGroup{}
filteredSources := make([]pkg.Source, 0)
for _, v := range sources {
if len(opts.Containers.Names) == 0 || contains(opts.Containers.Names, v.Name()) {
filteredSources = append(filteredSources, v)
}
}
streams := make([]StreamPair, len(filteredSources))
for i, v := range filteredSources {
name := v.Name()
out, err := v.Tail(ctx, true, opts.Tail)
r, g, b := generateRgb(i, len(streams))
streams[i] = StreamPair{
prefix: fmt.Sprintf("%-*s", prefixLength, name),
color: gchalk.RGB(r, g, b),
bold: gchalk.WithRGB(r, g, b).Bold,
stdout: bufio.NewScanner(out),
stderr: bufio.NewScanner(err),
wg: &wg,
}
}
out := make(chan string)
wg.Add(len(streams) * 2)
go func() {
wg.Wait()
close(out)
}()
for _, stream := range streams {
stream.Tail(out)
}
for val := range out {
fmt.Println(val)
}
}
type StreamPair struct {
prefix string
color gchalk.ColorFn
bold gchalk.ColorFn
stdout *bufio.Scanner
stderr *bufio.Scanner
wg *sync.WaitGroup
}
func (sp *StreamPair) Tail(out chan string) {
go tail(out, sp.stdout, sp.prefix, sp.color, sp.wg)
go tail(out, sp.stderr, sp.prefix, sp.bold, sp.wg)
}
func tail(out chan string, in *bufio.Scanner, prefix string, color gchalk.ColorFn, wg *sync.WaitGroup) {
defer wg.Done()
for in.Scan() {
bytes := in.Bytes()
if len(bytes) > 8 {
out <- color(prefix + " " + string(bytes[8:]))
}
}
}
func generateRgb(i int, size int) (r uint8, g uint8, b uint8) {
h := (float64(i) / float64(size)) * 360
s := rand.Float64()
l := 0.5
return colorutil.HslToRgb(h, s, l)
}
func Max(a int, b int) int {
if a > b {
return a
}
return b
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}