44 "errors"
55 "fmt"
66 "net/http"
7- "path/filepath"
87 "strings"
98
109 "github.com/MakeNowJust/heredoc"
@@ -41,7 +40,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
4140 }
4241
4342 cmd := & cobra.Command {
44- Use : "view [<run -id> | <file name>]" ,
43+ Use : "view [<workflow -id> | <workflow name > | <file name>]" ,
4544 Short : "View the summary of a workflow" ,
4645 Args : cobra .MaximumNArgs (1 ),
4746 Hidden : true ,
@@ -56,7 +55,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
5655 // support `-R, --repo` override
5756 opts .BaseRepo = f .BaseRepo
5857
59- opts .Raw = ! opts .IO .CanPrompt ()
58+ opts .Raw = ! opts .IO .IsStdoutTTY ()
6059
6160 if len (args ) > 0 {
6261 opts .Selector = args [0 ]
@@ -66,6 +65,10 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
6665 opts .Prompt = true
6766 }
6867
68+ if ! opts .YAML && opts .Ref != "" {
69+ return & cmdutil.FlagError {Err : errors .New ("`--yaml` required when specifying `--ref`" )}
70+ }
71+
6972 if runF != nil {
7073 return runF (opts )
7174 }
@@ -101,7 +104,6 @@ func runView(opts *ViewOptions) error {
101104
102105 if opts .Web {
103106 var url string
104- hostname := repo .RepoHost ()
105107 if opts .YAML {
106108 ref := opts .Ref
107109 if ref == "" {
@@ -112,10 +114,9 @@ func runView(opts *ViewOptions) error {
112114 return err
113115 }
114116 }
115- url = fmt . Sprintf ( "https://%s/%s/ blob/%s/%s", hostname , ghrepo . FullName ( repo ) , ref , workflow .Path )
117+ url = ghrepo . GenerateRepoURL ( repo , " blob/%s/%s" , ref , workflow .Path )
116118 } else {
117- baseName := filepath .Base (workflow .Path )
118- url = fmt .Sprintf ("https://%s/%s/actions/workflows/%s" , hostname , ghrepo .FullName (repo ), baseName )
119+ url = ghrepo .GenerateRepoURL (repo , "actions/workflows/%s" , workflow .Base ())
119120 }
120121 if opts .IO .IsStdoutTTY () {
121122 fmt .Fprintf (opts .IO .Out , "Opening %s in your browser.\n " , utils .DisplayURL (url ))
@@ -145,14 +146,11 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
145146 yaml , err := getWorkflowContent (client , repo , opts .Ref , workflow )
146147 opts .IO .StopProgressIndicator ()
147148 if err != nil {
148- if s , ok := err .(api.HTTPError ); ok {
149- if s .StatusCode == 404 {
150- base := filepath .Base (workflow .Path )
151- if opts .Ref != "" {
152- return fmt .Errorf ("could not find workflow file %s on %s, try specifying a different ref" , base , opts .Ref )
153- }
154- return fmt .Errorf ("could not find workflow file %s, try specifying a branch or tag using --ref" , base )
149+ if s , ok := err .(api.HTTPError ); ok && s .StatusCode == 404 {
150+ if opts .Ref != "" {
151+ return fmt .Errorf ("could not find workflow file %s on %s, try specifying a different ref" , workflow .Base (), opts .Ref )
155152 }
153+ return fmt .Errorf ("could not find workflow file %s, try specifying a branch or tag using `--ref`" , workflow .Base ())
156154 }
157155 return fmt .Errorf ("could not get workflow file content: %w" , err )
158156 }
@@ -168,7 +166,7 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
168166 cs := opts .IO .ColorScheme ()
169167 out := opts .IO .Out
170168
171- fileName := filepath .Base (workflow . Path )
169+ fileName := workflow .Base ()
172170 fmt .Fprintf (out , "%s - %s\n " , cs .Bold (workflow .Name ), cs .Gray (fileName ))
173171 fmt .Fprintf (out , "ID: %s" , cs .Cyanf ("%d" , workflow .ID ))
174172
@@ -215,7 +213,7 @@ func viewWorkflowInfo(opts *ViewOptions, client *api.Client, workflow *shared.Wo
215213 tp := utils .NewTablePrinter (opts .IO )
216214
217215 // Header
218- filename := filepath .Base (workflow . Path )
216+ filename := workflow .Base ()
219217 fmt .Fprintf (out , "%s - %s\n " , cs .Bold (workflow .Name ), cs .Cyan (filename ))
220218 fmt .Fprintf (out , "ID: %s\n \n " , cs .Cyanf ("%d" , workflow .ID ))
221219
0 commit comments