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
195 lines (166 loc) · 4.76 KB
/
view.go
File metadata and controls
195 lines (166 loc) · 4.76 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
package view
import (
"errors"
"fmt"
"net/http"
"path/filepath"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/workflow/shared"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/pkg/markdown"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
type ViewOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
WorkflowSelector string
Web bool
Prompt bool
Raw bool
}
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
opts := &ViewOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
}
cmd := &cobra.Command{
Use: "view [<run-id>]",
Short: "View a summary of a workflow run",
Args: cobra.MaximumNArgs(1),
Hidden: true,
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.CanPrompt()
if len(args) > 0 {
opts.WorkflowSelector = 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 runF != nil {
return runF(opts)
}
return runView(opts)
},
}
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open workflow in the browser")
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
if opts.Prompt {
workflow, err = promptWorkflows(client, repo)
if err != nil {
return err
}
} else {
workflows, err := shared.ResolveWorkflow(client, repo, opts.WorkflowSelector)
if err != nil {
return err
}
if len(workflows) == 0 {
return fmt.Errorf("could not find any workflows named %s", opts.WorkflowSelector)
}
if len(workflows) == 1 {
workflow = &workflows[0]
} else {
if !opts.IO.CanPrompt() {
errMsg := "could not resolve to a unique workflow; found:"
for _, workflow := range workflows {
errMsg += fmt.Sprintf(" %s", workflow.Base())
}
return errors.New(errMsg)
}
states := []shared.WorkflowState{shared.Active}
workflow, err = shared.SelectWorkflow(workflows, "Which workflow do you mean?", states)
if err != nil {
return err
}
}
}
if opts.Web {
hostname := repo.RepoHost()
baseName := filepath.Base(workflow.Path)
workflowURL := fmt.Sprintf("https://%s/%s/actions/workflows/%s",
hostname,
ghrepo.FullName(repo),
baseName)
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(workflowURL))
}
return utils.OpenInBrowser(workflowURL)
}
yaml, err := getWorkflowContent(client, repo, workflow)
if err != nil {
return fmt.Errorf("could not get workflow file content: %w", err)
}
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
fmt.Fprintln(out, cs.Bold(workflow.Name))
fmt.Fprintf(out, "%s ID: %s", cs.Gray(workflow.Path), 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 promptWorkflows(client *api.Client, repo ghrepo.Interface) (*shared.Workflow, error) {
workflows, err := shared.GetWorkflows(client, repo, 0)
if len(workflows) == 0 {
err = errors.New("no workflows are enabled")
}
if err != nil {
var httpErr api.HTTPError
if errors.As(err, &httpErr) && httpErr.StatusCode == 404 {
err = errors.New("no workflows are enabled")
}
return nil, fmt.Errorf("could not fetch workflows for %s: %w", ghrepo.FullName(repo), err)
}
states := []shared.WorkflowState{shared.Active}
return shared.SelectWorkflow(workflows, "Select a workflow", states)
}