@@ -33,18 +33,6 @@ type BrowseOptions struct {
3333 BranchFlag bool
3434}
3535
36- type exitCode int
37-
38- const (
39- exitUrlSuccess exitCode = 0
40- exitNonUrlSuccess exitCode = 1
41- exitNotInRepo exitCode = 2
42- exitTooManyFlags exitCode = 3
43- exitTooManyArgs exitCode = 4
44- exitExpectedArg exitCode = 5
45- exitInvalidCombo exitCode = 6
46- )
47-
4836func NewCmdBrowse (f * cmdutil.Factory ) * cobra.Command {
4937
5038 opts := & BrowseOptions {
@@ -108,117 +96,112 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
10896}
10997
11098func openInBrowser (cmd * cobra.Command , opts * BrowseOptions ) error {
111-
99+ help := "Use 'gh browse --help' for more information about browse \n "
112100 baseRepo , err := opts .BaseRepo ()
101+ if err != nil {
102+ return fmt .Errorf ("unable to determine base repository: %w\n %s" , err , help )
103+ }
104+
113105 httpClient , _ := opts .HttpClient ()
114106 apiClient := api .NewClientFromHTTP (httpClient )
115107 branchName , _ := api .RepoDefaultBranch (apiClient , baseRepo )
116108
117- if ! inRepo (err ) {
118- return printExit (exitNotInRepo , cmd , opts , "" )
119- }
120-
121109 if getFlagAmount (cmd ) > 1 {
122- return printExit ( exitTooManyFlags , cmd , opts , "" )
110+ return fmt . Errorf ( "accepts 1 flag, %d flag(s) were recieved \n %s" , getFlagAmount ( cmd ), help )
123111 }
124112
125113 repoUrl := ghrepo .GenerateRepoURL (baseRepo , "" )
126- response := exitUrlSuccess
127114
128115 if ! hasArg (opts ) && hasFlag (cmd ) {
129- response , repoUrl = addFlag (opts , repoUrl )
116+ err , repoUrl = addFlag (opts , repoUrl )
117+ if err != nil {
118+ return err
119+ }
130120 } else if hasArg (opts ) && ! hasFlag (cmd ) {
131- response , repoUrl = addArg (opts , repoUrl , branchName )
121+ err , repoUrl = addArg (opts , repoUrl , branchName )
122+ if err != nil {
123+ return err
124+ }
132125 } else if hasArg (opts ) && hasFlag (cmd ) {
133- response , repoUrl = addCombined (opts , repoUrl , branchName )
126+ err , repoUrl = addCombined (opts , repoUrl , branchName )
127+ if err != nil {
128+ return err
129+ }
134130 }
135131
136- if response == exitUrlSuccess || response == exitNonUrlSuccess {
137- opts .Browser . Browse ( repoUrl )
132+ if repoUrl == ghrepo . GenerateRepoURL ( baseRepo , "" ) {
133+ fmt . Fprintf ( opts .IO . Out , "now opening %s in browser . . . \n " , repoUrl )
138134 }
135+ opts .Browser .Browse (repoUrl )
139136
140- return printExit ( response , cmd , opts , repoUrl )
137+ return nil
141138}
142139
143- func addCombined (opts * BrowseOptions , url string , branchName string ) (exitCode , string ) {
140+ func addCombined (opts * BrowseOptions , url string , branchName string ) (error , string ) {
141+ help := "Use 'gh browse --help' for more information about browse\n "
144142
145143 if ! opts .BranchFlag {
146- return exitInvalidCombo , ""
144+ return fmt . Errorf ( "invalid use of flag and argument \n %s" , help ) , ""
147145 }
148146
149147 if opts .AdditionalArg == "" {
150- return exitUrlSuccess , url + "/tree/" + opts .SelectorArg
148+ url += "/tree/" + opts .SelectorArg
149+ fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
150+ return nil , url
151151 }
152152
153153 arr := parseFileArg (opts )
154154 if len (arr ) > 1 {
155- return exitUrlSuccess , url + "/tree/" + opts .AdditionalArg + "/" + arr [0 ] + "#L" + arr [1 ]
155+ url += "/tree/" + opts .SelectorArg
156+ fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
157+ return nil , url
156158 }
157159
158- return exitUrlSuccess , url + "/tree/" + opts .AdditionalArg + "/" + arr [0 ]
160+ url += "/tree/" + opts .SelectorArg
161+ fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
162+ return nil , url
159163}
160164
161- func addFlag (opts * BrowseOptions , url string ) (exitCode , string ) {
165+ func addFlag (opts * BrowseOptions , url string ) (error , string ) {
162166 if opts .ProjectsFlag {
163- return exitUrlSuccess , url + "/projects"
167+ url += "/projects"
164168 } else if opts .SettingsFlag {
165- return exitUrlSuccess , url + "/settings"
169+ url += "/settings"
166170 } else if opts .WikiFlag {
167- return exitUrlSuccess , url + "/wiki"
171+ url += "/wiki"
168172 }
169- return exitExpectedArg , ""
173+ fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
174+ return nil , url
170175}
171176
172- func addArg (opts * BrowseOptions , url string , branchName string ) (exitCode , string ) {
177+ func addArg (opts * BrowseOptions , url string , branchName string ) (error , string ) {
178+ help := "Use 'gh browse --help' for more information about browse\n "
173179
174180 if opts .AdditionalArg != "" {
175- return exitTooManyArgs , ""
181+ return fmt . Errorf ( "accepts 1 arg, 2 arg(s) were recieved \n %s" , help ) , ""
176182 }
177183
178184 if isNumber (opts .SelectorArg ) {
179185 url += "/issues/" + opts .SelectorArg
180- return exitNonUrlSuccess , url
186+ fmt .Fprintf (opts .IO .Out , "now opening issue/pr in browser . . .\n " )
187+ return nil , url
181188 }
182189
183190 arr := parseFileArg (opts )
184191 if len (arr ) > 1 {
185- return exitUrlSuccess , url + "/tree/" + branchName + "/" + arr [0 ] + "#L" + arr [1 ]
192+ fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
193+ return nil , url + "/tree/" + branchName + "/" + arr [0 ] + "#L" + arr [1 ]
186194 }
187195
188- return exitUrlSuccess , url + "/tree/" + branchName + "/" + arr [0 ]
196+ fmt .Fprintf (opts .IO .Out , "now opening %s in browser . . .\n " , url )
197+ return nil , url + "/tree/" + branchName + "/" + arr [0 ]
189198}
190199
191200func parseFileArg (opts * BrowseOptions ) []string {
192201 arr := strings .Split (opts .SelectorArg , ":" )
193202 return arr
194203}
195204
196- func printExit (exit exitCode , cmd * cobra.Command , opts * BrowseOptions , url string ) error {
197- w := opts .IO .ErrOut
198- cs := opts .IO .ColorScheme ()
199- help := "Use 'gh browse --help' for more information about browse\n "
200-
201- switch exit {
202- case exitUrlSuccess :
203- //fmt.Fprintf(w, "now opening %s in browser . . .\n", cs.Bold(url))
204- fmt .Fprintln (w , "Hello World One" )
205- case exitNonUrlSuccess :
206- //fmt.Fprintf(w, "now opening issue/pr in browser . . .\n")
207- fmt .Fprintln (w , "Hello World Two" )
208- case exitNotInRepo :
209- return fmt .Errorf ("change directory to a repository to open in browser\n %s" , help )
210- case exitTooManyFlags :
211- return fmt .Errorf ("accepts 1 flag, %d flag(s) were recieved\n %s" , getFlagAmount (cmd ), help )
212- case exitTooManyArgs :
213- return fmt .Errorf ("accepts 1 arg, 2 arg(s) were received \n %s" , help )
214- case exitExpectedArg :
215- return fmt .Errorf ("expected argument with this flag %s\n %s" , cs .Bold (url ), help )
216- case exitInvalidCombo :
217- return fmt .Errorf ("invalid use of flag and argument\n %s" , help )
218- }
219- return nil
220- }
221-
222205func hasFlag (cmd * cobra.Command ) bool {
223206 return getFlagAmount (cmd ) > 0
224207}
@@ -231,10 +214,6 @@ func getFlagAmount(cmd *cobra.Command) int {
231214 return cmd .Flags ().NFlag ()
232215}
233216
234- func inRepo (err error ) bool {
235- return err == nil
236- }
237-
238217func isNumber (arg string ) bool {
239218 _ , err := strconv .Atoi (arg )
240219 if err != nil {
0 commit comments