Skip to content

Commit 1657cf4

Browse files
committed
Fixed parsefileargs to throw errors with incorrect line numbers
1 parent a96612c commit 1657cf4

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

pkg/cmd/browse/browse.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,15 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
105105
}
106106

107107
func runBrowse(opts *BrowseOptions) error {
108-
help := "Use 'gh browse --help' for more information about browse\n"
109108

110109
baseRepo, err := opts.BaseRepo()
111110
if err != nil {
112-
return fmt.Errorf("unable to determine base repository: %w\n%s", err, help)
111+
return fmt.Errorf("unable to determine base repository: %w\nUse 'gh browse --help' for more information about browse\n", err)
113112
}
114113

115114
httpClient, err := opts.HttpClient()
116115
if err != nil {
117-
return fmt.Errorf("unable to create an http client: %w\n%s", err, help)
116+
return fmt.Errorf("unable to create an http client: %w\nUse 'gh browse --help' for more information about browse\n", err)
118117
}
119118

120119
apiClient := api.NewClientFromHTTP(httpClient)
@@ -129,9 +128,15 @@ func runBrowse(opts *BrowseOptions) error {
129128
hasFlag := opts.FlagAmount > 0
130129

131130
if opts.Branch != "" {
132-
repoUrl = addBranch(opts, repoUrl)
133-
} else if hasArg && !hasFlag || opts.RepoFlag {
134-
repoUrl = addArg(opts, repoUrl, branchName)
131+
err, repoUrl = addBranch(opts, repoUrl)
132+
if err != nil {
133+
return err
134+
}
135+
} else if hasArg && (!hasFlag || opts.RepoFlag) {
136+
err, repoUrl = addArg(opts, repoUrl, branchName)
137+
if err != nil {
138+
return err
139+
}
135140
} else if !hasArg && hasFlag {
136141
repoUrl = addFlag(opts, repoUrl)
137142
} else {
@@ -141,19 +146,22 @@ func runBrowse(opts *BrowseOptions) error {
141146
return nil
142147
}
143148

144-
func addBranch(opts *BrowseOptions, url string) string {
149+
func addBranch(opts *BrowseOptions, url string) (error, string) {
145150
if opts.SelectorArg == "" {
146151
url += "/tree/" + opts.Branch
147152
} else {
148-
arr := parseFileArg(opts)
153+
err, arr := parseFileArg(opts)
154+
if err != nil {
155+
return err, url
156+
}
149157
if len(arr) > 1 {
150158
url += "/tree/" + opts.Branch + "/" + arr[0] + "#L" + arr[1]
151159
} else {
152160
url += "/tree/" + opts.Branch + "/" + arr[0]
153161
}
154162
}
155163
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
156-
return url
164+
return nil, url
157165
}
158166

159167
func addFlag(opts *BrowseOptions, url string) string {
@@ -168,29 +176,34 @@ func addFlag(opts *BrowseOptions, url string) string {
168176
return url
169177
}
170178

171-
func addArg(opts *BrowseOptions, url string, branchName string) string {
179+
func addArg(opts *BrowseOptions, url string, branchName string) (error, string) {
172180
if isNumber(opts.SelectorArg) {
173181
url += "/issues/" + opts.SelectorArg
174182
fmt.Fprintf(opts.IO.Out, "now opening issue/pr in browser . . .\n")
175183
} else {
176-
arr := parseFileArg(opts)
177-
// if err != nil {
178-
179-
// }
184+
err, arr := parseFileArg(opts)
185+
if err != nil {
186+
return err, url
187+
}
180188
if len(arr) > 1 {
181189
url += "/tree/" + branchName + "/" + arr[0] + "#L" + arr[1]
182190
} else {
183191
url += "/tree/" + branchName + "/" + arr[0]
184192
}
185193
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
186194
}
187-
return url
195+
return nil, url
188196
}
189197

190-
func parseFileArg(opts *BrowseOptions) []string {
191-
// TODO make sure the line number is the second arg, and that there are only two elements in arr
198+
func parseFileArg(opts *BrowseOptions) (error, []string) {
192199
arr := strings.Split(opts.SelectorArg, ":")
193-
return arr
200+
if len(arr) > 2 {
201+
return fmt.Errorf("invalid use of colon\nUse 'gh browse --help' for more information about browse\n"), arr
202+
}
203+
if len(arr) > 1 && !isNumber(arr[1]) {
204+
return fmt.Errorf("nvalid line number after colon\nUse 'gh browse --help' for more information about browse\n"), arr
205+
}
206+
return nil, arr
194207
}
195208

196209
func isNumber(arg string) bool {

0 commit comments

Comments
 (0)