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

Commit 4dedcba

Browse files
Clean project variable command
1 parent 2b9c621 commit 4dedcba

File tree

3 files changed

+38
-39
lines changed

3 files changed

+38
-39
lines changed

commands/project_variable.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ func newAddProjectVaribleOption() *CreateUpdateProjectVaribleOption {
7272
}
7373

7474
type ProjectVariableCommand struct {
75-
UI ui.Ui
76-
Provider lab.Provider
75+
UI ui.Ui
76+
Provider lab.Provider
77+
ClientFactory lab.APIClientFactory
7778
}
7879

7980
func (c *ProjectVariableCommand) Synopsis() string {
@@ -97,32 +98,31 @@ func (c *ProjectVariableCommand) Run(args []string) int {
9798
return ExitCodeError
9899
}
99100

100-
op := projectVaribaleOperation(opt, parseArgs)
101-
if err := validProjectVariableArgs(op, parseArgs); err != nil {
101+
if err := c.Provider.Init(); err != nil {
102102
c.UI.Error(err.Error())
103103
return ExitCodeError
104104
}
105105

106-
// Initialize provider
107-
if err := c.Provider.Init(); err != nil {
106+
gitlabRemote, err := c.Provider.GetCurrentRemote()
107+
if err != nil {
108108
c.UI.Error(err.Error())
109109
return ExitCodeError
110110
}
111111

112-
// Getting git remote info
113-
gitlabRemote, err := c.Provider.GetCurrentRemote()
112+
token, err := c.Provider.GetAPIToken(gitlabRemote)
114113
if err != nil {
115114
c.UI.Error(err.Error())
116115
return ExitCodeError
117116
}
118117

119-
client, err := c.Provider.GetProjectVariableClient(gitlabRemote)
120-
if err != nil {
118+
if err := c.ClientFactory.Init(gitlabRemote.ApiUrl(), token); err != nil {
121119
c.UI.Error(err.Error())
122120
return ExitCodeError
123121
}
122+
client := c.ClientFactory.GetProjectVariableClient()
124123

125124
// Do issue operation
125+
op := projectVaribaleOperation(opt, parseArgs)
126126
switch op {
127127
case CreateProjectVariable:
128128
_, err := client.CreateVariable(

commands/project_variable_test.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ func TestProjectVariableCommand_Run_List(t *testing.T) {
3434
}
3535
mockProvider := &lab.MockProvider{
3636
MockGetCurrentRemote: mockCurrentRemote,
37-
MockGetProjectVariableClient: func(remote *git.RemoteInfo) (lab.ProjectVariable, error) {
38-
return mockClient, nil
37+
}
38+
mockClientFactory := &lab.MockAPIClientFactory{
39+
MockGetProjectVariableClient: func() lab.ProjectVariable {
40+
return mockClient
3941
},
4042
}
4143
mockUI := ui.NewMockUi()
4244
c := ProjectVariableCommand{
43-
UI: mockUI,
44-
Provider: mockProvider,
45+
UI: mockUI,
46+
Provider: mockProvider,
47+
ClientFactory: mockClientFactory,
4548
}
4649

4750
// Do command
@@ -69,14 +72,17 @@ func TestProjectVariableCommand_Run_Create(t *testing.T) {
6972
}
7073
mockProvider := &lab.MockProvider{
7174
MockGetCurrentRemote: mockCurrentRemote,
72-
MockGetProjectVariableClient: func(remote *git.RemoteInfo) (lab.ProjectVariable, error) {
73-
return mockClient, nil
75+
}
76+
mockClientFactory := &lab.MockAPIClientFactory{
77+
MockGetProjectVariableClient: func() lab.ProjectVariable {
78+
return mockClient
7479
},
7580
}
7681
mockUI := ui.NewMockUi()
7782
c := ProjectVariableCommand{
78-
UI: mockUI,
79-
Provider: mockProvider,
83+
UI: mockUI,
84+
Provider: mockProvider,
85+
ClientFactory: mockClientFactory,
8086
}
8187

8288
// Do command
@@ -104,14 +110,17 @@ func TestProjectVariableCommand_Run_Update(t *testing.T) {
104110
}
105111
mockProvider := &lab.MockProvider{
106112
MockGetCurrentRemote: mockCurrentRemote,
107-
MockGetProjectVariableClient: func(remote *git.RemoteInfo) (lab.ProjectVariable, error) {
108-
return mockClient, nil
113+
}
114+
mockClientFactory := &lab.MockAPIClientFactory{
115+
MockGetProjectVariableClient: func() lab.ProjectVariable {
116+
return mockClient
109117
},
110118
}
111119
mockUI := ui.NewMockUi()
112120
c := ProjectVariableCommand{
113-
UI: mockUI,
114-
Provider: mockProvider,
121+
UI: mockUI,
122+
Provider: mockProvider,
123+
ClientFactory: mockClientFactory,
115124
}
116125

117126
// Do command
@@ -138,14 +147,17 @@ func TestProjectVariableCommand_Run_Remove(t *testing.T) {
138147
}
139148
mockProvider := &lab.MockProvider{
140149
MockGetCurrentRemote: mockCurrentRemote,
141-
MockGetProjectVariableClient: func(remote *git.RemoteInfo) (lab.ProjectVariable, error) {
142-
return mockClient, nil
150+
}
151+
mockClientFactory := &lab.MockAPIClientFactory{
152+
MockGetProjectVariableClient: func() lab.ProjectVariable {
153+
return mockClient
143154
},
144155
}
145156
mockUI := ui.NewMockUi()
146157
c := ProjectVariableCommand{
147-
UI: mockUI,
148-
Provider: mockProvider,
158+
UI: mockUI,
159+
Provider: mockProvider,
160+
ClientFactory: mockClientFactory,
149161
}
150162

151163
// Do command

gitlab/gitlab.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type Provider interface {
1515
Init() error
1616
GetCurrentRemote() (*git.RemoteInfo, error)
1717
GetAPIToken(remote *git.RemoteInfo) (string, error)
18-
GetProjectVariableClient(remote *git.RemoteInfo) (ProjectVariable, error)
1918
}
2019

2120
type GitlabProvider struct {
@@ -141,14 +140,6 @@ func (p *GitlabProvider) GetAPIToken(remote *git.RemoteInfo) (string, error) {
141140
return token, nil
142141
}
143142

144-
func (p *GitlabProvider) GetProjectVariableClient(remote *git.RemoteInfo) (ProjectVariable, error) {
145-
gitlabClient, err := p.makeGitLabClient(remote)
146-
if err != nil {
147-
return nil, err
148-
}
149-
return NewProjectVariableClient(gitlabClient), nil
150-
}
151-
152143
func (p *GitlabProvider) selectTargetRemote(remoteInfos []*git.RemoteInfo) (*git.RemoteInfo, error) {
153144
// Receive number of the domain of the remote repository to be searched from stdin
154145
p.UI.Message("That repository existing multi gitlab remote repository.")
@@ -230,10 +221,6 @@ func (m *MockProvider) GetAPIToken(remote *git.RemoteInfo) (string, error) {
230221
return "", nil
231222
}
232223

233-
func (m *MockProvider) GetProjectVariableClient(remote *git.RemoteInfo) (ProjectVariable, error) {
234-
return m.MockGetProjectVariableClient(remote)
235-
}
236-
237224
func getGitlabClient(url, token string) (*gitlab.Client, error) {
238225
client := gitlab.NewClient(nil, token)
239226
if err := client.SetBaseURL(url); err != nil {

0 commit comments

Comments
 (0)