github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/application/events.go (about)

     1  package application
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/app_events"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	. "github.com/cloudfoundry/cli/cf/i18n"
     8  	"github.com/cloudfoundry/cli/cf/requirements"
     9  	"github.com/cloudfoundry/cli/cf/terminal"
    10  	"github.com/cloudfoundry/cli/flags"
    11  )
    12  
    13  type Events struct {
    14  	ui         terminal.UI
    15  	config     core_config.Reader
    16  	appReq     requirements.ApplicationRequirement
    17  	eventsRepo app_events.AppEventsRepository
    18  }
    19  
    20  func init() {
    21  	command_registry.Register(&Events{})
    22  }
    23  
    24  func (cmd *Events) MetaData() command_registry.CommandMetadata {
    25  	return command_registry.CommandMetadata{
    26  		Name:        "events",
    27  		Description: T("Show recent app events"),
    28  		Usage:       T("CF_NAME events APP_NAME"),
    29  	}
    30  }
    31  
    32  func (cmd *Events) Requirements(requirementsFactory requirements.Factory, c flags.FlagContext) (reqs []requirements.Requirement, err error) {
    33  	if len(c.Args()) != 1 {
    34  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("events"))
    35  	}
    36  
    37  	cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0])
    38  
    39  	reqs = []requirements.Requirement{
    40  		requirementsFactory.NewLoginRequirement(),
    41  		requirementsFactory.NewTargetedSpaceRequirement(),
    42  		cmd.appReq,
    43  	}
    44  	return
    45  }
    46  
    47  func (cmd *Events) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    48  	cmd.ui = deps.Ui
    49  	cmd.config = deps.Config
    50  	cmd.eventsRepo = deps.RepoLocator.GetAppEventsRepository()
    51  	return cmd
    52  }
    53  
    54  func (cmd *Events) Execute(c flags.FlagContext) {
    55  	app := cmd.appReq.GetApplication()
    56  
    57  	cmd.ui.Say(T("Getting events for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...\n",
    58  		map[string]interface{}{
    59  			"AppName":   terminal.EntityNameColor(app.Name),
    60  			"OrgName":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    61  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    62  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    63  
    64  	table := cmd.ui.Table([]string{T("time"), T("event"), T("actor"), T("description")})
    65  
    66  	events, apiErr := cmd.eventsRepo.RecentEvents(app.Guid, 50)
    67  	if apiErr != nil {
    68  		cmd.ui.Failed(T("Failed fetching events.\n{{.ApiErr}}",
    69  			map[string]interface{}{"ApiErr": apiErr.Error()}))
    70  		return
    71  	}
    72  
    73  	for _, event := range events {
    74  		table.Add(
    75  			event.Timestamp.Local().Format("2006-01-02T15:04:05.00-0700"),
    76  			event.Name,
    77  			event.ActorName,
    78  			event.Description,
    79  		)
    80  	}
    81  
    82  	table.Print()
    83  
    84  	if len(events) == 0 {
    85  		cmd.ui.Say(T("No events for app {{.AppName}}",
    86  			map[string]interface{}{"AppName": terminal.EntityNameColor(app.Name)}))
    87  		return
    88  	}
    89  }