@@ -5,49 +5,31 @@ import (
55 "fmt"
66 "os"
77 "path/filepath"
8- "strings"
98
109 "github.com/atotto/clipboard"
1110 flags "github.com/jessevdk/go-flags"
1211 "github.com/lighttiger2505/lab/cmd"
12+ "github.com/lighttiger2505/lab/commands/internal"
1313 "github.com/lighttiger2505/lab/git"
1414 gitpath "github.com/lighttiger2505/lab/git/path"
15- lab "github.com/lighttiger2505/lab/gitlab "
15+ "github.com/lighttiger2505/lab/internal/gitutil "
1616 "github.com/lighttiger2505/lab/ui"
1717)
1818
19+ type BrowseCommandOption struct {
20+ ProjectProfileOption * internal.ProjectProfileOption `group:"Project, Profile Options"`
21+ BrowseOption * BrowseOption `group:"Browse Options"`
22+ }
23+
1924type BrowseOption struct {
2025 Subpage string `short:"s" long:"subpage" description:"open project sub page"`
2126 URL bool `short:"u" long:"url" description:"show project url"`
2227 Copy bool `short:"c" long:"copy" description:"copy project url to clipboard"`
23- Project string `short:"p" long:"project" description:"command target specific project"`
24- }
25-
26- func newBrowseOption () * BrowseOption {
27- browse := flags .NewNamedParser ("lab" , flags .Default )
28- browse .AddGroup ("Browse Options" , "" , & BrowseOption {})
29- return & BrowseOption {}
30- }
31-
32- func (g * BrowseOption ) NameSpaceAndProject () (group , subgroup , project string ) {
33- splited := strings .Split (g .Project , "/" )
34- if len (splited ) == 3 {
35- group = splited [0 ]
36- subgroup = splited [1 ]
37- project = splited [2 ]
38- return
39- }
40- group = splited [0 ]
41- project = splited [1 ]
42- return
43- }
44-
45- type BrowseCommandOption struct {
46- BrowseOption * BrowseOption `group:"Global Options"`
4728}
4829
4930func newBrowseOptionParser (opt * BrowseCommandOption ) * flags.Parser {
50- opt .BrowseOption = newBrowseOption ()
31+ opt .ProjectProfileOption = & internal.ProjectProfileOption {}
32+ opt .BrowseOption = & BrowseOption {}
5133 parser := flags .NewParser (opt , flags .HelpFlag | flags .PassDoubleDash )
5234 parser .Usage = `browse - Browse project page
5335
@@ -67,10 +49,10 @@ Synopsis:
6749}
6850
6951type BrowseCommand struct {
70- Ui ui.Ui
71- Provider lab. Provider
72- GitClient git.Client
73- Opener cmd.URLOpener
52+ Ui ui.Ui
53+ RemoteCollecter gitutil. Collecter
54+ GitClient git.Client
55+ Opener cmd.URLOpener
7456}
7557
7658func (c * BrowseCommand ) Synopsis () string {
@@ -79,50 +61,46 @@ func (c *BrowseCommand) Synopsis() string {
7961
8062func (c * BrowseCommand ) Help () string {
8163 buf := & bytes.Buffer {}
82- var browseCommnadOption BrowseCommandOption
83- browseOptionParser := newBrowseOptionParser (& browseCommnadOption )
64+ var opt BrowseCommandOption
65+ browseOptionParser := newBrowseOptionParser (& opt )
8466 browseOptionParser .WriteHelp (buf )
8567 return buf .String ()
8668}
8769
8870func (c * BrowseCommand ) Run (args []string ) int {
89- var browseCommnadOption BrowseCommandOption
90- browseOptionParser := newBrowseOptionParser (& browseCommnadOption )
91- // Parse option
71+ var opt BrowseCommandOption
72+ browseOptionParser := newBrowseOptionParser (& opt )
9273 parseArgs , err := browseOptionParser .ParseArgs (args )
9374 if err != nil {
9475 c .Ui .Error (err .Error ())
9576 return ExitCodeError
9677 }
9778
98- browseOption := browseCommnadOption .BrowseOption
99-
100- // Initialize provider
101- if err := c .Provider .Init (); err != nil {
102- c .Ui .Error (err .Error ())
103- return ExitCodeError
104- }
105-
106- // Getting git remote info
107- gitlabRemote , err := c .Provider .GetCurrentRemote ()
79+ pInfo , err := c .RemoteCollecter .CollectTarget (
80+ opt .ProjectProfileOption .Project ,
81+ opt .ProjectProfileOption .Profile ,
82+ )
10883 if err != nil {
10984 c .Ui .Error (err .Error ())
11085 return ExitCodeError
11186 }
112- if browseOption .Project != "" {
113- group , subgroup , project := browseOption .NameSpaceAndProject ()
114- gitlabRemote .Group = group
115- gitlabRemote .SubGroup = subgroup
116- gitlabRemote .Repository = project
117- }
11887
119- branch , err := c .GitClient .CurrentRemoteBranch (gitlabRemote )
88+ var branch = "master"
89+ isGitDir , err := git .IsGitDirReverseTop ()
12090 if err != nil {
12191 c .Ui .Error (err .Error ())
12292 return ExitCodeError
12393 }
94+ if isGitDir {
95+ branch , err = c .GitClient .CurrentRemoteBranch ()
96+ if err != nil {
97+ c .Ui .Error (err .Error ())
98+ return ExitCodeError
99+ }
100+ }
124101
125- url , err := c .getURL (parseArgs , gitlabRemote , branch , browseOption )
102+ browseOption := opt .BrowseOption
103+ url , err := c .getURL (parseArgs , pInfo , branch , browseOption )
126104 if err != nil {
127105 c .Ui .Error (err .Error ())
128106 return ExitCodeError
@@ -147,7 +125,7 @@ func (c *BrowseCommand) Run(args []string) int {
147125 return ExitCodeOK
148126}
149127
150- func (c * BrowseCommand ) getURL (args []string , remote * git. RemoteInfo , branch string , opt * BrowseOption ) (string , error ) {
128+ func (c * BrowseCommand ) getURL (args []string , pInfo * gitutil. GitLabProjectInfo , branch string , opt * BrowseOption ) (string , error ) {
151129 if len (args ) > 0 {
152130 arg := args [0 ]
153131 if ! isFilePath (arg ) {
@@ -173,20 +151,20 @@ func (c *BrowseCommand) getURL(args []string, remote *git.RemoteInfo, branch str
173151 }
174152
175153 if opt .Subpage != "" {
176- return remote .BranchFileWithLine (branch , gitAbsPath , opt .Subpage ), nil
154+ return pInfo .BranchFileWithLine (branch , gitAbsPath , opt .Subpage ), nil
177155 }
178- return remote .BranchPath (branch , gitAbsPath ), nil
156+ return pInfo .BranchPath (branch , gitAbsPath ), nil
179157 }
180158
181159 if opt .Subpage != "" {
182- return remote .Subpage (opt .Subpage ), nil
160+ return pInfo .Subpage (opt .Subpage ), nil
183161 }
184162
185163 // TODO You need to ignore the branch when the project is specified as an option
186164 if branch == "master" {
187- return remote .RepositoryUrl (), nil
165+ return pInfo .RepositoryUrl (), nil
188166 }
189- return remote .BranchUrl (branch ), nil
167+ return pInfo .BranchUrl (branch ), nil
190168}
191169
192170func isFilePath (value string ) bool {
0 commit comments