| 66 | } |
| 67 | |
| 68 | func listRun(ctx context.Context, opts *ListOptions) error { |
| 69 | cfg, err := opts.Config() |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | host, _ := cfg.Authentication().DefaultHost() |
| 74 | |
| 75 | c, err := opts.EventClient(host) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | opts.IO.StartProgressIndicator() |
| 81 | subs, err := c.ListSubscriptions(ctx, opts.Limit) |
| 82 | opts.IO.StopProgressIndicator() |
| 83 | if err != nil { |
| 84 | return fmt.Errorf("failed to list subscriptions: %w", err) |
| 85 | } |
| 86 | |
| 87 | if opts.Exporter != nil { |
| 88 | return opts.Exporter.Write(opts.IO, subs) |
| 89 | } |
| 90 | |
| 91 | if len(subs) == 0 { |
| 92 | return cmdutil.NewNoResultsError("no event subscriptions") |
| 93 | } |
| 94 | |
| 95 | isTTY := opts.IO.IsStdoutTTY() |
| 96 | if isTTY { |
| 97 | fmt.Fprintf(opts.IO.Out, "\n%s\n\n", listHeader(len(subs), opts.Limit)) |
| 98 | } |
| 99 | cs := opts.IO.ColorScheme() |
| 100 | now := opts.Now() |
| 101 | tp := tableprinter.New(opts.IO, tableprinter.WithHeader("ID", "RESOURCE", "EVENTS", "EXPIRES")) |
| 102 | for _, sub := range subs { |
| 103 | tp.AddField(sub.ID, tableprinter.WithColor(cs.Cyan)) |
| 104 | tp.AddField(sub.ResourceURL) |
| 105 | if isTTY { |
| 106 | tp.AddField(strings.Join(sub.EventTypes, ", ")) |
| 107 | tp.AddField(text.FuzzyIn(now, sub.ExpiresAt), tableprinter.WithColor(cs.Muted)) |
| 108 | } else { |
| 109 | tp.AddField(strings.Join(sub.EventTypes, ",")) |
| 110 | tp.AddField(sub.ExpiresAt.Format(time.RFC3339)) |
| 111 | } |
| 112 | tp.EndRow() |
| 113 | } |
| 114 | return tp.Render() |
| 115 | } |
| 116 | |
| 117 | // listHeader describes the listed subscriptions. The API reports no total, so a full page may not be all of them. |
| 118 | func listHeader(count, limit int) string { |