Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (74 loc) · 1.68 KB
/
Copy pathmain.go
File metadata and controls
88 lines (74 loc) · 1.68 KB
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
package main
import (
"errors"
"fmt"
"os"
"os/signal"
"sync"
"github.com/rs/zerolog"
"github.com/spf13/pflag"
)
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
output := zerolog.ConsoleWriter{Out: os.Stdout}
output.PartsOrder = []string{"message"}
logger := zerolog.New(output).With().Logger()
var lines *int = pflag.IntP("tail", "n", 10, "Number of lines to show from the end of the logs for each container")
pflag.Parse()
if pflag.NArg() < 1 {
pflag.Usage()
os.Exit(1)
}
dc, err := NewDockerService()
if err != nil {
logger.Error().Err(err).Msg("Is docker service running?")
panic(err)
}
containers, err := dc.GetContainers()
if errors.Is(err, ErrNoContainers) {
logger.Error().Msg("No containers found")
return
} else if err != nil {
panic(err)
}
containerLogs := pflag.Args()
containersToWatch := make([]string, 0)
for key, name := range containers {
for _, cLog := range containerLogs {
if name == cLog {
containersToWatch = append(containersToWatch, key)
continue
}
if key[:len(cLog)] == cLog {
containersToWatch = append(containersToWatch, key)
continue
}
}
}
if len(containersToWatch) == 0 {
logger.Error().Msg("No matching containers found")
return
}
wg := sync.WaitGroup{}
for i, id := range containersToWatch {
wg.Add(1)
go func() {
err := dc.WatchLogs(id, containers[id], fmt.Sprintf("%d", *lines), Colors[i%len(Colors)])
if err != nil {
return
}
}()
}
awaitTermination(dc)
wg.Wait()
}
func awaitTermination(dc *DockerService) {
receiver := make(chan os.Signal)
signal.Notify(receiver, os.Interrupt, os.Kill)
<-receiver
err := dc.Close()
if err != nil {
return
}
os.Exit(0)
}