Skip to content

Commit ac3b8e0

Browse files
committed
feat: global flags supported projectName, openBrowser; optimism FlowContext usage;
1 parent 4990b3a commit ac3b8e0

File tree

10 files changed

+174
-129
lines changed

10 files changed

+174
-129
lines changed

cmd/gitlab-flow/commands_dash.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ func getDashFeatureDetailSubCommand() *cli.Command {
3535
},
3636
},
3737
Action: func(c *cli.Context) error {
38-
confPath := c.String("conf_path")
39-
debug := c.Bool("debug")
4038
featureBranchName := c.String("branch_name")
41-
data, err := getDash(confPath, debug).FeatureDetail(featureBranchName)
39+
data, err := getDash(c).FeatureDetail(featureBranchName)
4240
if err != nil {
4341
return err
4442
}
@@ -73,9 +71,7 @@ func getDashMilestoneOverviewSubCommand() *cli.Command {
7371
Action: func(c *cli.Context) error {
7472
milestoneName := c.String("milestone_name")
7573
filterBranchName := c.String("branch_name")
76-
confPath := c.String("conf_path")
77-
debug := c.Bool("debug")
78-
data, err := getDash(confPath, debug).MilestoneOverview(milestoneName, filterBranchName)
74+
data, err := getDash(c).MilestoneOverview(milestoneName, filterBranchName)
7975
if err != nil {
8076
return err
8177
}
@@ -94,9 +90,7 @@ func getDashProjectDetailSubCommand() *cli.Command {
9490
Category: "dash",
9591
//Flags: []cli.Flag{},
9692
Action: func(c *cli.Context) error {
97-
confPath := c.String("conf_path")
98-
debug := c.Bool("debug")
99-
data, err := getDash(confPath, debug).ProjectDetail()
93+
data, err := getDash(c).ProjectDetail()
10094
if err != nil {
10195
return err
10296
}

cmd/gitlab-flow/commands_feature.go

+7-22
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ func getFeatureBeginSubCommand() *cli.Command {
3939
if desc == "" {
4040
return errors.New("'Description' could not be empty")
4141
}
42-
debug := c.Bool("debug")
43-
confPath := c.String("conf_path")
44-
return getFlow(confPath, debug).FeatureBegin(title, desc)
42+
return getFlow(c).FeatureBegin(title, desc)
4543
},
4644
}
4745
}
@@ -72,11 +70,8 @@ func getFeatureBeginIssueSubCommand() *cli.Command {
7270

7371
issueTitle := c.Args().Get(0)
7472
issueDesc := c.Args().Get(1)
75-
76-
confPath := c.String("conf_path")
7773
featureBranchName := c.String("feature_branch_name")
78-
debug := c.Bool("debug")
79-
return getFlow(confPath, debug).FeatureBeginIssue(featureBranchName, issueTitle, issueDesc)
74+
return getFlow(c).FeatureBeginIssue(featureBranchName, issueTitle, issueDesc)
8075
},
8176
}
8277
}
@@ -108,9 +103,7 @@ func getFeatureFinishIssueSubCommand() *cli.Command {
108103
Action: func(c *cli.Context) error {
109104
featureBranchName := c.String("feature_branch_name")
110105
issueBranchName := c.String("issue_branch_name")
111-
confPath := c.String("conf_path")
112-
debug := c.Bool("debug")
113-
return getFlow(confPath, debug).FeatureFinishIssue(featureBranchName, issueBranchName)
106+
return getFlow(c).FeatureFinishIssue(featureBranchName, issueBranchName)
114107
},
115108
}
116109
}
@@ -132,9 +125,7 @@ func getFeatureDebugSubCommand() *cli.Command {
132125
},
133126
Action: func(c *cli.Context) error {
134127
featureBranchName := c.String("feature_branch_name")
135-
confPath := c.String("conf_path")
136-
debug := c.Bool("debug")
137-
return getFlow(confPath, debug).FeatureDebugging(featureBranchName)
128+
return getFlow(c).FeatureDebugging(featureBranchName)
138129
},
139130
}
140131
}
@@ -155,10 +146,8 @@ func getFeatureTestSubCommand() *cli.Command {
155146
},
156147
},
157148
Action: func(c *cli.Context) error {
158-
confPath := c.String("conf_path")
159149
featureBranchName := c.String("feature_branch_name")
160-
debug := c.Bool("debug")
161-
return getFlow(confPath, debug).FeatureTest(featureBranchName)
150+
return getFlow(c).FeatureTest(featureBranchName)
162151
},
163152
}
164153
}
@@ -179,10 +168,8 @@ func getFeatureReleaseSubCommand() *cli.Command {
179168
},
180169
},
181170
Action: func(c *cli.Context) error {
182-
confPath := c.String("conf_path")
183171
featureBranchName := c.String("feature_branch_name")
184-
debug := c.Bool("debug")
185-
return getFlow(confPath, debug).FeatureRelease(featureBranchName)
172+
return getFlow(c).FeatureRelease(featureBranchName)
186173
},
187174
}
188175
}
@@ -209,11 +196,9 @@ func getSyncMilestoneSubCommand() *cli.Command {
209196
},
210197
},
211198
Action: func(c *cli.Context) error {
212-
confPath := c.String("conf_path")
213199
milestoneID := c.Int("milestoneID")
214-
debug := c.Bool("debug")
215200
interact := c.Bool("interact")
216-
return getFlow(confPath, debug).SyncMilestone(milestoneID, interact)
201+
return getFlow(c).SyncMilestone(milestoneID, interact)
217202
},
218203
}
219204
}

