-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.go
129 lines (108 loc) · 3.61 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 (
"time"
"github.com/cloudfoundry/cli/cf/api/logs"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"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/flags"
)
type Logs struct {
ui terminal.UI
logsRepo logs.LogsRepository
config coreconfig.Reader
appReq requirements.ApplicationRequirement
}
func init() {
commandregistry.Register(&Logs{})
}
func (cmd *Logs) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["recent"] = &flags.BoolFlag{Name: "recent", Usage: T("Dump recent logs instead of tailing")}
return commandregistry.CommandMetadata{
Name: "logs",
Description: T("Tail or show recent logs for an app"),
Usage: []string{
T("CF_NAME logs APP_NAME"),
},
Flags: fs,
}
}
func (cmd *Logs) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) []requirements.Requirement {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("logs"))
}
cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0])
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
cmd.appReq,
}
return reqs
}
func (cmd *Logs) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.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", msg.ToLog(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())}))
}
c := make(chan logs.Loggable)
e := make(chan error)
go cmd.logsRepo.TailLogsFor(app.GUID, onConnect, c, e)
for {
select {
case msg, ok := <-c:
if !ok {
return
}
cmd.ui.Say("%s", msg.ToLog(time.Local))
case err := <-e:
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())
}
}