| 34 | } |
| 35 | |
| 36 | func EnableRepoOverride(cmd *cobra.Command, f *Factory) { |
| 37 | cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format") |
| 38 | _ = cmd.RegisterFlagCompletionFunc("repo", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 39 | remotes, err := f.Remotes() |
| 40 | if err != nil { |
| 41 | return nil, cobra.ShellCompDirectiveError |
| 42 | } |
| 43 | |
| 44 | config, err := f.Config() |
| 45 | if err != nil { |
| 46 | return nil, cobra.ShellCompDirectiveError |
| 47 | } |
| 48 | defaultHost, _ := config.Authentication().DefaultHost() |
| 49 | |
| 50 | var results []string |
| 51 | for _, remote := range remotes { |
| 52 | repo := remote.RepoOwner() + "/" + remote.RepoName() |
| 53 | if !strings.EqualFold(remote.RepoHost(), defaultHost) { |
| 54 | repo = remote.RepoHost() + "/" + repo |
| 55 | } |
| 56 | if strings.HasPrefix(repo, toComplete) { |
| 57 | results = append(results, repo) |
| 58 | } |
| 59 | } |
| 60 | sort.Strings(results) |
| 61 | return results, cobra.ShellCompDirectiveNoFileComp |
| 62 | }) |
| 63 | |
| 64 | overrideCmd := cmd |
| 65 | cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { |
| 66 | if err := executeParentHook(overrideCmd, cmd, args); err != nil { |
| 67 | return err |
| 68 | } |
| 69 | repoOverride, _ := cmd.Flags().GetString("repo") |
| 70 | f.BaseRepo = OverrideBaseRepoFunc(f.BaseRepo, repoOverride) |
| 71 | return nil |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func OverrideBaseRepoFunc(baseRepoFunc func() (ghrepo.Interface, error), override string) func() (ghrepo.Interface, error) { |
| 76 | if override == "" { |