forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.go
More file actions
269 lines (225 loc) · 6.81 KB
/
view.go
File metadata and controls
269 lines (225 loc) · 6.81 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package view
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
runShared "github.com/cli/cli/v2/pkg/cmd/run/shared"
"github.com/cli/cli/v2/pkg/cmd/workflow/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/markdown"
"github.com/cli/cli/v2/utils"
"github.com/spf13/cobra"
)
type ViewOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
Browser cmdutil.Browser
Selector string
Ref string
Web bool
Prompt bool
Raw bool
YAML bool
}
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
opts := &ViewOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Browser: f.Browser,
}
cmd := &cobra.Command{
Use: "view [<workflow-id> | <workflow-name> | <filename>]",
Short: "View the summary of a workflow",
Args: cobra.MaximumNArgs(1),
Example: heredoc.Doc(`
# Interactively select a workflow to view
$ gh workflow view
# View a specific workflow
$ gh workflow view 0451
`),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
opts.Raw = !opts.IO.IsStdoutTTY()
if len(args) > 0 {
opts.Selector = args[0]
} else if !opts.IO.CanPrompt() {
return &cmdutil.FlagError{Err: errors.New("workflow argument required when not running interactively")}
} else {
opts.Prompt = true
}
if !opts.YAML && opts.Ref != "" {
return &cmdutil.FlagError{Err: errors.New("`--yaml` required when specifying `--ref`")}
}
if runF != nil {
return runF(opts)
}
return runView(opts)
},
}
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open workflow in the browser")
cmd.Flags().BoolVarP(&opts.YAML, "yaml", "y", false, "View the workflow yaml file")
cmd.Flags().StringVarP(&opts.Ref, "ref", "r", "", "The branch or tag name which contains the version of the workflow file you'd like to view")
return cmd
}
func runView(opts *ViewOptions) error {
c, err := opts.HttpClient()
if err != nil {
return fmt.Errorf("could not build http client: %w", err)
}
client := api.NewClientFromHTTP(c)
repo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("could not determine base repo: %w", err)
}
var workflow *shared.Workflow
states := []shared.WorkflowState{shared.Active}
workflow, err = shared.ResolveWorkflow(opts.IO, client, repo, opts.Prompt, opts.Selector, states)
if err != nil {
return err
}
if opts.Web {
var url string
if opts.YAML {
ref := opts.Ref
if ref == "" {
opts.IO.StartProgressIndicator()
ref, err = api.RepoDefaultBranch(client, repo)
opts.IO.StopProgressIndicator()
if err != nil {
return err
}
}
url = ghrepo.GenerateRepoURL(repo, "blob/%s/%s", ref, workflow.Path)
} else {
url = ghrepo.GenerateRepoURL(repo, "actions/workflows/%s", workflow.Base())
}
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(url))
}
return opts.Browser.Browse(url)
}
if opts.YAML {
err = viewWorkflowContent(opts, client, workflow)
} else {
err = viewWorkflowInfo(opts, client, workflow)
}
if err != nil {
return err
}
return nil
}
func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared.Workflow) error {
repo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("could not determine base repo: %w", err)
}
opts.IO.StartProgressIndicator()
yamlBytes, err := shared.GetWorkflowContent(client, repo, *workflow, opts.Ref)
opts.IO.StopProgressIndicator()
if err != nil {
if s, ok := err.(api.HTTPError); ok && s.StatusCode == 404 {
if opts.Ref != "" {
return fmt.Errorf("could not find workflow file %s on %s, try specifying a different ref", workflow.Base(), opts.Ref)
}
return fmt.Errorf("could not find workflow file %s, try specifying a branch or tag using `--ref`", workflow.Base())
}
return fmt.Errorf("could not get workflow file content: %w", err)
}
yaml := string(yamlBytes)
theme := opts.IO.DetectTerminalTheme()
markdownStyle := markdown.GetStyle(theme)
if err := opts.IO.StartPager(); err != nil {
fmt.Fprintf(opts.IO.ErrOut, "starting pager failed: %v\n", err)
}
defer opts.IO.StopPager()
if !opts.Raw {
cs := opts.IO.ColorScheme()
out := opts.IO.Out
fileName := workflow.Base()
fmt.Fprintf(out, "%s - %s\n", cs.Bold(workflow.Name), cs.Gray(fileName))
fmt.Fprintf(out, "ID: %s", cs.Cyanf("%d", workflow.ID))
codeBlock := fmt.Sprintf("```yaml\n%s\n```", yaml)
rendered, err := markdown.RenderWithOpts(codeBlock, markdownStyle,
markdown.RenderOpts{
markdown.WithoutIndentation(),
markdown.WithoutWrap(),
})
if err != nil {
return err
}
_, err = fmt.Fprint(opts.IO.Out, rendered)
return err
}
if _, err := fmt.Fprint(opts.IO.Out, yaml); err != nil {
return err
}
if !strings.HasSuffix(yaml, "\n") {
_, err := fmt.Fprint(opts.IO.Out, "\n")
return err
}
return nil
}
func viewWorkflowInfo(opts *ViewOptions, client *api.Client, workflow *shared.Workflow) error {
repo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("could not determine base repo: %w", err)
}
opts.IO.StartProgressIndicator()
wr, err := getWorkflowRuns(client, repo, workflow)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get runs: %w", err)
}
out := opts.IO.Out
cs := opts.IO.ColorScheme()
tp := utils.NewTablePrinter(opts.IO)
// Header
filename := workflow.Base()
fmt.Fprintf(out, "%s - %s\n", cs.Bold(workflow.Name), cs.Cyan(filename))
fmt.Fprintf(out, "ID: %s\n\n", cs.Cyanf("%d", workflow.ID))
// Runs
fmt.Fprintf(out, "Total runs %d\n", wr.Total)
if wr.Total != 0 {
fmt.Fprintln(out, "Recent runs")
}
for _, run := range wr.Runs {
if opts.Raw {
tp.AddField(string(run.Status), nil, nil)
tp.AddField(string(run.Conclusion), nil, nil)
} else {
symbol, symbolColor := runShared.Symbol(cs, run.Status, run.Conclusion)
tp.AddField(symbol, nil, symbolColor)
}
tp.AddField(run.CommitMsg(), nil, cs.Bold)
tp.AddField(run.Name, nil, nil)
tp.AddField(run.HeadBranch, nil, cs.Bold)
tp.AddField(string(run.Event), nil, nil)
if opts.Raw {
elapsed := run.UpdatedAt.Sub(run.CreatedAt)
if elapsed < 0 {
elapsed = 0
}
tp.AddField(elapsed.String(), nil, nil)
}
tp.AddField(fmt.Sprintf("%d", run.ID), nil, cs.Cyan)
tp.EndRow()
}
err = tp.Render()
if err != nil {
return err
}
fmt.Fprintln(out)
// Footer
if wr.Total != 0 {
fmt.Fprintf(out, "To see more runs for this workflow, try: gh run list --workflow %s\n", filename)
}
fmt.Fprintf(out, "To see the YAML for this workflow, try: gh workflow view %s --yaml\n", filename)
return nil
}