-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
128 lines (108 loc) · 3.77 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
package application
import (
"fmt"
"time"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
"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/noaa/events"
"github.com/codegangsta/cli"
)
type Logs struct {
ui terminal.UI
config core_config.Reader
noaaRepo api.LogsNoaaRepository
appReq requirements.ApplicationRequirement
}
func NewLogs(ui terminal.UI, config core_config.Reader, noaaRepo api.LogsNoaaRepository) (cmd *Logs) {
cmd = new(Logs)
cmd.ui = ui
cmd.config = config
cmd.noaaRepo = noaaRepo
return
}
func (cmd *Logs) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "logs",
Description: T("Tail or show recent logs for an app"),
Usage: T("CF_NAME logs APP_NAME"),
Flags: []cli.Flag{
cli.BoolFlag{Name: "recent", Usage: T("Dump recent logs instead of tailing")},
},
}
}
func (cmd *Logs) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c)
}
if cmd.appReq == nil {
cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0])
} else {
cmd.appReq.SetApplicationName(c.Args()[0])
}
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
cmd.appReq,
}
return
}
func (cmd *Logs) Run(c *cli.Context) {
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.noaaRepo.RecentLogsFor(app.Guid)
if err != nil {
cmd.handleError(err)
}
for _, msg := range messages {
cmd.ui.Say("%s", LogNoaaMessageOutput(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.noaaRepo.TailNoaaLogsFor(app.Guid, onConnect, func(msg *events.LogMessage) {
cmd.ui.Say("%s", LogNoaaMessageOutput(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 LogNoaaMessageOutput(msg *events.LogMessage, loc *time.Location) string {
logHeader, coloredLogHeader := ui_helpers.ExtractNoaaLogHeader(msg, loc)
logContent := ui_helpers.ExtractNoaaLogContent(msg, logHeader)
return fmt.Sprintf("%s%s", coloredLogHeader, logContent)
}