forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
129 lines (107 loc) · 3.95 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package application
import (
"fmt"
"time"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/ui_helpers"
"github.com/cloudfoundry/loggregatorlib/logmessage"
"github.com/simonleung8/flags"
"github.com/simonleung8/flags/flag"
)
type Logs struct {
ui terminal.UI
config core_config.Reader
logsRepo api.LogsRepository
appReq requirements.ApplicationRequirement
}
func init() {
command_registry.Register(&Logs{})
}
func (cmd *Logs) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["recent"] = &cliFlags.BoolFlag{Name: "recent", Usage: T("Dump recent logs instead of tailing")}
return command_registry.CommandMetadata{
Name: "logs",
Description: T("Tail or show recent logs for an app"),
Usage: T("CF_NAME logs APP_NAME"),
Flags: fs,
}
}
func (cmd *Logs) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("logs"))
}
cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0])
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
cmd.appReq,
}
return
}
func (cmd *Logs) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.config = deps.Config
cmd.logsRepo = deps.RepoLocator.GetLogsRepository()
return cmd
}
func (cmd *Logs) Execute(c flags.FlagContext) {
app := cmd.appReq.GetApplication()
if c.Bool("recent") {
cmd.recentLogsFor(app)
} else {
cmd.tailLogsFor(app)
}
}
func (cmd *Logs) recentLogsFor(app models.Application) {
cmd.ui.Say(T("Connected, dumping recent logs for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...\n",
map[string]interface{}{
"AppName": terminal.EntityNameColor(app.Name),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
messages, err := cmd.logsRepo.RecentLogsFor(app.Guid)
if err != nil {
cmd.handleError(err)
}
for _, msg := range messages {
cmd.ui.Say("%s", LogMessageOutput(msg, time.Local))
}
}
func (cmd *Logs) tailLogsFor(app models.Application) {
onConnect := func() {
cmd.ui.Say(T("Connected, tailing logs for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...\n",
map[string]interface{}{
"AppName": terminal.EntityNameColor(app.Name),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
}
err := cmd.logsRepo.TailLogsFor(app.Guid, onConnect, func(msg *logmessage.LogMessage) {
cmd.ui.Say("%s", LogMessageOutput(msg, time.Local))
})
if err != nil {
cmd.handleError(err)
}
}
func (cmd *Logs) handleError(err error) {
switch err.(type) {
case nil:
case *errors.InvalidSSLCert:
cmd.ui.Failed(err.Error() + T("\nTIP: use 'cf login -a API --skip-ssl-validation' or 'cf api API --skip-ssl-validation' to suppress this error"))
default:
cmd.ui.Failed(err.Error())
}
}
func LogMessageOutput(msg *logmessage.LogMessage, loc *time.Location) string {
logHeader, coloredLogHeader := ui_helpers.ExtractLogHeader(msg, loc)
logContent := ui_helpers.ExtractLogContent(msg, logHeader)
return fmt.Sprintf("%s%s", coloredLogHeader, logContent)
}