Skip to content

Commit 97f8c0f

Browse files
committed
feat(conf/branchs): add branch enums in configuration which will replace builtin branch enums when it is configured.
1 parent efe729b commit 97f8c0f

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

cmd/gitlab-flow/support.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,23 @@ func getOpFeatureContext(c *cli.Context) *types.OpFeatureContext {
9292

9393
func getFlow(c *cli.Context) internal.IFlow {
9494
flags := parseGlobalFlags(c)
95-
ctx := setEnviron(flags)
95+
ctx := resolveFlags(flags)
9696
return internal.NewFlow(ctx)
9797
}
9898

9999
func getDash(c *cli.Context) internal.IDash {
100100
flags := parseGlobalFlags(c)
101-
ctx := setEnviron(flags)
101+
ctx := resolveFlags(flags)
102102
return internal.NewDash(ctx)
103103
}
104104

105-
// setEnviron set global environment of debug mode.
105+
// resolveFlags set global environment of debug mode.
106106
// DONE(@yeqown): apply project name from CLI and CWD.
107107
// DONE(@yeqown): CWD could be configured from CLI.
108-
func setEnviron(flags globalFlags) *types.FlowContext {
108+
func resolveFlags(flags globalFlags) *types.FlowContext {
109109
log.
110110
WithField("flags", flags).
111-
Debugf("setEnviron called")
111+
Debugf("resolveFlags called")
112112

113113
// get absolute path of current working directory.
114114
cwd, err := filepath.Abs(flags.CWD)
@@ -119,26 +119,28 @@ func setEnviron(flags globalFlags) *types.FlowContext {
119119
}
120120

121121
// prepare configuration
122-
cfg, err := conf.Load(flags.ConfPath, nil)
123-
if err != nil {
122+
var c *types.Config
123+
if c, err = conf.Load(flags.ConfPath, nil); err != nil {
124124
log.
125125
WithField("path", flags.ConfPath).
126126
Fatalf("could not load config file: %v", err)
127127
}
128128

129129
// pass flags parameters into configuration
130130
if flags.DebugMode {
131-
cfg.DebugMode = flags.DebugMode
131+
c.DebugMode = flags.DebugMode
132132
}
133133
if flags.OpenBrowser {
134-
cfg.OpenBrowser = flags.OpenBrowser
134+
c.OpenBrowser = flags.OpenBrowser
135135
}
136136

137-
if err = cfg.Valid(); err != nil {
137+
if err = c.Valid(); err != nil {
138138
log.
139-
WithField("cfg", cfg).
139+
WithField("config", c).
140140
Fatalf("config is invalid")
141141
}
142142

143-
return types.NewContext(cwd, flags.ConfPath, flags.ProjectName, cfg, flags.ForceRemote)
143+
types.SyncBranchSetting(c.Branch.Master, c.Branch.Dev, c.Branch.Test)
144+
145+
return types.NewContext(cwd, flags.ConfPath, flags.ProjectName, c, flags.ForceRemote)
144146
}

internal/conf/conf.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ func Load(confPath string, parser ConfigParser) (cfg *types.Config, err error) {
3535
var (
3636
r io.Reader
3737
)
38-
cfg = new(types.Config)
38+
cfg = &types.Config{
39+
OAuth: new(types.OAuth),
40+
Branch: new(types.BranchSetting),
41+
GitlabAPIURL: "",
42+
GitlabHost: "",
43+
DebugMode: false,
44+
OpenBrowser: false,
45+
}
3946
p := precheckConfigDirectory(confPath)
4047
r, err = os.OpenFile(p, os.O_RDONLY, 0777)
4148
if err != nil {
@@ -69,6 +76,11 @@ func Save(confPath string, cfg *types.Config, parser ConfigParser) error {
6976

7077
var (
7178
defaultConf = &types.Config{
79+
Branch: &types.BranchSetting{
80+
Master: types.MasterBranch,
81+
Dev: types.DevBranch,
82+
Test: types.TestBranch,
83+
},
7284
OAuth: &types.OAuth{
7385
AccessToken: "",
7486
RefreshToken: "",

internal/types/config.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ type OAuth struct {
1212
RefreshToken string `toml:"refresh_token"`
1313
}
1414

15+
// BranchSetting contains some personal setting of git branch.
16+
type BranchSetting struct {
17+
Master, Dev, Test BranchTyp
18+
}
19+
1520
// Config contains all fields can be specified by user.
1621
type Config struct {
17-
OAuth *OAuth
22+
OAuth *OAuth `toml:"oauth"`
23+
Branch *BranchSetting `toml:"branch"`
1824

1925
// Deprecated: use oauth as instead
2026
//AccessToken string `toml:"access_token"`

internal/types/enums.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@ func (b BranchTyp) String() string {
77
return string(b)
88
}
99

10-
// TODO(@yeqown) MasterBranch, DevBranch and TestBranch could be customized.
10+
// DONE(@yeqown) MasterBranch, DevBranch and TestBranch could be customized.
1111
var (
1212
MasterBranch BranchTyp = "master"
1313
DevBranch BranchTyp = "develop"
1414
TestBranch BranchTyp = "test"
1515
)
16+
17+
// SyncBranchSetting reset builtin branch enums manually.
18+
func SyncBranchSetting(master, dev, test BranchTyp) {
19+
if master != "" {
20+
MasterBranch = master
21+
}
22+
if dev != "" {
23+
DevBranch = dev
24+
}
25+
if test != "" {
26+
TestBranch = test
27+
}
28+
}

0 commit comments

Comments
 (0)