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

Commit acc04f5

Browse files
Move merge request flag impl
1 parent 065bd8b commit acc04f5

File tree

2 files changed

+131
-126
lines changed

2 files changed

+131
-126
lines changed

commands/mr/flag.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package mr
2+
3+
import (
4+
flags "github.com/jessevdk/go-flags"
5+
"github.com/lighttiger2505/lab/commands/internal"
6+
)
7+
8+
type Option struct {
9+
ProjectProfileOption *internal.ProjectProfileOption `group:"Project, Profile Options"`
10+
CreateUpdateOption *CreateUpdateOption `group:"Create, Update Options"`
11+
ListOption *ListOption `group:"List Options"`
12+
ShowOption *ShowOption `group:"Show Options"`
13+
BrowseOption *BrowseOption `group:"Browse Options"`
14+
}
15+
16+
type CreateUpdateOption struct {
17+
Edit bool `short:"e" long:"edit" description:"Edit the merge request on editor. Start the editor with the contents in the given title and message options."`
18+
Title string `short:"i" long:"title" value-name:"<title>" description:"The title of an merge request"`
19+
Message string `short:"m" long:"message" value-name:"<message>" description:"The message of an merge request"`
20+
Template string `short:"p" long:"template" value-name:"<merge request template>" description:"The template of an merge request"`
21+
SourceBranch string `long:"source" value-name:"<source branch>" description:"The source branch"`
22+
TargetBranch string `long:"target" value-name:"<target branch>" default:"master" default-mask:"master" description:"The target branch"`
23+
StateEvent string `long:"state-event" value-name:"<state>" description:"Change the status. \"opened\", \"closed\""`
24+
AssigneeID int `long:"cu-assignee-id" value-name:"<assignee id>" description:"The ID of assignee."`
25+
MilestoneID int `long:"cu-milestone-id" value-name:"<milestone id>" description:"The ID of milestone."`
26+
}
27+
28+
func (o *CreateUpdateOption) hasEdit() bool {
29+
if o.Edit {
30+
return true
31+
}
32+
return false
33+
}
34+
35+
func (o *CreateUpdateOption) hasCreate() bool {
36+
if o.Title != "" ||
37+
o.AssigneeID != 0 ||
38+
o.MilestoneID != 0 {
39+
return true
40+
}
41+
return false
42+
}
43+
44+
func (o *CreateUpdateOption) hasUpdate() bool {
45+
if o.Title != "" ||
46+
o.Message != "" ||
47+
o.StateEvent != "" ||
48+
o.AssigneeID != 0 ||
49+
o.MilestoneID != 0 {
50+
return true
51+
}
52+
return false
53+
}
54+
55+
type ListOption struct {
56+
Num int `short:"n" long:"num" value-name:"<num>" default:"20" default-mask:"20" description:"Limit the number of merge request to output."`
57+
State string `long:"state" value-name:"<state>" default:"all" default-mask:"all" description:"Print only merge request of the state just those that are \"opened\", \"closed\", \"merged\" or \"all\""`
58+
Scope string `long:"scope" value-name:"<scope>" default:"all" default-mask:"all" description:"Print only given scope. \"created-by-me\", \"assigned-to-me\" or \"all\"."`
59+
OrderBy string `long:"orderby" value-name:"<orderby>" default:"updated_at" default-mask:"updated_at" description:"Print merge request ordered by \"created_at\" or \"updated_at\" fields."`
60+
Sort string `long:"sort" value-name:"<sort>" default:"desc" default-mask:"desc" description:"Print merge request ordered in \"asc\" or \"desc\" order."`
61+
Search string `short:"s" long:"search" value-name:"<search word>" description:"Search merge request against their title and description."`
62+
Milestone string `long:"milestone" value-name:"<milestone>" description:"lists merge request that have an assigned milestone."`
63+
AuthorID int `long:"author-id" value-name:"<auther id>" description:"lists merge request that have an author id."`
64+
AssigneeID int `long:"assignee-id" value-name:"<assignee id>" description:"lists merge request that have an assignee id."`
65+
Opened bool `short:"o" long:"opened" description:"Shorthand of the state option for \"--state=opened\"."`
66+
Closed bool `short:"c" long:"closed" description:"Shorthand of the state option for \"--state=closed\"."`
67+
Merged bool `short:"g" long:"merged" description:"Shorthand of the state option for \"--state=merged\"."`
68+
CreatedMe bool `short:"r" long:"created-me" description:"Shorthand of the scope option for \"--scope=created-by-me\"."`
69+
AssignedMe bool `short:"a" long:"assigned-me" description:"Shorthand of the scope option for \"--scope=assigned-by-me\"."`
70+
AllProject bool `short:"A" long:"all-project" description:"Print the merge request of all projects"`
71+
}
72+
73+
func (l *ListOption) getState() string {
74+
if l.Opened {
75+
return "opened"
76+
}
77+
if l.Closed {
78+
return "closed"
79+
}
80+
if l.Merged {
81+
return "merged"
82+
}
83+
return l.State
84+
}
85+
86+
func (l *ListOption) getScope() string {
87+
if l.CreatedMe {
88+
return "created-by-me"
89+
}
90+
if l.AssignedMe {
91+
return "assigned-to-me"
92+
}
93+
return l.Scope
94+
}
95+
96+
type ShowOption struct {
97+
NoComment bool `long:"no-comment" description:"Not print a list of comments for a spcific merge request."`
98+
}
99+
100+
type BrowseOption struct {
101+
Browse bool `short:"b" long:"browse" description:"Browse merge request."`
102+
}
103+
104+
func newOptionParser(opt *Option) *flags.Parser {
105+
opt.ProjectProfileOption = &internal.ProjectProfileOption{}
106+
opt.CreateUpdateOption = &CreateUpdateOption{}
107+
opt.ListOption = &ListOption{}
108+
opt.BrowseOption = &BrowseOption{}
109+
parser := flags.NewParser(opt, flags.HelpFlag|flags.PassDoubleDash)
110+
parser.Usage = `merge-request - Create and Edit, list a merge request
111+
112+
Synopsis:
113+
# List merge request
114+
lab merge-request [-n <num>] -l [--state <state>] [--scope <scope>]
115+
[--orderby <orderby>] [--sort <sort>] -o -c -g
116+
-r -a -A
117+
118+
# Create merge request
119+
lab merge-request [-e] [-i <title>] [-d <message>] [--assignee-id=<assignee id>]
120+
121+
# Update merge request
122+
lab merge-request <merge request iid> [-t <title>] [-d <description>] [--state-event=<state>] [--assignee-id=<assignee id>]
123+
124+
# Show merge request
125+
lab merge-request <mergerequest iid>
126+
127+
# Browse merge request
128+
lab merge-request -b [<mergerequest iid>]`
129+
130+
return parser
131+
}

