Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

Commit e7ba9d3

Browse files
Merge pull request #70 from lighttiger2505/update-new-config
Update new config
2 parents 892d851 + ba0ba66 commit e7ba9d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1064
-1749
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
NAME := lab
2-
VERSION := v0.5.4
2+
VERSION := v0.6.0
33
REVISION := $(shell git rev-parse --short HEAD)
44
GOVERSION := $(go version)
55

commands/browse.go

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1924
type 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

4930
func 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

6951
type 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

7658
func (c *BrowseCommand) Synopsis() string {
@@ -79,50 +61,46 @@ func (c *BrowseCommand) Synopsis() string {
7961

8062
func (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

8870
func (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

192170
func isFilePath(value string) bool {

commands/browse_test.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package commands
22

33
import (
4-
"io/ioutil"
5-
"os"
64
"testing"
75

8-
"github.com/lighttiger2505/lab/config"
96
"github.com/lighttiger2505/lab/git"
10-
"github.com/lighttiger2505/lab/gitlab"
7+
"github.com/lighttiger2505/lab/internal/gitutil"
118
"github.com/lighttiger2505/lab/ui"
129
)
1310

@@ -34,23 +31,11 @@ func (m *MockURLOpener) Open(url string) error {
3431

3532
func TestBrowseCommandRun(t *testing.T) {
3633
mockUI := ui.NewMockUi()
37-
38-
f, _ := ioutil.TempFile("", "test")
39-
tmppath := f.Name()
40-
f.Write([]byte(config.ConfigDataTest))
41-
f.Close()
42-
defer os.Remove(tmppath)
43-
configManager := config.NewConfigManagerPath(tmppath)
44-
45-
// Initialize provider
46-
provider := gitlab.NewProvider(mockUI, mockGitClient, configManager)
47-
provider.Init()
48-
4934
c := BrowseCommand{
50-
Ui: mockUI,
51-
Provider: provider,
52-
GitClient: mockGitClient,
53-
Opener: &MockURLOpener{},
35+
Ui: mockUI,
36+
RemoteCollecter: &gitutil.MockCollecter{},
37+
GitClient: mockGitClient,
38+
Opener: &MockURLOpener{},
5439
}
5540
args := []string{}
5641
if code := c.Run(args); code != 0 {

commands/internal/flag.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package internal
2+
3+
type ProjectProfileOption struct {
4+
Project string `long:"project" value-name:"<group>/<name>" description:"Specify the project to be processed"`
5+
Profile string `long:"profile" value-name:"<profile>" description:"Specify the profile defined in the config file"`
6+
}

commands/internal/internal.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package internal
22

3-
import "regexp"
3+
import (
4+
"regexp"
5+
"strings"
6+
)
47

58
type Method interface {
69
Process() (string, error)
@@ -16,3 +19,22 @@ func SweepMarkdownComment(text string) string {
1619
r := regexp.MustCompile("<!--[\\s\\S]*?-->[\\n]*")
1720
return r.ReplaceAllString(text, "")
1821
}
22+
23+
func ParceRepositoryFullName(webURL string) string {
24+
splitURL := strings.Split(webURL, "/")[3:]
25+
26+
subPageWords := []string{
27+
"issues",
28+
"merge_requests",
29+
}
30+
var subPageIndex int
31+
for i, word := range splitURL {
32+
for _, subPageWord := range subPageWords {
33+
if word == subPageWord {
34+
subPageIndex = i
35+
}
36+
}
37+
}
38+
39+
return strings.Join(splitURL[:subPageIndex], "/")
40+
}

commands/issue/browse.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
package issue
22

33
import (
4-
"fmt"
4+
"strconv"
5+
"strings"
56

67
"github.com/lighttiger2505/lab/cmd"
7-
"github.com/lighttiger2505/lab/git"
88
)
99

1010
type browseMethod struct {
1111
opener cmd.URLOpener
12-
remote *git.RemoteInfo
12+
url string
1313
id int
1414
}
1515

1616
func (m *browseMethod) Process() (string, error) {
17-
var subpage string
17+
url := m.url
1818
if m.id > 0 {
19-
subpage = fmt.Sprintf("issues/%d", m.id)
20-
} else {
21-
subpage = "issues"
19+
url = strings.Join([]string{url, strconv.Itoa(m.id)}, "/")
2220
}
2321

24-
url := m.remote.Subpage(subpage)
2522
if err := m.opener.Open(url); err != nil {
2623
return "", err
2724
}

commands/issue/browse_test.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"testing"
55

66
"github.com/lighttiger2505/lab/cmd"
7-
"github.com/lighttiger2505/lab/git"
87
)
98

109
func Test_browseMethod_Process(t *testing.T) {
1110
type fields struct {
1211
opener cmd.URLOpener
13-
remote *git.RemoteInfo
12+
url string
1413
id int
1514
}
1615
tests := []struct {
@@ -33,12 +32,8 @@ func Test_browseMethod_Process(t *testing.T) {
3332
return nil
3433
},
3534
},
36-
remote: &git.RemoteInfo{
37-
Domain: "domain",
38-
Group: "group",
39-
Repository: "repository",
40-
},
41-
id: 0,
35+
url: "https://domain/group/repository/issues",
36+
id: 0,
4237
},
4338
want: "",
4439
wantErr: false,
@@ -57,12 +52,8 @@ func Test_browseMethod_Process(t *testing.T) {
5752
return nil
5853
},
5954
},
60-
remote: &git.RemoteInfo{
61-
Domain: "domain",
62-
Group: "group",
63-
Repository: "repository",
64-
},
65-
id: 12,
55+
url: "https://domain/group/repository/issues",
56+
id: 12,
6657
},
6758
want: "",
6859
wantErr: false,
@@ -72,7 +63,7 @@ func Test_browseMethod_Process(t *testing.T) {
7263
t.Run(tt.name, func(t *testing.T) {
7364
m := &browseMethod{
7465
opener: tt.fields.opener,
75-
remote: tt.fields.remote,
66+
url: tt.fields.url,
7667
id: tt.fields.id,
7768
}
7869
got, err := m.Process()

0 commit comments

Comments
 (0)