Skip to content

Commit 2bbd110

Browse files
committed
cmd/project_browse.go: Add --file and --branch options
@sding3 has requested the ability to browse to a specific file using a --file argument. In addition to this I think it is a good idea to also add a --branch argument so users can browse a file on a specific branch. Add --file and --branch options. Close #866 Signed-off-by: Prarit Bhargava <prarit@redhat.com>
1 parent 1282241 commit 2bbd110

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

cmd/project_browse.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
package cmd
22

33
import (
4+
"github.com/MakeNowJust/heredoc/v2"
45
"github.com/spf13/cobra"
56
lab "github.com/zaquestion/lab/internal/gitlab"
67
)
78

9+
var (
10+
projectFile string
11+
projectBranch string
12+
projectFileRef string
13+
)
14+
15+
func projectBrowseGetPath(webURL string, defaultBranch string, branch string, file string, ref string) (path string) {
16+
if branch == "" {
17+
branch = defaultBranch
18+
}
19+
20+
if ref != "" {
21+
path = webURL + "/-/tree/" + ref
22+
return path
23+
}
24+
25+
if file != "" {
26+
path = webURL + "/-/blob/" + branch + "/" + file
27+
} else {
28+
path = webURL + "/-/tree/" + branch
29+
}
30+
31+
return path
32+
}
33+
834
var projectBrowseCmd = &cobra.Command{
9-
Use: "browse [remote]",
10-
Aliases: []string{"b"},
11-
Short: "View project in a browser",
12-
Example: "lab project browse origin",
35+
Use: "browse [remote]",
36+
Aliases: []string{"b"},
37+
Short: "View project in a browser",
38+
Example: heredoc.Doc(`
39+
lab project browse origin
40+
lab project browse --file arch/x86/Makefile
41+
lab project b --ref ce697ccee1`),
1342
PersistentPreRun: labPersistentPreRun,
1443
Run: func(cmd *cobra.Command, args []string) {
1544
rn, _, err := parseArgsRemoteAndID(args)
@@ -22,13 +51,16 @@ var projectBrowseCmd = &cobra.Command{
2251
log.Fatal(err)
2352
}
2453

25-
err = browse(p.WebURL)
54+
err = browse(projectBrowseGetPath(p.WebURL, p.DefaultBranch, projectBranch, projectFile, projectFileRef))
2655
if err != nil {
2756
log.Fatal(err)
2857
}
2958
},
3059
}
3160

3261
func init() {
62+
projectBrowseCmd.Flags().StringVar(&projectFile, "file", "", "path to specified file")
63+
projectBrowseCmd.Flags().StringVar(&projectFileRef, "ref", "", "commit reference for file")
64+
projectBrowseCmd.Flags().StringVar(&projectBranch, "branch", "", "specific branch (overrides default branch)")
3365
projectCmd.AddCommand(projectBrowseCmd)
3466
}

cmd/project_browse_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,29 @@ func Test_projectBrowse(t *testing.T) {
1111
defer func() { browse = oldBrowse }()
1212

1313
browse = func(url string) error {
14-
require.Equal(t, "https://gitlab.com/zaquestion/test", url)
14+
require.Equal(t, "https://gitlab.com/zaquestion/test/-/tree/master", url)
1515
return nil
1616
}
1717

1818
projectBrowseCmd.Run(nil, []string{""})
1919
}
20+
21+
func Test_projectGetPath(t *testing.T) {
22+
defaultPath := projectBrowseGetPath("https://gitlab.com/zaquestion/test", "master", "", "", "")
23+
require.Equal(t, defaultPath, "https://gitlab.com/zaquestion/test/-/tree/master")
24+
}
25+
26+
func Test_projectGetPathAndFile(t *testing.T) {
27+
pathAndFile := projectBrowseGetPath("https://gitlab.com/zaquestion/test", "master", "", "README.md", "")
28+
require.Equal(t, pathAndFile, "https://gitlab.com/zaquestion/test/-/blob/master/README.md")
29+
}
30+
31+
func Test_projectGetPathAndFileAndBranch(t *testing.T) {
32+
pathAndFileAndBranch := projectBrowseGetPath("https://gitlab.com/zaquestion/test", "master", "newbranch", "README.md", "")
33+
require.Equal(t, pathAndFileAndBranch, "https://gitlab.com/zaquestion/test/-/blob/newbranch/README.md")
34+
}
35+
36+
func Test_projectGetPathAndRef(t *testing.T) {
37+
pathRef := projectBrowseGetPath("https://gitlab.com/zaquestion/test", "", "", "", "12345abcdef")
38+
require.Equal(t, pathRef, "https://gitlab.com/zaquestion/test/-/tree/12345abcdef")
39+
}

0 commit comments

Comments
 (0)