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
165 lines (129 loc) · 3.36 KB
/
list.go
File metadata and controls
165 lines (129 loc) · 3.36 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
package list
import (
"fmt"
"net/http"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
const (
defaultLimit = 10
Active WorkflowState = "active"
DisabledManually WorkflowState = "disabled_manually"
)
type ListOptions struct {
IO *iostreams.IOStreams
HttpClient func() (*http.Client, error)
BaseRepo func() (ghrepo.Interface, error)
PlainOutput bool
All bool
Limit int
}
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
opts := &ListOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
}
cmd := &cobra.Command{
Use: "list",
Short: "List GitHub Actions workflows",
Args: cobra.NoArgs,
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
terminal := opts.IO.IsStdoutTTY() && opts.IO.IsStdinTTY()
opts.PlainOutput = !terminal
if opts.Limit < 1 {
return &cmdutil.FlagError{Err: fmt.Errorf("invalid limit: %v", opts.Limit)}
}
if runF != nil {
return runF(opts)
}
return listRun(opts)
},
}
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of workflows to fetch")
cmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all workflows, including disabled workflows")
return cmd
}
func listRun(opts *ListOptions) error {
repo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("could not determine base repo: %w", err)
}
httpClient, err := opts.HttpClient()
if err != nil {
return fmt.Errorf("could not create http client: %w", err)
}
client := api.NewClientFromHTTP(httpClient)
opts.IO.StartProgressIndicator()
workflows, err := getWorkflows(client, repo, opts.Limit)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("could not get workflows: %w", err)
}
if len(workflows) == 0 {
if !opts.PlainOutput {
fmt.Fprintln(opts.IO.ErrOut, "No workflows found")
}
return nil
}
tp := utils.NewTablePrinter(opts.IO)
cs := opts.IO.ColorScheme()
for _, workflow := range workflows {
if workflow.Disabled() && !opts.All {
continue
}
tp.AddField(workflow.Name, nil, cs.Bold)
tp.AddField(string(workflow.State), nil, nil)
tp.AddField(fmt.Sprintf("%d", workflow.ID), nil, cs.Cyan)
tp.EndRow()
}
return tp.Render()
}
type WorkflowState string
type Workflow struct {
Name string
ID int
State WorkflowState
}
func (w *Workflow) Disabled() bool {
return w.State != Active
}
type WorkflowsPayload struct {
Workflows []Workflow
}
func getWorkflows(client *api.Client, repo ghrepo.Interface, limit int) ([]Workflow, error) {
perPage := limit
page := 1
if limit > 100 {
perPage = 100
}
workflows := []Workflow{}
for len(workflows) < limit {
var result WorkflowsPayload
path := fmt.Sprintf("repos/%s/actions/workflows?per_page=%d&page=%d", ghrepo.FullName(repo), perPage, page)
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
if err != nil {
return nil, err
}
if len(result.Workflows) == 0 {
break
}
for _, workflow := range result.Workflows {
workflows = append(workflows, workflow)
if len(workflows) == limit {
break
}
}
if len(result.Workflows) < perPage {
break
}
page++
}
return workflows, nil
}