From 6e7cd6fd05ff20d301bb7148166590556c0aeb98 Mon Sep 17 00:00:00 2001 From: ravocean Date: Sun, 23 May 2021 15:31:01 -0700 Subject: [PATCH] Added parseArgs() function, beginning to work on parseFlags() functions --- pkg/cmd/browse/browse.go | 59 +++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index 69364770d..3e065399e 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -2,33 +2,62 @@ package browse import ( "fmt" + "net/http" + "strconv" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/cmdutil" + "github.com/cli/cli/pkg/iostreams" "github.com/spf13/cobra" ) +type browser interface { + Browse(string) error +} + +type BrowseOptions struct { + HttpClient func() (*http.Client, error) + IO *iostreams.IOStreams + 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 +} func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command { + opts := &BrowseOptions{ + IO: f.IOStreams, + HttpClient: f.HttpClient, + Browser: f.Browser, + BaseRepo: f.BaseRepo, + } + cmd := &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 Run: func(cmd *cobra.Command, args []string) { - openInBrowser(cmd, f) // run gets rid of the usage / runs function + + if len(args) > 0 { + opts.SelectorArg = args[0] + } + openInBrowser(cmd, opts) // run gets rid of the usage / runs function }, } return cmd } -func openInBrowser(cmd *cobra.Command, f *cmdutil.Factory) { +func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) { // args := cmd.Args - baseRepo, err := f.BaseRepo() - w := f.IOStreams.ErrOut - cs := f.IOStreams.ColorScheme() + + baseRepo, err := opts.BaseRepo() + w := opts.IO.ErrOut + cs := opts.IO.ColorScheme() help := "Use 'gh browse --help' for more information about browse\n" if err != nil { fmt.Fprintf(w, "%s Navigate to a repository to open in browser\n%s", @@ -39,13 +68,29 @@ func openInBrowser(cmd *cobra.Command, f *cmdutil.Factory) { // ATTN: add into the empty string where you want to go within the repo repoUrl := ghrepo.GenerateRepoURL(baseRepo, "") - f.Browser.Browse(repoUrl) + opts.Browser.Browse(repoUrl) fmt.Fprintf(w, "%s Now opening %s in browser . . .\n", cs.Green("✓"), cs.Bold(ghrepo.FullName(baseRepo))) } +func parseArgs(opts *BrowseOptions) { + if opts.SelectorArg != "" { + + convertedArg, err := strconv.Atoi(opts.SelectorArg) + if err != nil { //It's not a number, but a file name + opts.FileArg = opts.SelectorArg + + } else { // It's a number, open issue or pull request + opts.NumberArg = convertedArg + } + } +} + +func parseFlags(opts *BrowseOptions) { + +} + //hello //making pull request -