github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+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_metadata"
     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/codegangsta/cli"
    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 NewEvents(ui terminal.UI, config core_config.Reader, eventsRepo app_events.AppEventsRepository) (cmd *Events) {
    21  	cmd = new(Events)
    22  	cmd.ui = ui
    23  	cmd.config = config
    24  	cmd.eventsRepo = eventsRepo
    25  	return
    26  }
    27  
    28  func (cmd *Events) Metadata() command_metadata.CommandMetadata {
    29  	return command_metadata.CommandMetadata{
    30  		Name:        "events",
    31  		Description: T("Show recent app events"),
    32  		Usage:       T("CF_NAME events APP_NAME"),
    33  	}
    34  }
    35  
    36  func (cmd *Events) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
    37  	if len(c.Args()) != 1 {
    38  		cmd.ui.FailWithUsage(c)
    39  	}
    40  
    41  	cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0])
    42  
    43  	reqs = []requirements.Requirement{
    44  		requirementsFactory.NewLoginRequirement(),
    45  		requirementsFactory.NewTargetedSpaceRequirement(),
    46  		cmd.appReq,
    47  	}
    48  	return
    49  }
    50  
    51  func (cmd *Events) Run(c *cli.Context) {
    52  	app := cmd.appReq.GetApplication()
    53  
    54  	cmd.ui.Say(T("Getting events for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...\n",
    55  		map[string]interface{}{
    56  			"AppName":   terminal.EntityNameColor(app.Name),
    57  			"OrgName":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    58  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    59  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    60  
    61  	table := cmd.ui.Table([]string{T("time"), T("event"), T("actor"), T("description")})
    62  
    63  	events, apiErr := cmd.eventsRepo.RecentEvents(app.Guid, 50)
    64  	if apiErr != nil {
    65  		cmd.ui.Failed(T("Failed fetching events.\n{{.ApiErr}}",
    66  			map[string]interface{}{"ApiErr": apiErr.Error()}))
    67  		return
    68  	}
    69  
    70  	for _, event := range events {
    71  		table.Add(
    72  			event.Timestamp.Local().Format("2006-01-02T15:04:05.00-0700"),
    73  			event.Name,
    74  			event.ActorName,
    75  			event.Description,
    76  		)
    77  	}
    78  
    79  	table.Print()
    80  
    81  	if len(events) == 0 {
    82  		cmd.ui.Say(T("No events for app {{.AppName}}",
    83  			map[string]interface{}{"AppName": terminal.EntityNameColor(app.Name)}))
    84  		return
    85  	}
    86  }