From 1e61f41cd93431e643312f1e49a5a772835076ef Mon Sep 17 00:00:00 2001 From: ravocean Date: Tue, 25 May 2021 21:42:42 -0700 Subject: [PATCH] Tidied up the logic of openInBrowser --- pkg/cmd/browse/browse.go | 71 +++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index 718942c9f..35682a683 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -23,13 +23,14 @@ type BrowseOptions struct { BaseRepo func() (ghrepo.Interface, error) Browser browser - SelectorArg string - FileArg string // Used for storing the file path - NumberArg int // Used for storing pull request number + SelectorArg string + AdditionalArg string + // FileArg string // Used for storing the file path + IsNumberArg bool // Used for storing pull request number - ProjectsFlag bool - WikiFlag bool - SettingsFlag bool + ProjectsFlag bool + WikiFlag bool + SettingsFlag bool } type exitCode int @@ -56,9 +57,14 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command { Long: "Work with GitHub in the browser", // displays when you are on the help page of this command Short: "Open GitHub in the browser", // displays in the gh root help Use: "browse", // necessary!!! This is the cmd that gets passed on the prompt - Args: cobra.RangeArgs(0, 1), // make sure only one arg at most is passed + Args: cobra.RangeArgs(0, 2), // make sure only one arg at most is passed Run: func(cmd *cobra.Command, args []string) { + + if len(args) > 1 { + + } + if len(args) > 0 { opts.SelectorArg = args[0] } @@ -90,28 +96,38 @@ func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) { repoUrl := ghrepo.GenerateRepoURL(baseRepo, "") parseArgs(opts) - if !hasArgs(opts) { // handle flags without args - repoUrl += addFlags(opts) // add flags, if any - response := getHttpResponse(opts, repoUrl) - if response != exitSuccess { // test the connection to see if the url is valid - printExit(response, cmd, opts, repoUrl) // if not, print error, and return - return - } - opts.Browser.Browse(repoUrl) // otherwise open repo - printExit(response, cmd, opts, repoUrl) // print success - return - } + if !hasArgs(opts) && hasFlags(cmd) { + repoUrl += addFlags(opts) + } else if hasArgs(opts) && !hasFlags(cmd) { + repoUrl += addArgs(opts) + } else if hasArgs(opts) && hasFlags(cmd) { + + } + + + + // if !hasArgs(opts) { // handle flags without args + // repoUrl += addFlags(opts) // add flags, if any + // response := getHttpResponse(opts, repoUrl) + // if response != exitSuccess { // test the connection to see if the url is valid + // printExit(response, cmd, opts, repoUrl) // if not, print error, and return + // return + // } + // opts.Browser.Browse(repoUrl) // otherwise open repo + // printExit(response, cmd, opts, repoUrl) // print success + // return + // } printExit(exitError, cmd, opts, "") } func parseArgs(opts *BrowseOptions) { if opts.SelectorArg != "" { - convertedArg, err := strconv.Atoi(opts.SelectorArg) + _, err := strconv.Atoi(opts.SelectorArg) if err != nil { //It's not a number, but a file name - opts.FileArg = opts.SelectorArg + opts.IsNumberArg = false } else { // It's a number, open issue or pull request - opts.NumberArg = convertedArg + opts.IsNumberArg = true } } } @@ -127,6 +143,13 @@ func addFlags(opts *BrowseOptions) string { return "" } +func addArgs(opts *BrowseOptions) string { + if opts.IsNumberArg { + return "/issues/" + opts.SelectorArg + } + return "/" + opts.SelectorArg +} + func printExit(errorCode exitCode, cmd *cobra.Command, opts *BrowseOptions, url string) { w := opts.IO.ErrOut cs := opts.IO.ColorScheme() @@ -181,8 +204,12 @@ func getHttpResponse(opts *BrowseOptions, url string) exitCode { return exitError } +func hasFlags(cmd *cobra.Command) bool { + return getFlagAmount(cmd) > 0 +} + func hasArgs(opts *BrowseOptions) bool { - return opts.NumberArg != 0 || opts.FileArg != "" + return opts.SelectorArg != "" } func getFlagAmount(cmd *cobra.Command) int {