commands/mr/mr.go

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"strconv"
77

8-
flags "github.com/jessevdk/go-flags"
98
"github.com/lighttiger2505/lab/commands/internal"
109
"github.com/lighttiger2505/lab/git"
1110
lab "github.com/lighttiger2505/lab/gitlab"
@@ -20,131 +19,6 @@ const (
2019
ExitCodeFileError int = iota //2
2120
)
2221

23-
type CreateUpdateOption struct {
24-
Edit bool `short:"e" long:"edit" description:"Edit the merge request on editor. Start the editor with the contents in the given title and message options."`
25-
Title string `short:"i" long:"title" value-name:"<title>" description:"The title of an merge request"`
26-
Message string `short:"m" long:"message" value-name:"<message>" description:"The message of an merge request"`
27-
Template string `short:"p" long:"template" value-name:"<merge request template>" description:"The template of an merge request"`
28-
SourceBranch string `long:"source" value-name:"<source branch>" description:"The source branch"`
29-
TargetBranch string `long:"target" value-name:"<target branch>" default:"master" default-mask:"master" description:"The target branch"`
30-
StateEvent string `long:"state-event" value-name:"<state>" description:"Change the status. \"opened\", \"closed\""`
31-
AssigneeID int `long:"cu-assignee-id" value-name:"<assignee id>" description:"The ID of assignee."`
32-
MilestoneID int `long:"cu-milestone-id" value-name:"<milestone id>" description:"The ID of milestone."`
33-
}
34-
35-
func (o *CreateUpdateOption) hasEdit() bool {
36-
if o.Edit {
37-
return true
38-
}
39-
return false
40-
}
41-
42-
func (o *CreateUpdateOption) hasCreate() bool {
43-
if o.Title != "" ||
44-
o.AssigneeID != 0 ||
45-
o.MilestoneID != 0 {
46-
return true
47-
}
48-
return false
49-
}
50-
51-
func (o *CreateUpdateOption) hasUpdate() bool {
52-
if o.Title != "" ||
53-
o.Message != "" ||
54-
o.StateEvent != "" ||
55-
o.AssigneeID != 0 ||
56-
o.MilestoneID != 0 {
57-
return true
58-
}
59-
return false
60-
}
61-
62-
type ListOption struct {
63-
Num int `short:"n" long:"num" value-name:"<num>" default:"20" default-mask:"20" description:"Limit the number of merge request to output."`
64-
State string `long:"state" value-name:"<state>" default:"all" default-mask:"all" description:"Print only merge request of the state just those that are \"opened\", \"closed\", \"merged\" or \"all\""`
65-
Scope string `long:"scope" value-name:"<scope>" default:"all" default-mask:"all" description:"Print only given scope. \"created-by-me\", \"assigned-to-me\" or \"all\"."`
66-
OrderBy string `long:"orderby" value-name:"<orderby>" default:"updated_at" default-mask:"updated_at" description:"Print merge request ordered by \"created_at\" or \"updated_at\" fields."`
67-
Sort string `long:"sort" value-name:"<sort>" default:"desc" default-mask:"desc" description:"Print merge request ordered in \"asc\" or \"desc\" order."`
68-
Search string `short:"s" long:"search" value-name:"<search word>" description:"Search merge request against their title and description."`
69-
Milestone string `long:"milestone" value-name:"<milestone>" description:"lists merge request that have an assigned milestone."`
70-
AuthorID int `long:"author-id" value-name:"<auther id>" description:"lists merge request that have an author id."`
71-
AssigneeID int `long:"assignee-id" value-name:"<assignee id>" description:"lists merge request that have an assignee id."`
72-
Opened bool `short:"o" long:"opened" description:"Shorthand of the state option for \"--state=opened\"."`
73-
Closed bool `short:"c" long:"closed" description:"Shorthand of the state option for \"--state=closed\"."`
74-
Merged bool `short:"g" long:"merged" description:"Shorthand of the state option for \"--state=merged\"."`
75-
CreatedMe bool `short:"r" long:"created-me" description:"Shorthand of the scope option for \"--scope=created-by-me\"."`
76-
AssignedMe bool `short:"a" long:"assigned-me" description:"Shorthand of the scope option for \"--scope=assigned-by-me\"."`
77-
AllProject bool `short:"A" long:"all-project" description:"Print the merge request of all projects"`
78-
}
79-
80-
func (l *ListOption) getState() string {
81-
if l.Opened {
82-
return "opened"
83-
}
84-
if l.Closed {
85-
return "closed"
86-
}
87-
if l.Merged {
88-
return "merged"
89-
}
90-
return l.State
91-
}
92-
93-
func (l *ListOption) getScope() string {
94-
if l.CreatedMe {
95-
return "created-by-me"
96-
}
97-
if l.AssignedMe {
98-
return "assigned-to-me"
99-
}
100-
return l.Scope
101-
}
102-
103-
type ShowOption struct {
104-
NoComment bool `long:"no-comment" description:"Not print a list of comments for a spcific merge request."`
105-
}
106-
107-
type BrowseOption struct {
108-
Browse bool `short:"b" long:"browse" description:"Browse merge request."`
109-
}
110-
111-
type Option struct {
112-
ProjectProfileOption *internal.ProjectProfileOption `group:"Project, Profile Options"`
113-
CreateUpdateOption *CreateUpdateOption `group:"Create, Update Options"`
114-
ListOption *ListOption `group:"List Options"`
115-
ShowOption *ShowOption `group:"Show Options"`
116-
BrowseOption *BrowseOption `group:"Browse Options"`
117-
}
118-
119-
func newOptionParser(opt *Option) *flags.Parser {
120-
opt.ProjectProfileOption = &internal.ProjectProfileOption{}
121-
opt.CreateUpdateOption = &CreateUpdateOption{}
122-
opt.ListOption = &ListOption{}
123-
opt.BrowseOption = &BrowseOption{}
124-
parser := flags.NewParser(opt, flags.HelpFlag|flags.PassDoubleDash)
125-
parser.Usage = `merge-request - Create and Edit, list a merge request
126-
127-
Synopsis:
128-
# List merge request
129-
lab merge-request [-n <num>] -l [--state <state>] [--scope <scope>]
130-
[--orderby <orderby>] [--sort <sort>] -o -c -g
131-
-r -a -A
132-
133-
# Create merge request
134-
lab merge-request [-e] [-i <title>] [-d <message>] [--assignee-id=<assignee id>]
135-
136-
# Update merge request
137-
lab merge-request <merge request iid> [-t <title>] [-d <description>] [--state-event=<state>] [--assignee-id=<assignee id>]
138-
139-
# Show merge request
140-
lab merge-request <mergerequest iid>
141-
142-
# Browse merge request
143-
lab merge-request -b [<mergerequest iid>]`
144-
145-
return parser
146-
}
147-
14822
type MergeRequestCommand struct {
14923
UI ui.UI
15024
RemoteCollecter gitutil.Collecter

0 commit comments

Comments
 (0)