github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/application/events.go (about)

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