-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathlogs.go
88 lines (71 loc) · 2.16 KB
/
logs.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
package commands
import (
"kool-dev/kool/core/builder"
"strconv"
"strings"
"github.com/spf13/cobra"
)
// KoolLogsFlags holds the flags for the logs command
type KoolLogsFlags struct {
Tail int
Follow bool
}
// KoolLogs holds handlers and functions to implement the logs command logic
type KoolLogs struct {
DefaultKoolService
Flags *KoolLogsFlags
list builder.Command
logs builder.Command
}
func AddKoolLogs(root *cobra.Command) {
var (
logs = NewKoolLogs()
logsCmd = NewLogsCommand(logs)
)
root.AddCommand(logsCmd)
}
// NewKoolLogs creates a new handler for logs logic
func NewKoolLogs() *KoolLogs {
return &KoolLogs{
*newDefaultKoolService(),
&KoolLogsFlags{25, false},
builder.NewCommand("docker", "compose", "ps", "-aq"),
builder.NewCommand("docker", "compose", "logs"),
}
}
// Execute runs the logs logic with incoming arguments.
func (l *KoolLogs) Execute(args []string) (err error) {
var services string
if services, err = l.Shell().Exec(l.list, args...); err != nil {
return
}
if services = strings.TrimSpace(services); services == "" {
l.Shell().Warning("There are no containers")
return
}
if l.Flags.Tail == 0 {
l.logs.AppendArgs("--tail", "all")
} else {
l.logs.AppendArgs("--tail", strconv.Itoa(l.Flags.Tail))
}
if l.Flags.Follow {
l.logs.AppendArgs("--follow")
}
err = l.Shell().Interactive(l.logs, args...)
return
}
// NewLogsCommand initializes new kool logs command
func NewLogsCommand(logs *KoolLogs) (logsCmd *cobra.Command) {
logsCmd = &cobra.Command{
Use: "logs [OPTIONS] [SERVICE...]",
Short: "Display log output from running service containers",
Long: `Display log output from all running service containers,
or one or more specified [SERVICE...] containers. Add a '-f' option to the
the command to follow the log output (i.e. 'kool logs -f [SERVICE...]').`,
RunE: DefaultCommandRunFunction(logs),
DisableFlagsInUseLine: true,
}
logsCmd.Flags().IntVarP(&logs.Flags.Tail, "tail", "t", 25, "Number of lines to show from the end of the logs for each container. A value equal to 0 will show all lines.")
logsCmd.Flags().BoolVarP(&logs.Flags.Follow, "follow", "f", false, "Follow log output.")
return
}