-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
100 lines (82 loc) · 2.72 KB
/
events.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
package application
import (
"errors"
"github.com/cloudfoundry/cli/cf/api/appevents"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/flags"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
)
type Events struct {
ui terminal.UI
config coreconfig.Reader
appReq requirements.ApplicationRequirement
eventsRepo appevents.Repository
}
func init() {
commandregistry.Register(&Events{})
}
func (cmd *Events) MetaData() commandregistry.CommandMetadata {
return commandregistry.CommandMetadata{
Name: "events",
Description: T("Show recent app events"),
Usage: []string{
"CF_NAME events ",
T("APP_NAME"),
},
}
}
func (cmd *Events) Requirements(requirementsFactory requirements.Factory, c flags.FlagContext) []requirements.Requirement {
if len(c.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("events"))
}
cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0])
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
cmd.appReq,
}
return reqs
}
func (cmd *Events) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.eventsRepo = deps.RepoLocator.GetAppEventsRepository()
return cmd
}
func (cmd *Events) Execute(c flags.FlagContext) error {
app := cmd.appReq.GetApplication()
cmd.ui.Say(T("Getting events 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())}))
table := cmd.ui.Table([]string{T("time"), T("event"), T("actor"), T("description")})
events, err := cmd.eventsRepo.RecentEvents(app.GUID, 50)
if err != nil {
return errors.New(T("Failed fetching events.\n{{.APIErr}}",
map[string]interface{}{"APIErr": err.Error()}))
}
for _, event := range events {
actor := event.ActorName
if actor == "" {
actor = event.Actor
}
table.Add(
event.Timestamp.Local().Format("2006-01-02T15:04:05.00-0700"),
event.Name,
actor,
event.Description,
)
}
table.Print()
if len(events) == 0 {
cmd.ui.Say(T("No events for app {{.AppName}}",
map[string]interface{}{"AppName": terminal.EntityNameColor(app.Name)}))
return nil
}
return nil
}