| 36 | } |
| 37 | |
| 38 | func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobra.Command { |
| 39 | opts := &CheckoutOptions{ |
| 40 | IO: f.IOStreams, |
| 41 | HttpClient: f.HttpClient, |
| 42 | GitClient: f.GitClient, |
| 43 | Config: f.Config, |
| 44 | Remotes: f.Remotes, |
| 45 | Branch: f.Branch, |
| 46 | } |
| 47 | |
| 48 | cmd := &cobra.Command{ |
| 49 | Use: "checkout [<number> | <url> | <branch>]", |
| 50 | Short: "Check out a pull request in git", |
| 51 | Example: heredoc.Doc(` |
| 52 | # Interactively select a PR from the 10 most recent to check out |
| 53 | $ gh pr checkout |
| 54 | |
| 55 | # Checkout a specific PR |
| 56 | $ gh pr checkout 32 |
| 57 | $ gh pr checkout https://github.com/OWNER/REPO/pull/32 |
| 58 | $ gh pr checkout feature |
| 59 | `), |
| 60 | Args: cobra.MaximumNArgs(1), |
| 61 | Aliases: []string{"co"}, |
| 62 | RunE: func(cmd *cobra.Command, args []string) error { |
| 63 | if len(args) > 0 { |
| 64 | opts.PRResolver = &specificPRResolver{ |
| 65 | prFinder: shared.NewFinder(f), |
| 66 | selector: args[0], |
| 67 | } |
| 68 | } else if opts.IO.CanPrompt() { |
| 69 | baseRepo, err := f.BaseRepo() |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | httpClient, err := f.HttpClient() |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | opts.PRResolver = &promptingPRResolver{ |
| 80 | io: opts.IO, |
| 81 | prompter: f.Prompter, |
| 82 | prLister: shared.NewLister(httpClient), |
| 83 | baseRepo: baseRepo, |
| 84 | } |
| 85 | } else { |
| 86 | return cmdutil.FlagErrorf("pull request number, URL, or branch required when not running interactively") |
| 87 | } |
| 88 | |
| 89 | if runF != nil { |
| 90 | return runF(opts) |
| 91 | } |
| 92 | return checkoutRun(opts) |
| 93 | }, |
| 94 | } |
| 95 | |