Skip to content

Commit f27b47f

Browse files
authored
Merge pull request cli#31 from jlsestak/trunk
Updated errors
2 parents ed87cf7 + 6f09e4f commit f27b47f

File tree

1 file changed

+42
-49
lines changed

1 file changed

+42
-49
lines changed

pkg/cmd/browse/browse.go

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ type BrowseOptions struct {
3737
type exitCode int
3838

3939
const (
40-
exitSuccess exitCode = 0
41-
exitNotInRepo exitCode = 1
42-
exitTooManyFlags exitCode = 2
43-
exitTooManyArgs exitCode = 3
44-
exitExpectedArg exitCode = 4
45-
exitInvalidCombo exitCode = 5
40+
exitUrlSuccess exitCode = 0
41+
exitNonUrlSuccess exitCode = 1
42+
exitNotInRepo exitCode = 2
43+
exitTooManyFlags exitCode = 3
44+
exitTooManyArgs exitCode = 4
45+
exitExpectedArg exitCode = 5
46+
exitInvalidCombo exitCode = 6
4647
)
4748

4849
func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
@@ -86,7 +87,7 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
8687
To configure a web browser other than the default, use the BROWSER environment variable
8788
`),
8889
},
89-
Run: func(cmd *cobra.Command, args []string) {
90+
RunE: func(cmd *cobra.Command, args []string) error {
9091

9192
if len(args) > 1 {
9293
opts.AdditionalArg = args[1]
@@ -95,7 +96,8 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
9596
if len(args) > 0 {
9697
opts.SelectorArg = args[0]
9798
}
98-
openInBrowser(cmd, opts)
99+
return openInBrowser(cmd, opts)
100+
99101
},
100102
}
101103
cmdutil.EnableRepoOverride(cmd, f)
@@ -107,25 +109,23 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
107109
return cmd
108110
}
109111

110-
func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) (exitCode, string) {
112+
func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) error {
111113

112114
baseRepo, err := opts.BaseRepo()
113115
httpClient, _ := opts.HttpClient()
114116
apiClient := api.NewClientFromHTTP(httpClient)
115117
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
116118

117-
if !inRepo(err) { // must be in a repo to execute
118-
printExit(exitNotInRepo, cmd, opts, "")
119-
return exitNotInRepo, ""
119+
if !inRepo(err) {
120+
return printExit(exitNotInRepo, cmd, opts, "")
120121
}
121122

122-
if getFlagAmount(cmd) > 1 { // command can't have more than one flag
123-
printExit(exitTooManyFlags, cmd, opts, "")
124-
return exitTooManyFlags, ""
123+
if getFlagAmount(cmd) > 1 {
124+
return printExit(exitTooManyFlags, cmd, opts, "")
125125
}
126126

127127
repoUrl := ghrepo.GenerateRepoURL(baseRepo, "")
128-
response := exitSuccess
128+
response := exitUrlSuccess
129129

130130
if !hasArg(opts) && hasFlag(cmd) {
131131
response, repoUrl = addFlag(opts, repoUrl)
@@ -135,40 +135,40 @@ func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) (exitCode, string) {
135135
response, repoUrl = addCombined(opts, repoUrl, branchName)
136136
}
137137

138-
if response == exitSuccess {
139-
opts.Browser.Browse(repoUrl) // otherwise open repo
138+
if response == exitUrlSuccess || response == exitNonUrlSuccess {
139+
opts.Browser.Browse(repoUrl)
140140
}
141141

142-
printExit(response, cmd, opts, repoUrl) // print success
143-
return response, repoUrl
142+
return printExit(response, cmd, opts, repoUrl)
143+
144144
}
145145

146146
func addCombined(opts *BrowseOptions, url string, branchName string) (exitCode, string) {
147147

148-
if !opts.BranchFlag { // gh browse --settings main.go
148+
if !opts.BranchFlag {
149149
return exitInvalidCombo, ""
150150
}
151151

152152
if opts.AdditionalArg == "" {
153-
return exitSuccess, url + "/tree/" + opts.SelectorArg
153+
return exitUrlSuccess, url + "/tree/" + opts.SelectorArg
154154
}
155155

156156
arr := parseFileArg(opts)
157157
if len(arr) > 1 {
158-
return exitSuccess, url + "/tree/" + opts.AdditionalArg + "/" + arr[0] + "#L" + arr[1]
158+
return exitUrlSuccess, url + "/tree/" + opts.AdditionalArg + "/" + arr[0] + "#L" + arr[1]
159159
}
160160

161-
return exitSuccess, url + "/tree/" + opts.AdditionalArg + "/" + arr[0]
161+
return exitUrlSuccess, url + "/tree/" + opts.AdditionalArg + "/" + arr[0]
162162

163163
}
164164

165165
func addFlag(opts *BrowseOptions, url string) (exitCode, string) {
166166
if opts.ProjectsFlag {
167-
return exitSuccess, url + "/projects"
167+
return exitUrlSuccess, url + "/projects"
168168
} else if opts.SettingsFlag {
169-
return exitSuccess, url + "/settings"
169+
return exitUrlSuccess, url + "/settings"
170170
} else if opts.WikiFlag {
171-
return exitSuccess, url + "/wiki"
171+
return exitUrlSuccess, url + "/wiki"
172172
}
173173
return exitExpectedArg, "" // Flag is a branch and needs an argument
174174
}
@@ -181,53 +181,46 @@ func addArg(opts *BrowseOptions, url string, branchName string) (exitCode, strin
181181

182182
if isNumber(opts.SelectorArg) {
183183
url += "/issues/" + opts.SelectorArg
184-
return exitSuccess, url
184+
return exitNonUrlSuccess, url
185185
}
186186

187187
arr := parseFileArg(opts)
188188
if len(arr) > 1 {
189-
return exitSuccess, url + "/tree/" + branchName + "/" + arr[0] + "#L" + arr[1]
189+
return exitUrlSuccess, url + "/tree/" + branchName + "/" + arr[0] + "#L" + arr[1]
190190
}
191191

192-
return exitSuccess, url + "/tree/" + branchName + "/" + arr[0]
192+
return exitUrlSuccess, url + "/tree/" + branchName + "/" + arr[0]
193193
}
194194

195195
func parseFileArg(opts *BrowseOptions) []string {
196196
arr := strings.Split(opts.SelectorArg, ":")
197197
return arr
198198
}
199199

200-
func printExit(exit exitCode, cmd *cobra.Command, opts *BrowseOptions, url string) {
200+
func printExit(exit exitCode, cmd *cobra.Command, opts *BrowseOptions, url string) error {
201201
w := opts.IO.ErrOut
202202
cs := opts.IO.ColorScheme()
203203
help := "Use 'gh browse --help' for more information about browse\n"
204204

205205
switch exit {
206-
case exitSuccess:
207-
fmt.Fprintf(w, "%s now opening %s in browser . . .\n",
208-
cs.Green("✓"), cs.Bold(url))
206+
case exitUrlSuccess:
207+
fmt.Fprintf(w, "now opening %s in browser . . .\n", cs.Bold(url))
209208
break
210-
case exitNotInRepo:
211-
fmt.Fprintf(w, "%s change directory to a repository to open in browser\n%s",
212-
cs.Red("x"), help)
209+
case exitNonUrlSuccess:
210+
fmt.Fprintf(w, "now opening issue/pr in browser . . .\n")
213211
break
212+
case exitNotInRepo:
213+
return fmt.Errorf("change directory to a repository to open in browser\n%s", help)
214214
case exitTooManyFlags:
215-
fmt.Fprintf(w, "%s accepts 1 flag, %d flag(s) were recieved\n%s",
216-
cs.Red("x"), getFlagAmount(cmd), help)
217-
break
215+
return fmt.Errorf("accepts 1 flag, %d flag(s) were recieved\n%s", getFlagAmount(cmd), help)
218216
case exitTooManyArgs:
219-
fmt.Fprintf(w, "%s accepts 1 arg, 2 arg(s) were received \n%s",
220-
cs.Red("x"), help)
221-
break
217+
return fmt.Errorf("accepts 1 arg, 2 arg(s) were received \n%s", help)
222218
case exitExpectedArg:
223-
fmt.Fprintf(w, "%s expected argument with this flag %s\n%s",
224-
cs.Red("x"), cs.Bold(url), help)
225-
break
219+
return fmt.Errorf("expected argument with this flag %s\n%s", cs.Bold(url), help)
226220
case exitInvalidCombo:
227-
fmt.Fprintf(w, "%s invalid use of flag and argument\n%s",
228-
cs.Red("x"), help)
229-
break
221+
return fmt.Errorf("invalid use of flag and argument\n%s", help)
230222
}
223+
return nil
231224
}
232225

233226
func hasFlag(cmd *cobra.Command) bool {

0 commit comments

Comments
 (0)