/
logs.go
115 lines (109 loc) · 2.88 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
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
package cli
import (
"bufio"
"fmt"
"github.com/containerum/chkit/pkg/chkitErrors"
"github.com/containerum/chkit/pkg/cli/prerun"
"github.com/containerum/chkit/pkg/client"
"github.com/containerum/chkit/pkg/context"
"github.com/containerum/chkit/pkg/util/activekit"
"github.com/containerum/chkit/pkg/util/angel"
"github.com/containerum/chkit/pkg/util/ferr"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
ErrUnableToReadLogs chkitErrors.Err = "unable to read logs"
)
var logsCommandAliases = []string{"log"}
func Logs(ctx *context.Context) *cobra.Command {
var logsConfig = struct {
Follow bool
Prev bool
Tail uint
}{}
command := &cobra.Command{
Use: "logs",
Aliases: logsCommandAliases,
Short: "View pod logs",
Example: `logs pod_label [container] [--follow] [--prev] [--tail n] [--quiet]`,
PreRun: func(cmd *cobra.Command, args []string) {
if err := prerun.PreRun(ctx); err != nil {
angel.Angel(ctx, err)
ctx.Exit(1)
}
if err := prerun.GetNamespaceByUserfriendlyID(ctx, cmd.Flags()); err != nil {
ferr.Println(err)
ctx.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
var podName string
var containerName string
client := ctx.GetClient()
switch len(args) {
case 2:
containerName = args[1]
fallthrough
case 1:
podName = args[0]
default:
var pods, err = client.GetPodList(ctx.GetNamespace().ID)
if err != nil {
ferr.Println(err)
ctx.Exit(1)
}
(&activekit.Menu{
Title: "Select pod",
Items: activekit.ItemsFromIter(uint(pods.Len()), func(index uint) *activekit.MenuItem {
var po = pods[index]
return &activekit.MenuItem{
Label: po.Name,
Action: func() error {
podName = po.Name
return nil
},
}
}),
}).Run()
}
params := chClient.GetPodLogsParams{
Namespace: ctx.GetNamespace().ID,
Pod: podName,
Container: containerName,
Follow: logsConfig.Follow,
Previous: logsConfig.Prev,
Tail: int(logsConfig.Tail),
}
rc, err := client.GetPodLogs(params)
if err != nil {
logrus.WithError(err).Errorf("error while getting logs")
ferr.Println(err)
ctx.Exit(1)
}
defer rc.Close()
scanner := bufio.NewScanner(rc)
var nLines uint64
for scanner.Scan() {
if err := scanner.Err(); err != nil {
err = ErrUnableToReadLogs.Wrap(err)
logrus.WithError(err).Errorf("unable to scan logs byte stream")
activekit.Attention(err.Error())
ctx.Exit(1)
}
fmt.Println(scanner.Text())
nLines++
}
if err != nil {
activekit.Attention(err.Error())
ctx.Exit(1)
}
},
PostRun: ctx.CobraPostRun,
}
command.PersistentFlags().
BoolVarP(&logsConfig.Follow, "follow", "f", false, `follow pod logs`)
command.PersistentFlags().
UintVarP(&logsConfig.Tail, "tail", "t", 100, `print last <value> log lines`)
return command
}