| 28 | } |
| 29 | |
| 30 | func NewCmdRevert(f *cmdutil.Factory, runF func(*RevertOptions) error) *cobra.Command { |
| 31 | opts := &RevertOptions{ |
| 32 | IO: f.IOStreams, |
| 33 | HttpClient: f.HttpClient, |
| 34 | } |
| 35 | |
| 36 | var bodyFile string |
| 37 | |
| 38 | cmd := &cobra.Command{ |
| 39 | Use: "revert {<number> | <url> | <branch>}", |
| 40 | Short: "Revert a pull request", |
| 41 | Args: cmdutil.ExactArgs(1, "cannot revert pull request: number, url, or branch required"), |
| 42 | RunE: func(cmd *cobra.Command, args []string) error { |
| 43 | opts.Finder = shared.NewFinder(f) |
| 44 | |
| 45 | if len(args) > 0 { |
| 46 | opts.SelectorArg = args[0] |
| 47 | } |
| 48 | |
| 49 | bodyProvided := cmd.Flags().Changed("body") |
| 50 | bodyFileProvided := bodyFile != "" |
| 51 | |
| 52 | if err := cmdutil.MutuallyExclusive( |
| 53 | "specify only one of `--body` or `--body-file`", |
| 54 | bodyProvided, |
| 55 | bodyFileProvided, |
| 56 | ); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | if bodyProvided || bodyFileProvided { |
| 61 | opts.BodySet = true |
| 62 | if bodyFileProvided { |
| 63 | b, err := cmdutil.ReadFile(bodyFile, opts.IO.In) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | opts.Body = string(b) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if runF != nil { |
| 72 | return runF(opts) |
| 73 | } |
| 74 | return revertRun(opts) |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | cmd.Flags().BoolVarP(&opts.IsDraft, "draft", "d", false, "Mark revert pull request as a draft") |
| 79 | cmd.Flags().StringVarP(&opts.Title, "title", "t", "", "Title for the revert pull request") |
| 80 | cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Body for the revert pull request") |
| 81 | cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file` (use \"-\" to read from standard input)") |
| 82 | return cmd |
| 83 | } |
| 84 | |
| 85 | func revertRun(opts *RevertOptions) error { |
| 86 | cs := opts.IO.ColorScheme() |