| 77 | } |
| 78 | |
| 79 | func (a *App) List(ctx context.Context, opts *listOptions, exporter cmdutil.Exporter) error { |
| 80 | if opts.useWeb && opts.repo == "" { |
| 81 | return a.browser.Browse(fmt.Sprintf("%s/codespaces", a.apiClient.ServerURL())) |
| 82 | } |
| 83 | |
| 84 | var codespaces []*api.Codespace |
| 85 | err := a.RunWithProgress("Fetching codespaces", func() (err error) { |
| 86 | codespaces, err = a.apiClient.ListCodespaces(ctx, api.ListCodespacesOptions{Limit: opts.limit, RepoName: opts.repo, OrgName: opts.orgName, UserName: opts.userName}) |
| 87 | return |
| 88 | }) |
| 89 | if err != nil { |
| 90 | return fmt.Errorf("error getting codespaces: %w", err) |
| 91 | } |
| 92 | |
| 93 | hasNonProdVSCSTarget := false |
| 94 | for _, apiCodespace := range codespaces { |
| 95 | if apiCodespace.VSCSTarget != "" && apiCodespace.VSCSTarget != api.VSCSTargetProduction { |
| 96 | hasNonProdVSCSTarget = true |
| 97 | break |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if err := a.io.StartPager(); err != nil { |
| 102 | a.errLogger.Printf("error starting pager: %v", err) |
| 103 | } |
| 104 | defer a.io.StopPager() |
| 105 | |
| 106 | if exporter != nil { |
| 107 | return exporter.Write(a.io, codespaces) |
| 108 | } |
| 109 | |
| 110 | if len(codespaces) == 0 { |
| 111 | return cmdutil.NewNoResultsError("no codespaces found") |
| 112 | } |
| 113 | |
| 114 | if opts.useWeb && codespaces[0].Repository.ID > 0 { |
| 115 | return a.browser.Browse(fmt.Sprintf("%s/codespaces?repository_id=%d", a.apiClient.ServerURL(), codespaces[0].Repository.ID)) |
| 116 | } |
| 117 | |
| 118 | headers := []string{ |
| 119 | "NAME", |
| 120 | "DISPLAY NAME", |
| 121 | } |
| 122 | if opts.orgName != "" { |
| 123 | headers = append(headers, "OWNER") |
| 124 | } |
| 125 | headers = append(headers, |
| 126 | "REPOSITORY", |
| 127 | "BRANCH", |
| 128 | "STATE", |
| 129 | "CREATED AT", |
| 130 | ) |
| 131 | if hasNonProdVSCSTarget { |
| 132 | headers = append(headers, "VSCS TARGET") |
| 133 | } |
| 134 | tp := tableprinter.New(a.io, tableprinter.WithHeader(headers...)) |
| 135 | |
| 136 | cs := a.io.ColorScheme() |