From 8a4872bab38611904db7a58244c40c92c76cc8e0 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 22 Jun 2020 14:07:49 -0400 Subject: [PATCH 1/7] Remove global repo flag --- command/root.go | 1 - 1 file changed, 1 deletion(-) diff --git a/command/root.go b/command/root.go index 693603e05..3f83ba278 100644 --- a/command/root.go +++ b/command/root.go @@ -52,7 +52,6 @@ func init() { RootCmd.AddCommand(versionCmd) RootCmd.SetVersionTemplate(versionOutput) - RootCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") RootCmd.PersistentFlags().Bool("help", false, "Show help for command") RootCmd.Flags().Bool("version", false, "Show gh version") // TODO: From dd8cbccbd54ed5b1c94560ab0d917f853ae716c9 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 22 Jun 2020 14:26:41 -0400 Subject: [PATCH 2/7] Add repo flag to issue commands --- command/issue.go | 6 ++++++ command/util.go | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 command/util.go diff --git a/command/issue.go b/command/issue.go index 372ad8bd1..ca9087dbb 100644 --- a/command/issue.go +++ b/command/issue.go @@ -24,8 +24,10 @@ import ( func init() { RootCmd.AddCommand(issueCmd) issueCmd.AddCommand(issueStatusCmd) + includeRepoFlag(issueStatusCmd) issueCmd.AddCommand(issueCreateCmd) + includeRepoFlag(issueCreateCmd) issueCreateCmd.Flags().StringP("title", "t", "", "Supply a title. Will prompt for one otherwise.") issueCreateCmd.Flags().StringP("body", "b", "", @@ -37,6 +39,7 @@ func init() { issueCreateCmd.Flags().StringP("milestone", "m", "", "Add the issue to a milestone by `name`") issueCmd.AddCommand(issueListCmd) + includeRepoFlag(issueListCmd) issueListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label") issueListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|all}") @@ -44,10 +47,13 @@ func init() { issueListCmd.Flags().StringP("author", "A", "", "Filter by author") issueCmd.AddCommand(issueViewCmd) + includeRepoFlag(issueViewCmd) issueViewCmd.Flags().BoolP("web", "w", false, "Open an issue in the browser") issueCmd.AddCommand(issueCloseCmd) + includeRepoFlag(issueCloseCmd) issueCmd.AddCommand(issueReopenCmd) + includeRepoFlag(issueReopenCmd) } var issueCmd = &cobra.Command{ diff --git a/command/util.go b/command/util.go new file mode 100644 index 000000000..a7d4a0f31 --- /dev/null +++ b/command/util.go @@ -0,0 +1,8 @@ +package Command + +import "github.com/spf13/cobra" +package Command + +func includeRepoFlag(cmd *cobra.Command) { + cmd.StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") +} From 6a02ad3f1b83cab385d36cdc0e59ef436d38cd1e Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 22 Jun 2020 15:09:21 -0400 Subject: [PATCH 3/7] Add repoflag to pr commands --- command/pr.go | 7 +++++++ command/pr_diff.go | 1 + command/pr_review.go | 1 + 3 files changed, 9 insertions(+) diff --git a/command/pr.go b/command/pr.go index 18150d4eb..0aa7d25b1 100644 --- a/command/pr.go +++ b/command/pr.go @@ -27,16 +27,22 @@ func init() { prCmd.AddCommand(prCheckoutCmd) prCmd.AddCommand(prCreateCmd) prCmd.AddCommand(prStatusCmd) + includeRepoFlag(prStatusCmd) prCmd.AddCommand(prCloseCmd) + includeRepoFlag(prCloseCmd) prCmd.AddCommand(prReopenCmd) + includeRepoFlag(prReopenCmd) prCmd.AddCommand(prMergeCmd) + includeRepoFlag(prMergeCmd) prMergeCmd.Flags().BoolP("delete-branch", "d", true, "Delete the local and remote branch after merge") prMergeCmd.Flags().BoolP("merge", "m", false, "Merge the commits with the base branch") prMergeCmd.Flags().BoolP("rebase", "r", false, "Rebase the commits onto the base branch") prMergeCmd.Flags().BoolP("squash", "s", false, "Squash the commits into one commit and merge it into the base branch") prCmd.AddCommand(prReadyCmd) + includeRepoFlag(prReadyCmd) prCmd.AddCommand(prListCmd) + includeRepoFlag(prListCmd) prListCmd.Flags().IntP("limit", "L", 30, "Maximum number of items to fetch") prListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|merged|all}") prListCmd.Flags().StringP("base", "B", "", "Filter by base branch") @@ -44,6 +50,7 @@ func init() { prListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") prCmd.AddCommand(prViewCmd) + includeRepoFlag(prViewCmd) prViewCmd.Flags().BoolP("web", "w", false, "Open a pull request in the browser") } diff --git a/command/pr_diff.go b/command/pr_diff.go index ae50a2618..0bc9c8613 100644 --- a/command/pr_diff.go +++ b/command/pr_diff.go @@ -19,6 +19,7 @@ func init() { prDiffCmd.Flags().StringP("color", "c", "auto", "Whether or not to output color: {always|never|auto}") prCmd.AddCommand(prDiffCmd) + includeRepoFlag(prDiffCmd) } func prDiff(cmd *cobra.Command, args []string) error { diff --git a/command/pr_review.go b/command/pr_review.go index 08efc638d..77723ab44 100644 --- a/command/pr_review.go +++ b/command/pr_review.go @@ -15,6 +15,7 @@ import ( func init() { prCmd.AddCommand(prReviewCmd) + includeRepoFlag(prCmd) prReviewCmd.Flags().BoolP("approve", "a", false, "Approve pull request") prReviewCmd.Flags().BoolP("request-changes", "r", false, "Request changes on a pull request") From 2761739c29c7f12fb9a1f21e7f5c5bb8779c212f Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 22 Jun 2020 15:19:33 -0400 Subject: [PATCH 4/7] Correct package name --- command/util.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/command/util.go b/command/util.go index a7d4a0f31..130f97844 100644 --- a/command/util.go +++ b/command/util.go @@ -1,8 +1,7 @@ -package Command +package command import "github.com/spf13/cobra" -package Command func includeRepoFlag(cmd *cobra.Command) { - cmd.StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") + cmd.Flags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") } From 625b673b587095df7d985a06e5e5883559215687 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 22 Jun 2020 15:30:22 -0400 Subject: [PATCH 5/7] Ignore repo flag errors in determineBaseRepo --- command/root.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/command/root.go b/command/root.go index 3f83ba278..83dfbd3f0 100644 --- a/command/root.go +++ b/command/root.go @@ -303,8 +303,8 @@ func changelogURL(version string) string { } func determineBaseRepo(apiClient *api.Client, cmd *cobra.Command, ctx context.Context) (ghrepo.Interface, error) { - repo, err := cmd.Flags().GetString("repo") - if err == nil && repo != "" { + repo, _ := cmd.Flags().GetString("repo") + if repo != "" { baseRepo, err := ghrepo.FromFullName(repo) if err != nil { return nil, fmt.Errorf("argument error: %w", err) @@ -312,17 +312,12 @@ func determineBaseRepo(apiClient *api.Client, cmd *cobra.Command, ctx context.Co return baseRepo, nil } - baseOverride, err := cmd.Flags().GetString("repo") - if err != nil { - return nil, err - } - remotes, err := ctx.Remotes() if err != nil { return nil, err } - repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, baseOverride) + repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, repo) if err != nil { return nil, err } From db88ac415523282f6c7cba3fa6434586934ab737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 30 Jun 2020 18:51:53 +0200 Subject: [PATCH 6/7] Declare `-R, --repo` flag on all `issue` and `pr` subcommands --- command/issue.go | 8 ++------ command/pr.go | 9 ++------- command/pr_diff.go | 1 - command/pr_review.go | 1 - command/util.go | 7 ------- 5 files changed, 4 insertions(+), 22 deletions(-) delete mode 100644 command/util.go diff --git a/command/issue.go b/command/issue.go index 9569c9603..287ca7260 100644 --- a/command/issue.go +++ b/command/issue.go @@ -22,12 +22,12 @@ import ( ) func init() { + issueCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") + RootCmd.AddCommand(issueCmd) issueCmd.AddCommand(issueStatusCmd) - includeRepoFlag(issueStatusCmd) issueCmd.AddCommand(issueCreateCmd) - includeRepoFlag(issueCreateCmd) issueCreateCmd.Flags().StringP("title", "t", "", "Supply a title. Will prompt for one otherwise.") issueCreateCmd.Flags().StringP("body", "b", "", @@ -39,7 +39,6 @@ func init() { issueCreateCmd.Flags().StringP("milestone", "m", "", "Add the issue to a milestone by `name`") issueCmd.AddCommand(issueListCmd) - includeRepoFlag(issueListCmd) issueListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by labels") issueListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|all}") @@ -47,13 +46,10 @@ func init() { issueListCmd.Flags().StringP("author", "A", "", "Filter by author") issueCmd.AddCommand(issueViewCmd) - includeRepoFlag(issueViewCmd) issueViewCmd.Flags().BoolP("web", "w", false, "Open an issue in the browser") issueCmd.AddCommand(issueCloseCmd) - includeRepoFlag(issueCloseCmd) issueCmd.AddCommand(issueReopenCmd) - includeRepoFlag(issueReopenCmd) } var issueCmd = &cobra.Command{ diff --git a/command/pr.go b/command/pr.go index 961cdbf1a..6d8ae22aa 100644 --- a/command/pr.go +++ b/command/pr.go @@ -23,26 +23,22 @@ import ( ) func init() { + prCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") + RootCmd.AddCommand(prCmd) prCmd.AddCommand(prCheckoutCmd) prCmd.AddCommand(prCreateCmd) prCmd.AddCommand(prStatusCmd) - includeRepoFlag(prStatusCmd) prCmd.AddCommand(prCloseCmd) - includeRepoFlag(prCloseCmd) prCmd.AddCommand(prReopenCmd) - includeRepoFlag(prReopenCmd) prCmd.AddCommand(prMergeCmd) - includeRepoFlag(prMergeCmd) prMergeCmd.Flags().BoolP("delete-branch", "d", true, "Delete the local and remote branch after merge") prMergeCmd.Flags().BoolP("merge", "m", false, "Merge the commits with the base branch") prMergeCmd.Flags().BoolP("rebase", "r", false, "Rebase the commits onto the base branch") prMergeCmd.Flags().BoolP("squash", "s", false, "Squash the commits into one commit and merge it into the base branch") prCmd.AddCommand(prReadyCmd) - includeRepoFlag(prReadyCmd) prCmd.AddCommand(prListCmd) - includeRepoFlag(prListCmd) prListCmd.Flags().IntP("limit", "L", 30, "Maximum number of items to fetch") prListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|merged|all}") prListCmd.Flags().StringP("base", "B", "", "Filter by base branch") @@ -50,7 +46,6 @@ func init() { prListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") prCmd.AddCommand(prViewCmd) - includeRepoFlag(prViewCmd) prViewCmd.Flags().BoolP("web", "w", false, "Open a pull request in the browser") } diff --git a/command/pr_diff.go b/command/pr_diff.go index 0bc9c8613..ae50a2618 100644 --- a/command/pr_diff.go +++ b/command/pr_diff.go @@ -19,7 +19,6 @@ func init() { prDiffCmd.Flags().StringP("color", "c", "auto", "Whether or not to output color: {always|never|auto}") prCmd.AddCommand(prDiffCmd) - includeRepoFlag(prDiffCmd) } func prDiff(cmd *cobra.Command, args []string) error { diff --git a/command/pr_review.go b/command/pr_review.go index 77723ab44..08efc638d 100644 --- a/command/pr_review.go +++ b/command/pr_review.go @@ -15,7 +15,6 @@ import ( func init() { prCmd.AddCommand(prReviewCmd) - includeRepoFlag(prCmd) prReviewCmd.Flags().BoolP("approve", "a", false, "Approve pull request") prReviewCmd.Flags().BoolP("request-changes", "r", false, "Request changes on a pull request") diff --git a/command/util.go b/command/util.go deleted file mode 100644 index 130f97844..000000000 --- a/command/util.go +++ /dev/null @@ -1,7 +0,0 @@ -package command - -import "github.com/spf13/cobra" - -func includeRepoFlag(cmd *cobra.Command) { - cmd.Flags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") -} From cd5a0d69fbddd10214895bcd58bd6a1e66dd596d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 2 Jul 2020 12:36:31 +0200 Subject: [PATCH 7/7] :nail_polish: be clearer about the value passed to ResolveRemotesToRepos `repo` will always be blank here, so replace the argument with a blank literal instead. --- command/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/root.go b/command/root.go index 83dfbd3f0..cf0e3cbb1 100644 --- a/command/root.go +++ b/command/root.go @@ -317,7 +317,7 @@ func determineBaseRepo(apiClient *api.Client, cmd *cobra.Command, ctx context.Co return nil, err } - repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, repo) + repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "") if err != nil { return nil, err }