| 43 | } |
| 44 | |
| 45 | func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { |
| 46 | opts := &ViewOptions{ |
| 47 | IO: f.IOStreams, |
| 48 | HttpClient: f.HttpClient, |
| 49 | Browser: f.Browser, |
| 50 | Prompter: f.Prompter, |
| 51 | now: time.Now(), |
| 52 | } |
| 53 | |
| 54 | cmd := &cobra.Command{ |
| 55 | Use: "view [<workflow-id> | <workflow-name> | <filename>]", |
| 56 | Short: "View the summary of a workflow", |
| 57 | Args: cobra.MaximumNArgs(1), |
| 58 | Example: heredoc.Doc(` |
| 59 | # Interactively select a workflow to view |
| 60 | $ gh workflow view |
| 61 | |
| 62 | # View a specific workflow |
| 63 | $ gh workflow view 0451 |
| 64 | `), |
| 65 | RunE: func(cmd *cobra.Command, args []string) error { |
| 66 | // support `-R, --repo` override |
| 67 | opts.BaseRepo = f.BaseRepo |
| 68 | |
| 69 | opts.Raw = !opts.IO.IsStdoutTTY() |
| 70 | |
| 71 | if len(args) > 0 { |
| 72 | opts.Selector = args[0] |
| 73 | } else if !opts.IO.CanPrompt() { |
| 74 | return cmdutil.FlagErrorf("workflow argument required when not running interactively") |
| 75 | } else { |
| 76 | opts.Prompt = true |
| 77 | } |
| 78 | |
| 79 | if !opts.YAML && opts.Ref != "" { |
| 80 | return cmdutil.FlagErrorf("`--yaml` required when specifying `--ref`") |
| 81 | } |
| 82 | |
| 83 | if runF != nil { |
| 84 | return runF(opts) |
| 85 | } |
| 86 | return runView(opts) |
| 87 | }, |
| 88 | } |
| 89 | |
| 90 | cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open workflow in the browser") |
| 91 | cmd.Flags().BoolVarP(&opts.YAML, "yaml", "y", false, "View the workflow yaml file") |
| 92 | 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") |
| 93 | |
| 94 | _ = cmdutil.RegisterBranchCompletionFlags(f.GitClient, cmd, "ref") |
| 95 | |
| 96 | return cmd |
| 97 | } |
| 98 | |
| 99 | func runView(opts *ViewOptions) error { |
| 100 | c, err := opts.HttpClient() |