cmd/gitlab-flow/commands_hotfix.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ func getHotfixStartSubCommand() *cli.Command {
3737
if desc == "" {
3838
return errors.New("desc could not be empty")
3939
}
40-
confPath := c.String("conf_path")
41-
debug := c.Bool("debug")
42-
return getFlow(confPath, debug).HotfixBegin(title, desc)
40+
return getFlow(c).HotfixBegin(title, desc)
4341
},
4442
}
4543
}
@@ -68,11 +66,8 @@ func getHotfixFinishSubCommand() *cli.Command {
6866
WithFields(log.Fields{"args": c.Args().Slice()}).
6967
Debug("finish hotfix")
7068
}()
71-
7269
hotfixBranchName := c.String("hotfix_branch_name")
73-
confPath := c.String("conf_path")
74-
debug := c.Bool("debug")
75-
return getFlow(confPath, debug).HotfixFinish(hotfixBranchName)
70+
return getFlow(c).HotfixFinish(hotfixBranchName)
7671
},
7772
}
7873
}

cmd/gitlab-flow/main.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package main
33
import (
44
"os"
55

6-
"github.com/yeqown/gitlab-flow/internal/conf"
7-
86
"github.com/urfave/cli/v2"
97
"github.com/yeqown/log"
108
)
@@ -22,7 +20,7 @@ func main() {
2220
}
2321
app.Version = "v1.5.1"
2422
app.Description = `A tool for managing gitlab Feature/Milestone/Issue/MergeRequest as gitlab-flow.`
25-
app.Flags = _globalFlags
23+
app.Flags = _cliGlobalFlags
2624

2725
mountCommands(app)
2826

@@ -39,23 +37,3 @@ func mountCommands(app *cli.App) {
3937
getDashCommand(),
4038
}
4139
}
42-
43-
// _globalFlags should be used like this:
44-
// flow --debug -c path/to/config SUB-COMMAND [...options]
45-
var _globalFlags = []cli.Flag{
46-
&cli.StringFlag{
47-
Name: "conf_path",
48-
Aliases: []string{"c"},
49-
Value: conf.DefaultConfPath(),
50-
DefaultText: "~/.gitlab-flow",
51-
Usage: "choose which `path/to/file` to load",
52-
Required: false,
53-
},
54-
&cli.BoolFlag{
55-
Name: "debug",
56-
Value: false,
57-
Usage: "--debug",
58-
DefaultText: "false",
59-
Required: false,
60-
},
61-
}

cmd/gitlab-flow/support.go

+76-22
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,111 @@ package main
33
import (
44
"os"
55

6+
"github.com/urfave/cli/v2"
7+
68
"github.com/yeqown/gitlab-flow/internal"
79
"github.com/yeqown/gitlab-flow/internal/conf"
810
"github.com/yeqown/gitlab-flow/internal/types"
911

1012
"github.com/yeqown/log"
1113
)
1214

