From b43f78bc196f1fd6a9e4a1ba0e63a9d3ad861740 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 2 Jul 2021 23:06:50 +0200 Subject: [PATCH 1/2] completions: auto-complete `--repo` values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Looking at the locally-registered remotes, we have a pretty good idea what `--repo` values are available. Let's complete them. Helped by Nate Smith and Mislav Marohnić. Signed-off-by: Johannes Schindelin --- pkg/cmdutil/repo_override.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/cmdutil/repo_override.go b/pkg/cmdutil/repo_override.go index 6ce68011b..d9e63aa4f 100644 --- a/pkg/cmdutil/repo_override.go +++ b/pkg/cmdutil/repo_override.go @@ -2,6 +2,8 @@ package cmdutil import ( "os" + "sort" + "strings" "github.com/cli/cli/internal/ghrepo" "github.com/spf13/cobra" @@ -19,6 +21,30 @@ func executeParentHooks(cmd *cobra.Command, args []string) error { func EnableRepoOverride(cmd *cobra.Command, f *Factory) { cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format") + _ = cmd.RegisterFlagCompletionFunc("repo", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + var results []string + remotes, err := f.Remotes() + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + config, err := f.Config() + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + gh_host,err := config.DefaultHost() + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + for _, remote := range remotes.FilterByHosts([]string{gh_host}) { + repo := remote.RepoOwner() + "/" + remote.RepoName() + if strings.HasPrefix(repo, toComplete) { + results = append(results, repo) + } + } + sort.Strings(results) + return results, cobra.ShellCompDirectiveNoFileComp + }) cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { if err := executeParentHooks(cmd, args); err != nil { From d6b70beeaa92110054e5a27d0a4a2554e83916eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 28 Jul 2021 17:18:56 +0200 Subject: [PATCH 2/2] List repos from non-default hostnames in completions for `-R` --- pkg/cmdutil/repo_override.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/cmdutil/repo_override.go b/pkg/cmdutil/repo_override.go index d9e63aa4f..bdbe3a518 100644 --- a/pkg/cmdutil/repo_override.go +++ b/pkg/cmdutil/repo_override.go @@ -22,22 +22,26 @@ func executeParentHooks(cmd *cobra.Command, args []string) error { func EnableRepoOverride(cmd *cobra.Command, f *Factory) { cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format") _ = cmd.RegisterFlagCompletionFunc("repo", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - var results []string remotes, err := f.Remotes() if err != nil { return nil, cobra.ShellCompDirectiveError } + config, err := f.Config() if err != nil { return nil, cobra.ShellCompDirectiveError } - gh_host,err := config.DefaultHost() + defaultHost, err := config.DefaultHost() if err != nil { return nil, cobra.ShellCompDirectiveError } - for _, remote := range remotes.FilterByHosts([]string{gh_host}) { + var results []string + for _, remote := range remotes { repo := remote.RepoOwner() + "/" + remote.RepoName() + if !strings.EqualFold(remote.RepoHost(), defaultHost) { + repo = remote.RepoHost() + "/" + repo + } if strings.HasPrefix(repo, toComplete) { results = append(results, repo) }