forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
197 lines (165 loc) · 5.28 KB
/
Copy pathlist.go
File metadata and controls
197 lines (165 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package codespace
import (
"context"
"fmt"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/codespaces/api"
"github.com/cli/cli/v2/internal/tableprinter"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/spf13/cobra"
)
type listOptions struct {
limit int
repo string
orgName string
userName string
useWeb bool
}
func newListCmd(app *App) *cobra.Command {
opts := &listOptions{}
var exporter cmdutil.Exporter
listCmd := &cobra.Command{
Use: "list",
Short: "List codespaces",
Long: heredoc.Doc(`
List codespaces of the authenticated user.
Alternatively, organization administrators may list all codespaces billed to the organization.
`),
Aliases: []string{"ls"},
Args: noArgsConstraint,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmdutil.MutuallyExclusive(
"using `--org` or `--user` with `--repo` is not allowed",
opts.repo != "",
opts.orgName != "" || opts.userName != "",
); err != nil {
return err
}
if err := cmdutil.MutuallyExclusive(
"using `--web` with `--org` or `--user` is not supported, please use with `--repo` instead",
opts.useWeb,
opts.orgName != "" || opts.userName != "",
); err != nil {
return err
}
if opts.limit < 1 {
return cmdutil.FlagErrorf("invalid limit: %v", opts.limit)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return app.List(cmd.Context(), opts, exporter)
},
}
listCmd.Flags().IntVarP(&opts.limit, "limit", "L", 30, "Maximum number of codespaces to list")
listCmd.Flags().StringVarP(&opts.repo, "repo", "R", "", "Repository name with owner: user/repo")
if err := addDeprecatedRepoShorthand(listCmd, &opts.repo); err != nil {
fmt.Fprintf(app.io.ErrOut, "%v\n", err)
}
listCmd.Flags().StringVarP(&opts.orgName, "org", "o", "", "The `login` handle of the organization to list codespaces for (admin-only)")
listCmd.Flags().StringVarP(&opts.userName, "user", "u", "", "The `username` to list codespaces for (used with --org)")
cmdutil.AddJSONFlags(listCmd, &exporter, api.ListCodespaceFields)
listCmd.Flags().BoolVarP(&opts.useWeb, "web", "w", false, "List codespaces in the web browser, cannot be used with --user or --org")
return listCmd
}
func (a *App) List(ctx context.Context, opts *listOptions, exporter cmdutil.Exporter) error {
if opts.useWeb && opts.repo == "" {
return a.browser.Browse(fmt.Sprintf("%s/codespaces", a.apiClient.ServerURL()))
}
var codespaces []*api.Codespace
err := a.RunWithProgress("Fetching codespaces", func() (err error) {
codespaces, err = a.apiClient.ListCodespaces(ctx, api.ListCodespacesOptions{Limit: opts.limit, RepoName: opts.repo, OrgName: opts.orgName, UserName: opts.userName})
return
})
if err != nil {
return fmt.Errorf("error getting codespaces: %w", err)
}
hasNonProdVSCSTarget := false
for _, apiCodespace := range codespaces {
if apiCodespace.VSCSTarget != "" && apiCodespace.VSCSTarget != api.VSCSTargetProduction {
hasNonProdVSCSTarget = true
break
}
}
if err := a.io.StartPager(); err != nil {
a.errLogger.Printf("error starting pager: %v", err)
}
defer a.io.StopPager()
if exporter != nil {
return exporter.Write(a.io, codespaces)
}
if len(codespaces) == 0 {
return cmdutil.NewNoResultsError("no codespaces found")
}
if opts.useWeb && codespaces[0].Repository.ID > 0 {
return a.browser.Browse(fmt.Sprintf("%s/codespaces?repository_id=%d", a.apiClient.ServerURL(), codespaces[0].Repository.ID))
}
headers := []string{
"NAME",
"DISPLAY NAME",
}
if opts.orgName != "" {
headers = append(headers, "OWNER")
}
headers = append(headers,
"REPOSITORY",
"BRANCH",
"STATE",
"CREATED AT",
)
if hasNonProdVSCSTarget {
headers = append(headers, "VSCS TARGET")
}
tp := tableprinter.New(a.io, tableprinter.WithHeader(headers...))
cs := a.io.ColorScheme()
for _, apiCodespace := range codespaces {
c := codespace{apiCodespace}
var stateColor func(string) string
switch c.State {
case api.CodespaceStateStarting:
stateColor = cs.Yellow
case api.CodespaceStateAvailable:
stateColor = cs.Green
}
formattedName := formatNameForVSCSTarget(c.Name, c.VSCSTarget)
var nameColor func(string) string
switch c.PendingOperation {
case false:
nameColor = cs.Yellow
case true:
nameColor = cs.Gray
}
tp.AddField(formattedName, tableprinter.WithColor(nameColor))
tp.AddField(c.DisplayName)
if opts.orgName != "" {
tp.AddField(c.Owner.Login)
}
tp.AddField(c.Repository.FullName)
tp.AddField(c.branchWithGitStatus(), tableprinter.WithColor(cs.Cyan))
if c.PendingOperation {
tp.AddField(c.PendingOperationDisabledReason, tableprinter.WithColor(nameColor))
} else {
tp.AddField(c.State, tableprinter.WithColor(stateColor))
}
ct, err := time.Parse(time.RFC3339, c.CreatedAt)
if err != nil {
return fmt.Errorf("error parsing date %q: %w", c.CreatedAt, err)
}
tp.AddTimeField(time.Now(), ct, cs.Gray)
if hasNonProdVSCSTarget {
tp.AddField(c.VSCSTarget)
}
tp.EndRow()
}
return tp.Render()
}
func formatNameForVSCSTarget(name, vscsTarget string) string {
if vscsTarget == api.VSCSTargetDevelopment || vscsTarget == api.VSCSTargetLocal {
return fmt.Sprintf("%s 🚧", name)
}
if vscsTarget == api.VSCSTargetPPE {
return fmt.Sprintf("%s ✨", name)
}
return name
}