func (cmd *events) printEvents(ctx context.Context, obj *types.ManagedObjectReference, page []types.BaseEvent, m *event.Manager) error { event.Sort(page) if obj != nil { // print the object reference fmt.Fprintf(os.Stdout, "\n==> %s <==\n", obj.String()) } for _, e := range page { cat, err := m.EventCategory(ctx, e) if err != nil { return err } event := e.GetEvent() msg := strings.TrimSpace(event.FullFormattedMessage) // if this is a TaskEvent gather a little more information if t, ok := e.(*types.TaskEvent); ok { // some tasks won't have this information, so just use the event message if t.Info.Entity != nil { msg = fmt.Sprintf("%s (target=%s %s)", msg, t.Info.Entity.Type, t.Info.EntityName) } } fmt.Fprintf(os.Stdout, "[%s] [%s] %s\n", event.CreatedTime.Local().Format(time.ANSIC), cat, msg) } return nil }
func (cmd *events) printEvents(ctx context.Context, page []types.BaseEvent, m *event.Manager) error { event.Sort(page) for _, e := range page { cat, err := m.EventCategory(ctx, e) if err != nil { return err } event := e.GetEvent() msg := strings.TrimSpace(event.FullFormattedMessage) if t, ok := e.(*types.TaskEvent); ok { msg = fmt.Sprintf("%s (target=%s %s)", msg, t.Info.Entity.Type, t.Info.EntityName) } fmt.Fprintf(os.Stdout, "[%s] [%s] %s\n", event.CreatedTime.Local().Format(time.ANSIC), cat, msg) } return nil }
func (cmd *events) Run(f *flag.FlagSet) error { ctx := context.TODO() c, err := cmd.Client() if err != nil { return err } m := event.NewManager(c) objs, err := cmd.ManagedObjects(ctx, f.Args()) if err != nil { return err } var events []types.BaseEvent for _, o := range objs { filter := types.EventFilterSpec{ Entity: &types.EventFilterSpecByEntity{ Entity: o, Recursion: types.EventFilterSpecRecursionOptionAll, }, } collector, err := m.CreateCollectorForEvents(ctx, filter) if err != nil { return fmt.Errorf("[%#v] %s", o, err) } defer collector.Destroy(ctx) err = collector.SetPageSize(ctx, cmd.Max) if err != nil { return err } page, err := collector.LatestPage(ctx) if err != nil { return err } events = append(events, page...) } event.Sort(events) for _, e := range events { cat, err := m.EventCategory(ctx, e) if err != nil { return err } event := e.GetEvent() msg := strings.TrimSpace(event.FullFormattedMessage) if t, ok := e.(*types.TaskEvent); ok { msg = fmt.Sprintf("%s (target=%s %s)", msg, t.Info.Entity.Type, t.Info.EntityName) } fmt.Fprintf(os.Stdout, "[%s] [%s] %s\n", event.CreatedTime.Local().Format(time.ANSIC), cat, msg) } return nil }
func (cmd *events) Run(f *flag.FlagSet) error { ctx := context.TODO() finder, err := cmd.Finder() if err != nil { return err } c, err := cmd.Client() if err != nil { return err } m := event.NewManager(c) var objs []list.Element args := f.Args() if len(args) == 0 { args = []string{"."} } for _, arg := range args { es, err := finder.ManagedObjectList(ctx, arg) if err != nil { return err } objs = append(objs, es...) } var events []types.BaseEvent for _, o := range objs { filter := types.EventFilterSpec{ Entity: &types.EventFilterSpecByEntity{ Entity: o.Object.Reference(), Recursion: types.EventFilterSpecRecursionOptionAll, }, } collector, err := m.CreateCollectorForEvents(ctx, filter) if err != nil { return fmt.Errorf("[%s] %s", o.Path, err) } defer collector.Destroy(ctx) err = collector.SetPageSize(ctx, cmd.Max) if err != nil { return err } page, err := collector.LatestPage(ctx) if err != nil { return err } events = append(events, page...) } event.Sort(events) tw := tabwriter.NewWriter(os.Stdout, 3, 0, 2, ' ', 0) for _, e := range events { cat, err := m.EventCategory(ctx, e) if err != nil { return err } event := e.GetEvent() msg := strings.TrimSpace(event.FullFormattedMessage) if t, ok := e.(*types.TaskEvent); ok { msg = fmt.Sprintf("%s (target=%s %s)", msg, t.Info.Entity.Type, t.Info.EntityName) } fmt.Fprintf(tw, "[%s]\t[%s]\t%s\n", event.CreatedTime.Local().Format(time.ANSIC), cat, msg) } return tw.Flush() }