@@ -11,7 +11,6 @@ import (
1111 "github.com/cli/cli/internal/ghrepo"
1212 "github.com/cli/cli/pkg/cmdutil"
1313 "github.com/cli/cli/pkg/iostreams"
14- "github.com/cli/cli/utils"
1514 "github.com/spf13/cobra"
1615)
1716
@@ -25,12 +24,10 @@ type BrowseOptions struct {
2524 HttpClient func () (* http.Client , error )
2625 IO * iostreams.IOStreams
2726
28- FlagAmount int
2927 SelectorArg string
3028
3129 Branch string
3230 ProjectsFlag bool
33- RepoFlag bool
3431 SettingsFlag bool
3532 WikiFlag bool
3633}
@@ -71,25 +68,33 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
7168 - by path for opening folders and files, e.g. "cmd/gh/main.go"
7269 ` ),
7370 "help:environment" : heredoc .Doc (`
74- To configure a web browser other than the default, use the BROWSER environment variable.
71+ To configure a web browser other than the default, use the BROWSER environment variable.
7572 ` ),
7673 },
7774 RunE : func (cmd * cobra.Command , args []string ) error {
7875 opts .BaseRepo = f .BaseRepo
79- opts .FlagAmount = cmd .Flags ().NFlag ()
8076
81- if opts . FlagAmount > 2 {
82- return & cmdutil. FlagError { Err : fmt . Errorf ( "cannot have more than two flags, %d were recieved" , opts . FlagAmount )}
77+ if len ( args ) > 0 {
78+ opts . SelectorArg = args [ 0 ]
8379 }
84- if repoOverride , _ := cmd .Flags ().GetString ("repo" ); repoOverride != "" {
85- opts .RepoFlag = true
80+
81+ if err := cmdutil .MutuallyExclusive ("cannot use --projects with --settings" , opts .ProjectsFlag , opts .SettingsFlag ); err != nil {
82+ return err
8683 }
87- if opts . FlagAmount > 1 && ! opts .RepoFlag {
88- return & cmdutil. FlagError { Err : fmt . Errorf ( "these two flags are incompatible, see below for instructions" )}
84+ if err := cmdutil . MutuallyExclusive ( "cannot use --projects with --wiki" , opts .ProjectsFlag , opts . WikiFlag ); err != nil {
85+ return err
8986 }
90-
91- if len (args ) > 0 {
92- opts .SelectorArg = args [0 ]
87+ if err := cmdutil .MutuallyExclusive ("cannot use --projects with --branch" , opts .ProjectsFlag , opts .Branch != "" ); err != nil {
88+ return err
89+ }
90+ if err := cmdutil .MutuallyExclusive ("cannot use --settings with --wiki" , opts .SettingsFlag , opts .WikiFlag ); err != nil {
91+ return err
92+ }
93+ if err := cmdutil .MutuallyExclusive ("cannot use --settings with --branch" , opts .SettingsFlag , opts .Branch != "" ); err != nil {
94+ return err
95+ }
96+ if err := cmdutil .MutuallyExclusive ("cannot use --wiki with --branch" , opts .WikiFlag , opts .Branch != "" ); err != nil {
97+ return err
9398 }
9499
95100 if runF != nil {
@@ -98,6 +103,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
98103 return runBrowse (opts )
99104 },
100105 }
106+
101107 cmdutil .EnableRepoOverride (cmd , f )
102108 cmd .Flags ().BoolVarP (& opts .ProjectsFlag , "projects" , "p" , false , "Open repository projects" )
103109 cmd .Flags ().BoolVarP (& opts .WikiFlag , "wiki" , "w" , false , "Open repository wiki" )
@@ -119,96 +125,58 @@ func runBrowse(opts *BrowseOptions) error {
119125 return fmt .Errorf ("unable to create an http client: %w\n Use 'gh browse --help' for more information about browse\n " , err )
120126 }
121127
122- repoUrl := ghrepo .GenerateRepoURL (baseRepo , "" )
123-
124- hasArg := opts .SelectorArg != ""
125- hasFlag := opts .FlagAmount > 0
128+ url := ghrepo .GenerateRepoURL (baseRepo , "" )
126129
127- if opts .Branch != "" {
128- repoUrl , err = addBranch (opts , repoUrl )
129- if err != nil {
130- return err
131- }
132- } else if hasArg && (! hasFlag || opts .RepoFlag ) {
133- apiClient := api .NewClientFromHTTP (httpClient )
134- branchName , err := api .RepoDefaultBranch (apiClient , baseRepo )
135- if err != nil {
136- return fmt .Errorf ("unable to determine default branch for %s: %w" , ghrepo .FullName (baseRepo ), err )
137- }
138- repoUrl , err = addArg (opts , repoUrl , branchName )
139- if err != nil {
140- return err
141- }
142- } else if ! hasArg && hasFlag {
143- repoUrl = addFlag (opts , repoUrl )
144- } else {
145- if opts .IO .IsStdoutTTY () {
146- fmt .Fprintf (opts .IO .Out , "Opening %s in your browser.\n " , utils .DisplayURL (repoUrl ))
147- }
130+ if opts .ProjectsFlag {
131+ err := opts .Browser .Browse (url + "/projects" )
132+ return err
148133 }
149- opts .Browser .Browse (repoUrl )
150- return nil
151- }
152134
153- func addBranch (opts * BrowseOptions , url string ) (string , error ) {
154- if opts .SelectorArg == "" {
155- url += "/tree/" + opts .Branch
156- } else {
157- arr , err := parseFileArg (opts .SelectorArg )
158- if err != nil {
159- return url , err
160- }
161- url += parsedUrl (arr , opts .Branch )
162- }
163- if opts .IO .IsStdoutTTY () {
164- fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
135+ if opts .SettingsFlag {
136+ err := opts .Browser .Browse (url + "/settings" )
137+ return err
165138 }
166- return url , nil
167- }
168139
169- func addFlag (opts * BrowseOptions , url string ) string {
170- if opts .ProjectsFlag {
171- url += "/projects"
172- } else if opts .SettingsFlag {
173- url += "/settings"
174- } else if opts .WikiFlag {
175- url += "/wiki"
176- }
177- if opts .IO .IsStdoutTTY () {
178- fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
140+ if opts .WikiFlag {
141+ err := opts .Browser .Browse (url + "/wiki" )
142+ return err
179143 }
180- return url
181- }
182144
183- func addArg (opts * BrowseOptions , url string , branchName string ) (string , error ) {
184145 if isNumber (opts .SelectorArg ) {
185146 url += "/issues/" + opts .SelectorArg
186- if opts .IO .IsStdoutTTY () {
187- fmt .Fprintf (opts .IO .Out , "now opening issue/pr in browser . . .\n " )
188- }
147+ err := opts .Browser .Browse (url )
148+ return err
149+ }
150+
151+ if opts .Branch != "" {
152+ url += "/tree/" + opts .Branch + "/"
189153 } else {
190- arr , err := parseFileArg (opts .SelectorArg )
154+ apiClient := api .NewClientFromHTTP (httpClient )
155+ branchName , err := api .RepoDefaultBranch (apiClient , baseRepo )
191156 if err != nil {
192- return url , err
157+ return err
193158 }
159+ url += "/tree/" + branchName + "/"
160+ }
194161
195- url += parsedUrl (arr , branchName )
196-
197- if opts .IO .IsStdoutTTY () {
198- fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
162+ if opts .SelectorArg != "" {
163+ arr , err := parseFileArg (opts .SelectorArg )
164+ if err != nil {
165+ return err
166+ }
167+ if len (arr ) > 1 {
168+ url += arr [0 ] + "#L" + arr [1 ]
169+ } else {
170+ url += arr [0 ]
199171 }
200172 }
201- return url , nil
202- }
203173
204- func parsedUrl (arr []string , branchName string ) string {
205- if len (arr ) > 1 {
206- return "/tree/" + branchName + "/" + arr [0 ] + "#L" + arr [1 ]
207- } else {
208- return "/tree/" + branchName + "/" + arr [0 ]
174+ err = opts .Browser .Browse (url )
175+ if opts .IO .IsStdoutTTY () && err == nil {
176+ fmt .Fprintf (opts .IO .Out , "now opening %s in browser\n " , url )
209177 }
178+ return err
210179}
211-
212180func parseFileArg (fileArg string ) ([]string , error ) {
213181 arr := strings .Split (fileArg , ":" )
214182 if len (arr ) > 2 {
@@ -217,6 +185,7 @@ func parseFileArg(fileArg string) ([]string, error) {
217185 if len (arr ) > 1 && ! isNumber (arr [1 ]) {
218186 return arr , fmt .Errorf ("invalid line number after colon\n Use 'gh browse --help' for more information about browse\n " )
219187 }
188+
220189 return arr , nil
221190}
222191
0 commit comments