13-
func getFlow(confPath string, debug bool) internal.IFlow {
14-
cfg := setEnviron(confPath, debug)
15-
// DONE(@yeqown) get cwd correctly.
16-
cwd, _ := os.Getwd()
17-
ctx := types.NewContext(cwd, confPath, cfg)
15+
// _cliGlobalFlags should be used like this:
16+
// flow --debug -c path/to/config SUB-COMMAND [...options]
17+
var _cliGlobalFlags = []cli.Flag{
18+
&cli.StringFlag{
19+
Name: "conf_path",
20+
Aliases: []string{"c"},
21+
Value: conf.DefaultConfPath(),
22+
DefaultText: "~/.gitlab-flow",
23+
Usage: "choose which `path/to/file` to load",
24+
Required: false,
25+
},
26+
&cli.BoolFlag{
27+
Name: "debug",
28+
Value: false,
29+
Usage: "verbose mode",
30+
DefaultText: "false",
31+
Required: false,
32+
},
33+
&cli.StringFlag{
34+
Name: "project",
35+
Aliases: []string{"p"},
36+
Value: "",
37+
DefaultText: "extract working directory",
38+
Usage: "input `projectName` to locate which project should be operate.",
39+
Required: false,
40+
},
41+
&cli.BoolFlag{
42+
Name: "web",
43+
Value: false,
44+
Usage: "open web browser automatically or not",
45+
DefaultText: "false",
46+
Required: false,
47+
},
48+
}
49+
50+
type globalFlags struct {
51+
ConfPath string
52+
DebugMode bool
53+
ProjectName string
54+
OpenBrowser bool
55+
}
56+
57+
func parseGlobalFlags(c *cli.Context) globalFlags {
58+
return globalFlags{
59+
ConfPath: c.String("conf_path"),
60+
DebugMode: c.Bool("debug"),
61+
ProjectName: c.String("project"),
62+
OpenBrowser: c.Bool("web"),
63+
}
64+
}
65+
66+
func getFlow(c *cli.Context) internal.IFlow {
67+
flags := parseGlobalFlags(c)
68+
ctx := setEnviron(flags)
1869
return internal.NewFlow(ctx)
1970
}
2071

21-
func getDash(confPath string, debug bool) internal.IDash {
22-
cfg := setEnviron(confPath, debug)
23-
cwd, _ := os.Getwd()
24-
ctx := types.NewContext(cwd, confPath, cfg)
72+
func getDash(c *cli.Context) internal.IDash {
73+
flags := parseGlobalFlags(c)
74+
ctx := setEnviron(flags)
2575
return internal.NewDash(ctx)
2676
}
2777

2878
// setEnviron set global environment of debug mode.
29-
func setEnviron(confPath string, debug bool) *types.Config {
30-
if !debug {
79+
// DONE(@yeqown): apply project name from CLI and CWD.
80+
// TODO(@yeqown): CWD could be configured from CLI.
81+
func setEnviron(flags globalFlags) *types.FlowContext {
82+
if !flags.DebugMode {
3183
log.SetLogLevel(log.LevelInfo)
3284
} else {
3385
// open caller report
3486
log.SetCallerReporter(true)
3587
log.SetLogLevel(log.LevelDebug)
3688
}
37-
3889
log.
39-
WithFields(log.Fields{
40-
"confPath": confPath,
41-
"debug": debug,
42-
}).
90+
WithField("flags", flags).
4391
Debugf("setEnviron called")
4492

45-
cfg, err := conf.Load(confPath, nil)
93+
// prepare configuration
94+
cfg, err := conf.Load(flags.ConfPath, nil)
4695
if err != nil {
4796
log.
48-
Fatalf("could not load config file from %s", confPath)
97+
WithField("path", flags.ConfPath).
98+
Fatalf("could not load config file")
4999
panic("could not reach")
50100
}
51-
if err = cfg.Debug(debug).Valid(); err != nil {
101+
if err = cfg.
102+
Apply(flags.DebugMode, flags.OpenBrowser).
103+
Valid(); err != nil {
52104
log.
53-
WithField("config", cfg).
54-
Fatalf("config is invalid: %s", confPath)
105+
WithField("cfg", cfg).
106+
Fatalf("config is invalid")
55107
panic("could not reach")
56108
}
57109

58-
return cfg
110+
// generate a FlowContext
111+
cwd, _ := os.Getwd()
112+
return types.NewContext(cwd, flags.ConfPath, flags.ProjectName, cfg)
59113
}

internal/dash_impl.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewDash(ctx *types.FlowContext) IDash {
3737

3838
dash := dashImpl{
3939
ctx: ctx,
40-
repo: impl.NewBasedSqlite3(impl.ConnectDB(ctx.ConfPath, ctx.Conf.DebugMode)),
40+
repo: impl.NewBasedSqlite3(impl.ConnectDB(ctx.ConfPath(), ctx.Conf.DebugMode)),
4141
gitOperator: gitop.NewBasedCmd(ctx.CWD),
4242
}
4343

@@ -68,8 +68,8 @@ func init() {
6868

6969
func (d dashImpl) fillContextWithProject() {
7070
// DONE(@yeqown): fill project information from local repository or remote gitlab repository.
71-
// FIXME(@yeqown): projectName would be different from project path, use git repository name as project name.
72-
projectName := extractProjectNameFromCWD(d.ctx.CWD)
71+
// DONE(@yeqown): projectName would be different from project path, use git repository name as project name.
72+
projectName := d.ctx.ProjectName()
7373
project := new(types.ProjectBasics)
7474
project.Name = projectName
7575

internal/flow.go

-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package internal
22

33
import (
44
"fmt"
5-
"path/filepath"
65
"regexp"
76
"strconv"
87
"strings"
@@ -47,12 +46,6 @@ type IFlow interface {
4746
SyncMilestone(milestoneID int, interact bool) error
4847
}
4948

50-
// extractProjectNameFromCWD get project name from current working directory.
51-
func extractProjectNameFromCWD(cwd string) string {
52-
splited := strings.Split(cwd, string(filepath.Separator))
53-
return splited[len(splited)-1]
54-
}
55-
5649
var (
5750
_closePattern = "[Cc]loses? #(\\d+)"
5851

0 commit comments

Comments
 (0)