Skip to content

Commit cac372d

Browse files
authored
Merge pull request cli#48 from ravocean/trunk
Refactored browse.go
2 parents b08a6d2 + 21691c8 commit cac372d

File tree

2 files changed

+96
-88
lines changed

2 files changed

+96
-88
lines changed

pkg/cmd/browse/browse.go

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,13 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
7878
opts.SelectorArg = args[0]
7979
}
8080

81-
if err := cmdutil.MutuallyExclusive("cannot use --projects with --settings", opts.ProjectsFlag, opts.SettingsFlag); err != nil {
82-
return err
83-
}
84-
if err := cmdutil.MutuallyExclusive("cannot use --projects with --wiki", opts.ProjectsFlag, opts.WikiFlag); err != nil {
85-
return err
86-
}
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 {
81+
if err := cmdutil.MutuallyExclusive(
82+
"specify only one of `--branch`, `--projects`, `--wiki`, or `--settings`",
83+
opts.Branch != "",
84+
opts.WikiFlag,
85+
opts.SettingsFlag,
86+
opts.ProjectsFlag,
87+
); err != nil {
9788
return err
9889
}
9990

@@ -127,54 +118,54 @@ func runBrowse(opts *BrowseOptions) error {
127118

128119
url := ghrepo.GenerateRepoURL(baseRepo, "")
129120

130-
if opts.ProjectsFlag {
131-
err := opts.Browser.Browse(url + "/projects")
132-
return err
133-
}
121+
if opts.SelectorArg == "" {
134122

135-
if opts.SettingsFlag {
136-
err := opts.Browser.Browse(url + "/settings")
137-
return err
138-
}
139-
140-
if opts.WikiFlag {
141-
err := opts.Browser.Browse(url + "/wiki")
142-
return err
143-
}
144-
145-
if isNumber(opts.SelectorArg) {
146-
url += "/issues/" + opts.SelectorArg
147-
err := opts.Browser.Browse(url)
148-
return err
149-
}
123+
if opts.ProjectsFlag {
124+
url += "/projects"
125+
}
150126

151-
if opts.Branch != "" {
152-
url += "/tree/" + opts.Branch + "/"
153-
} else {
154-
apiClient := api.NewClientFromHTTP(httpClient)
155-
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
156-
if err != nil {
157-
return err
127+
if opts.SettingsFlag {
128+
url += "/settings"
158129
}
159-
url += "/tree/" + branchName + "/"
160-
}
161130

162-
if opts.SelectorArg != "" {
163-
arr, err := parseFileArg(opts.SelectorArg)
164-
if err != nil {
165-
return err
131+
if opts.WikiFlag {
132+
url += "/wiki"
133+
}
134+
if opts.Branch != "" {
135+
url += "/tree/" + opts.Branch + "/"
166136
}
167-
if len(arr) > 1 {
168-
url += arr[0] + "#L" + arr[1]
137+
} else {
138+
if isNumber(opts.SelectorArg) {
139+
url += "/issues/" + opts.SelectorArg
169140
} else {
170-
url += arr[0]
141+
arr, err := parseFileArg(opts.SelectorArg)
142+
if err != nil {
143+
return err
144+
}
145+
if opts.Branch != "" {
146+
url += "/tree/" + opts.Branch + "/"
147+
} else {
148+
apiClient := api.NewClientFromHTTP(httpClient)
149+
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
150+
if err != nil {
151+
return err
152+
}
153+
url += "/tree/" + branchName + "/"
154+
}
155+
if opts.SelectorArg != "" {
156+
if len(arr) > 1 {
157+
url += arr[0] + "#L" + arr[1]
158+
} else {
159+
url += arr[0]
160+
}
161+
}
171162
}
172163
}
173-
174164
err = opts.Browser.Browse(url)
175165
if opts.IO.IsStdoutTTY() && err == nil {
176166
fmt.Fprintf(opts.IO.Out, "now opening %s in browser\n", url)
177167
}
168+
178169
return err
179170
}
180171
func parseFileArg(fileArg string) ([]string, error) {

pkg/cmd/browse/browse_test.go

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -138,83 +138,100 @@ func Test_runBrowse(t *testing.T) {
138138
opts: BrowseOptions{
139139
SelectorArg: "",
140140
},
141-
baseRepo: ghrepo.New("jessica", "cli"),
142-
defaultBranch: "trunk",
143-
expectedURL: "https://github.com/jessica/cli/tree/trunk/",
144-
},
145-
{
146-
name: "file argument",
147-
opts: BrowseOptions{SelectorArg: "path/to/file.txt"},
148-
baseRepo: ghrepo.New("ken", "cli"),
149-
defaultBranch: "main",
150-
expectedURL: "https://github.com/ken/cli/tree/main/path/to/file.txt",
151-
},
152-
{
153-
name: "branch flag",
154-
opts: BrowseOptions{
155-
Branch: "trunk",
156-
},
157-
baseRepo: ghrepo.New("thanh", "cli"),
158-
expectedURL: "https://github.com/thanh/cli/tree/trunk/",
141+
baseRepo: ghrepo.New("jlsestak", "cli"),
142+
expectedURL: "https://github.com/jlsestak/cli",
159143
},
160144
{
161145
name: "settings flag",
162146
opts: BrowseOptions{
163147
SettingsFlag: true,
164148
},
165-
baseRepo: ghrepo.New("bchadwic", "cli"),
166-
expectedURL: "https://github.com/bchadwic/cli/settings",
149+
baseRepo: ghrepo.New("bchadwic", "ObscuredByClouds"),
150+
expectedURL: "https://github.com/bchadwic/ObscuredByClouds/settings",
167151
},
168152
{
169153
name: "projects flag",
170154
opts: BrowseOptions{
171155
ProjectsFlag: true,
172156
},
173-
baseRepo: ghrepo.New("bchadwic", "cli"),
174-
expectedURL: "https://github.com/bchadwic/cli/projects",
157+
baseRepo: ghrepo.New("ttran112", "7ate9"),
158+
expectedURL: "https://github.com/ttran112/7ate9/projects",
175159
},
176160
{
177161
name: "wiki flag",
178162
opts: BrowseOptions{
179163
WikiFlag: true,
180164
},
181-
baseRepo: ghrepo.New("bchadwic", "cli"),
182-
expectedURL: "https://github.com/bchadwic/cli/wiki",
165+
baseRepo: ghrepo.New("ravocean", "ThreatLevelMidnight"),
166+
expectedURL: "https://github.com/ravocean/ThreatLevelMidnight/wiki",
167+
},
168+
{
169+
name: "file argument",
170+
opts: BrowseOptions{SelectorArg: "path/to/file.txt"},
171+
baseRepo: ghrepo.New("ken", "mrprofessor"),
172+
defaultBranch: "main",
173+
expectedURL: "https://github.com/ken/mrprofessor/tree/main/path/to/file.txt",
183174
},
184175
{
185176
name: "issue argument",
186177
opts: BrowseOptions{
187178
SelectorArg: "217",
188179
},
189-
baseRepo: ghrepo.New("bchadwic", "cli"),
190-
expectedURL: "https://github.com/bchadwic/cli/issues/217",
180+
baseRepo: ghrepo.New("kevin", "MinTy"),
181+
expectedURL: "https://github.com/kevin/MinTy/issues/217",
182+
},
183+
{
184+
name: "branch flag",
185+
opts: BrowseOptions{
186+
Branch: "trunk",
187+
},
188+
baseRepo: ghrepo.New("jlsestak", "vegan"),
189+
expectedURL: "https://github.com/jlsestak/vegan/tree/trunk/",
190+
},
191+
{
192+
name: "branch flag with file",
193+
opts: BrowseOptions{
194+
Branch: "trunk",
195+
SelectorArg: "main.go",
196+
},
197+
baseRepo: ghrepo.New("bchadwic", "LedZeppelinIV"),
198+
expectedURL: "https://github.com/bchadwic/LedZeppelinIV/tree/trunk/main.go",
191199
},
192200
{
193201
name: "file with line number",
194202
opts: BrowseOptions{
195203
SelectorArg: "path/to/file.txt:32",
196204
},
197-
baseRepo: ghrepo.New("bchadwic", "cli"),
205+
baseRepo: ghrepo.New("ravocean", "angur"),
198206
defaultBranch: "trunk",
199-
expectedURL: "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32",
207+
expectedURL: "https://github.com/ravocean/angur/tree/trunk/path/to/file.txt#L32",
200208
},
201209
{
202210
name: "file with invalid line number",
203211
opts: BrowseOptions{
204212
SelectorArg: "path/to/file.txt:32:32",
205213
},
206-
baseRepo: ghrepo.New("bchadwic", "cli"),
207-
defaultBranch: "trunk",
208-
wantsErr: true,
214+
baseRepo: ghrepo.New("ttran112", "ttrain211"),
215+
wantsErr: true,
209216
},
210217
{
211-
name: "file with line argument",
218+
name: "branch with issue number",
212219
opts: BrowseOptions{
213-
SelectorArg: "path/to/file.txt:32",
220+
SelectorArg: "217",
221+
Branch: "trunk",
214222
},
215-
baseRepo: ghrepo.New("bchadwic", "cli"),
216-
defaultBranch: "trunk",
217-
expectedURL: "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32",
223+
baseRepo: ghrepo.New("ken", "grc"),
224+
wantsErr: false,
225+
expectedURL: "https://github.com/ken/grc/issues/217",
226+
},
227+
{
228+
name: "opening branch",
229+
opts: BrowseOptions{
230+
Branch: "first-browse-pull",
231+
},
232+
baseRepo: ghrepo.New("github", "ThankYouGitHub"),
233+
wantsErr: false,
234+
expectedURL: "https://github.com/github/ThankYouGitHub/tree/first-browse-pull/",
218235
},
219236
}
220237

0 commit comments

Comments
 (0)