| 61 | } |
| 62 | |
| 63 | func listRun(opts *ListOptions) error { |
| 64 | if opts.Web { |
| 65 | webURL := capi.AgentsHomeURL |
| 66 | if opts.IO.IsStdoutTTY() { |
| 67 | fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(webURL)) |
| 68 | } |
| 69 | return opts.Browser.Browse(webURL) |
| 70 | } |
| 71 | |
| 72 | if opts.Limit <= 0 { |
| 73 | opts.Limit = defaultLimit |
| 74 | } |
| 75 | |
| 76 | capiClient, err := opts.CapiClient() |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | opts.IO.StartProgressIndicatorWithLabel("Fetching agent tasks...") |
| 82 | defer opts.IO.StopProgressIndicator() |
| 83 | var sessions []*capi.Session |
| 84 | ctx := context.Background() |
| 85 | |
| 86 | sessions, err = capiClient.ListLatestSessionsForViewer(ctx, opts.Limit) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | opts.IO.StopProgressIndicator() |
| 92 | |
| 93 | if len(sessions) == 0 && opts.Exporter == nil { |
| 94 | return cmdutil.NewNoResultsError("no agent tasks found") |
| 95 | } |
| 96 | |
| 97 | if opts.Exporter != nil { |
| 98 | return opts.Exporter.Write(opts.IO, sessions) |
| 99 | } |
| 100 | |
| 101 | if err := opts.IO.StartPager(); err == nil { |
| 102 | defer opts.IO.StopPager() |
| 103 | } else { |
| 104 | fmt.Fprintf(opts.IO.ErrOut, "error starting pager: %v\n", err) |
| 105 | } |
| 106 | |
| 107 | if opts.IO.IsStdoutTTY() { |
| 108 | count := len(sessions) |
| 109 | header := fmt.Sprintf("Showing %s", text.Pluralize(count, "session")) |
| 110 | fmt.Fprintf(opts.IO.Out, "%s\n\n", header) |
| 111 | } |
| 112 | |
| 113 | cs := opts.IO.ColorScheme() |
| 114 | tp := tableprinter.New(opts.IO, tableprinter.WithHeader("Session Name", "Pull Request", "Repo", "Session State", "Created")) |
| 115 | for _, s := range sessions { |
| 116 | if s.ResourceType != "pull" || s.PullRequest == nil || s.PullRequest.Repository == nil { |
| 117 | // Skip these sessions in case they happen, for now. |
| 118 | continue |
| 119 | } |
| 120 | |