diff --git a/pkg/cmd/workflow/view/view.go b/pkg/cmd/workflow/view/view.go index f32633aaf..dbcabcb81 100644 --- a/pkg/cmd/workflow/view/view.go +++ b/pkg/cmd/workflow/view/view.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net/http" - "path/filepath" "strings" "github.com/MakeNowJust/heredoc" @@ -41,7 +40,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman } cmd := &cobra.Command{ - Use: "view [ | ]", + Use: "view [ | | ]", Short: "View the summary of a workflow", Args: cobra.MaximumNArgs(1), Hidden: true, @@ -56,7 +55,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman // support `-R, --repo` override opts.BaseRepo = f.BaseRepo - opts.Raw = !opts.IO.CanPrompt() + opts.Raw = !opts.IO.IsStdoutTTY() if len(args) > 0 { opts.Selector = args[0] @@ -66,6 +65,10 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman 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) } @@ -101,7 +104,6 @@ func runView(opts *ViewOptions) error { if opts.Web { var url string - hostname := repo.RepoHost() if opts.YAML { ref := opts.Ref if ref == "" { @@ -112,10 +114,9 @@ func runView(opts *ViewOptions) error { return err } } - url = fmt.Sprintf("https://%s/%s/blob/%s/%s", hostname, ghrepo.FullName(repo), ref, workflow.Path) + url = ghrepo.GenerateRepoURL(repo, "blob/%s/%s", ref, workflow.Path) } else { - baseName := filepath.Base(workflow.Path) - url = fmt.Sprintf("https://%s/%s/actions/workflows/%s", hostname, ghrepo.FullName(repo), baseName) + 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)) @@ -145,14 +146,11 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared yaml, err := getWorkflowContent(client, repo, opts.Ref, workflow) opts.IO.StopProgressIndicator() if err != nil { - if s, ok := err.(api.HTTPError); ok { - if s.StatusCode == 404 { - base := filepath.Base(workflow.Path) - if opts.Ref != "" { - return fmt.Errorf("could not find workflow file %s on %s, try specifying a different ref", base, opts.Ref) - } - return fmt.Errorf("could not find workflow file %s, try specifying a branch or tag using --ref", base) + 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) } @@ -168,7 +166,7 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared cs := opts.IO.ColorScheme() out := opts.IO.Out - fileName := filepath.Base(workflow.Path) + 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)) @@ -215,7 +213,7 @@ func viewWorkflowInfo(opts *ViewOptions, client *api.Client, workflow *shared.Wo tp := utils.NewTablePrinter(opts.IO) // Header - filename := filepath.Base(workflow.Path) + 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)) diff --git a/pkg/cmd/workflow/view/view_test.go b/pkg/cmd/workflow/view/view_test.go index 434cf0c83..005b5c1f2 100644 --- a/pkg/cmd/workflow/view/view_test.go +++ b/pkg/cmd/workflow/view/view_test.go @@ -90,19 +90,32 @@ func TestNewCmdView(t *testing.T) { }, }, { - name: "ref tty", - cli: "--ref 456", + name: "ref tty", + cli: "--ref 456", + tty: true, + wantsErr: true, + }, + { + name: "ref nontty", + cli: "123 -r 456", + wantsErr: true, + }, + { + name: "yaml ref tty", + cli: "--yaml --ref 456", tty: true, wants: ViewOptions{ Prompt: true, + YAML: true, Ref: "456", }, }, { - name: "ref nontty", - cli: "123 -r 456", + name: "yaml ref nontty", + cli: "123 -y -r 456", wants: ViewOptions{ Raw: true, + YAML: true, Ref: "456", Selector: "123", }, @@ -319,7 +332,7 @@ func TestViewRun(t *testing.T) { ) }, wantErr: true, - wantErrOut: "could not find workflow file flow.yml, try specifying a branch or tag using --ref", + wantErrOut: "could not find workflow file flow.yml, try specifying a branch or tag using `--ref`", }, { name: "workflow with yaml and ref",