| 20 | } |
| 21 | |
| 22 | func EnableRepoOverride(cmd *cobra.Command, f *Factory) { |
| 23 | cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format") |
| 24 | _ = cmd.RegisterFlagCompletionFunc("repo", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 25 | remotes, err := f.Remotes() |
| 26 | if err != nil { |
| 27 | return nil, cobra.ShellCompDirectiveError |
| 28 | } |
| 29 | |
| 30 | config, err := f.Config() |
| 31 | if err != nil { |
| 32 | return nil, cobra.ShellCompDirectiveError |
| 33 | } |
| 34 | defaultHost, _ := config.Authentication().DefaultHost() |
| 35 | |
| 36 | var results []string |
| 37 | for _, remote := range remotes { |
| 38 | repo := remote.RepoOwner() + "/" + remote.RepoName() |
| 39 | if !strings.EqualFold(remote.RepoHost(), defaultHost) { |
| 40 | repo = remote.RepoHost() + "/" + repo |
| 41 | } |
| 42 | if strings.HasPrefix(repo, toComplete) { |
| 43 | results = append(results, repo) |
| 44 | } |
| 45 | } |
| 46 | sort.Strings(results) |
| 47 | return results, cobra.ShellCompDirectiveNoFileComp |
| 48 | }) |
| 49 | |
| 50 | cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { |
| 51 | if err := executeParentHooks(cmd, args); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | repoOverride, _ := cmd.Flags().GetString("repo") |
| 55 | f.BaseRepo = OverrideBaseRepoFunc(f.BaseRepo, repoOverride) |
| 56 | return nil |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func OverrideBaseRepoFunc(baseRepoFunc func() (ghrepo.Interface, error), override string) func() (ghrepo.Interface, error) { |
| 61 | if override == "" { |