Skip to content

Commit 4b499be

Browse files
authored
Merge pull request cli#3942 from dscho/complete--repo-flag
Allow auto-completing the `--repo` values
2 parents ef9b781 + d6b70be commit 4b499be

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pkg/cmdutil/repo_override.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cmdutil
22

33
import (
44
"os"
5+
"sort"
6+
"strings"
57

68
"github.com/cli/cli/internal/ghrepo"
79
"github.com/spf13/cobra"
@@ -19,6 +21,34 @@ func executeParentHooks(cmd *cobra.Command, args []string) error {
1921

2022
func EnableRepoOverride(cmd *cobra.Command, f *Factory) {
2123
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, err := config.DefaultHost()
35+
if err != nil {
36+
return nil, cobra.ShellCompDirectiveError
37+
}
38+
39+
var results []string
40+
for _, remote := range remotes {
41+
repo := remote.RepoOwner() + "/" + remote.RepoName()
42+
if !strings.EqualFold(remote.RepoHost(), defaultHost) {
43+
repo = remote.RepoHost() + "/" + repo
44+
}
45+
if strings.HasPrefix(repo, toComplete) {
46+
results = append(results, repo)
47+
}
48+
}
49+
sort.Strings(results)
50+
return results, cobra.ShellCompDirectiveNoFileComp
51+
})
2252

2353
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
2454
if err := executeParentHooks(cmd, args); err != nil {

0 commit comments

Comments
 (0)