@@ -5,18 +5,15 @@ import (
55 "fmt"
66 "net/http"
77 "path/filepath"
8- "regexp"
98 "strings"
109
11- "github.com/AlecAivazis/survey/v2"
1210 "github.com/MakeNowJust/heredoc"
1311 "github.com/cli/cli/api"
1412 "github.com/cli/cli/internal/ghrepo"
1513 "github.com/cli/cli/pkg/cmd/workflow/shared"
1614 "github.com/cli/cli/pkg/cmdutil"
1715 "github.com/cli/cli/pkg/iostreams"
1816 "github.com/cli/cli/pkg/markdown"
19- "github.com/cli/cli/pkg/prompt"
2017 "github.com/cli/cli/utils"
2118 "github.com/spf13/cobra"
2219)
@@ -26,8 +23,8 @@ type ViewOptions struct {
2623 IO * iostreams.IOStreams
2724 BaseRepo func () (ghrepo.Interface , error )
2825
29- WorkflowID string
30- Web bool
26+ WorkflowSelector string
27+ Web bool
3128
3229 Prompt bool
3330 Raw bool
@@ -57,9 +54,9 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
5754 opts .Raw = ! opts .IO .CanPrompt ()
5855
5956 if len (args ) > 0 {
60- opts .WorkflowID = args [0 ]
57+ opts .WorkflowSelector = args [0 ]
6158 } else if ! opts .IO .CanPrompt () {
62- return & cmdutil.FlagError {Err : errors .New ("workflow ID required when not running interactively" )}
59+ return & cmdutil.FlagError {Err : errors .New ("workflow argument required when not running interactively" )}
6360 } else {
6461 opts .Prompt = true
6562 }
@@ -94,13 +91,31 @@ func runView(opts *ViewOptions) error {
9491 if err != nil {
9592 return err
9693 }
97- }
98-
99- if workflow == nil {
100- workflow , err = resolveWorkflow (opts .IO , client , repo , opts .WorkflowID )
94+ } else {
95+ workflows , err := shared .ResolveWorkflow (client , repo , opts .WorkflowSelector )
10196 if err != nil {
10297 return err
10398 }
99+ if len (workflows ) == 0 {
100+ return fmt .Errorf ("could not find any workflows named %s" , opts .WorkflowSelector )
101+ }
102+
103+ if len (workflows ) == 1 {
104+ workflow = & workflows [0 ]
105+ } else {
106+ if ! opts .IO .CanPrompt () {
107+ errMsg := "could not resolve to a unique workflow; found:"
108+ for _ , workflow := range workflows {
109+ errMsg += fmt .Sprintf (" %s" , workflow .Base ())
110+ }
111+ return errors .New (errMsg )
112+ }
113+ states := []shared.WorkflowState {shared .Active }
114+ workflow , err = shared .SelectWorkflow (workflows , "Which workflow do you mean?" , states )
115+ if err != nil {
116+ return err
117+ }
118+ }
104119 }
105120
106121 if opts .Web {
@@ -175,79 +190,6 @@ func promptWorkflows(client *api.Client, repo ghrepo.Interface) (*shared.Workflo
175190 return nil , fmt .Errorf ("could not fetch workflows for %s: %w" , ghrepo .FullName (repo ), err )
176191 }
177192
178- filtered := []shared.Workflow {}
179- candidates := []string {}
180- for _ , workflow := range workflows {
181- if ! workflow .Disabled () {
182- filtered = append (filtered , workflow )
183- candidates = append (candidates , workflow .Name )
184- }
185- }
186-
187- var selected int
188-
189- err = prompt .SurveyAskOne (& survey.Select {
190- Message : "Select a workflow" ,
191- Options : candidates ,
192- PageSize : 10 ,
193- }, & selected )
194- if err != nil {
195- return nil , err
196- }
197-
198- return & filtered [selected ], nil
199- }
200-
201- func resolveWorkflow (io * iostreams.IOStreams , client * api.Client , repo ghrepo.Interface , workflowSelector string ) (* shared.Workflow , error ) {
202- if workflowSelector == "" {
203- return nil , errors .New ("empty workflow selector" )
204- }
205-
206- idRE := regexp .MustCompile (`^\d+$` )
207-
208- if idRE .MatchString (workflowSelector ) {
209- workflow , err := getWorkflowByID (client , repo , workflowSelector )
210- if err != nil {
211- return nil , err
212- }
213- return workflow , nil
214- }
215-
216- workflows , err := getWorkflowsByName (client , repo , workflowSelector )
217- if err != nil {
218- return nil , err
219- }
220-
221- if len (workflows ) == 0 {
222- return nil , fmt .Errorf ("could not find any workflows named %s" , workflowSelector )
223- }
224-
225- if len (workflows ) == 1 {
226- return & workflows [0 ], nil
227- }
228-
229- if ! io .CanPrompt () {
230- errMsg := "could not resolve to a unique workflow; found:"
231- for _ , workflow := range workflows {
232- errMsg += fmt .Sprintf (" %s (ID: %d)" , workflow .Path , workflow .ID )
233- }
234- return nil , errors .New (errMsg )
235- }
236-
237- candidates := []string {}
238- for _ , workflow := range workflows {
239- candidates = append (candidates , fmt .Sprintf ("%s (ID: %d, path: %s)" , workflow .Name , workflow .ID , workflow .Path ))
240- }
241-
242- var selected int
243- err = prompt .SurveyAskOne (& survey.Select {
244- Message : "Which workflow do you mean?" ,
245- Options : candidates ,
246- PageSize : 10 ,
247- }, & selected )
248- if err != nil {
249- return nil , err
250- }
251-
252- return & workflows [selected ], nil
193+ states := []shared.WorkflowState {shared .Active }
194+ return shared .SelectWorkflow (workflows , "Select a workflow" , states )
253195}
0 commit comments