Skip to content

Commit d8970cc

Browse files
committed
fix(parameter): dependson does not take effect
1 parent f1353f1 commit d8970cc

File tree

9 files changed

+102
-36
lines changed

9 files changed

+102
-36
lines changed

cmd/cz/cz.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ package cz
33
import (
44
"errors"
55
"fmt"
6+
"os"
7+
"path/filepath"
8+
"time"
69

710
cliflag "github.com/shipengqi/component-base/cli/flag"
811
"github.com/shipengqi/component-base/term"
912
"github.com/shipengqi/golib/convutil"
13+
"github.com/shipengqi/golib/fsutil"
14+
"github.com/shipengqi/log"
1015
"github.com/spf13/cobra"
1116

1217
"github.com/shipengqi/commitizen/internal/config"
@@ -19,6 +24,23 @@ func New() *cobra.Command {
1924
c := &cobra.Command{
2025
Use: "commitizen",
2126
Long: `Command line utility to standardize git commit messages.`,
27+
PreRun: func(cmd *cobra.Command, args []string) {
28+
opts := &log.Options{
29+
DisableRotate: true,
30+
DisableFileCaller: true,
31+
DisableConsoleCaller: true,
32+
DisableConsoleLevel: true,
33+
DisableConsoleTime: true,
34+
Output: fmt.Sprintf("%s/commitizen/logs", os.TempDir()),
35+
FileLevel: log.DebugLevel.String(),
36+
FilenameEncoder: filenameEncoder,
37+
}
38+
err := fsutil.MkDirAll(opts.Output)
39+
if err != nil {
40+
panic(err)
41+
}
42+
log.Configure(opts)
43+
},
2244
RunE: func(_ *cobra.Command, _ []string) error {
2345
isRepo, err := git.IsGitRepo()
2446
if err != nil {
@@ -80,3 +102,7 @@ func New() *cobra.Command {
80102

81103
return c
82104
}
105+
106+
func filenameEncoder() string {
107+
return fmt.Sprintf("%s.%s.log", filepath.Base(os.Args[0]), time.Now().Format("20060102150405"))
108+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/onsi/gomega v1.33.0
1010
github.com/shipengqi/component-base v0.2.9
1111
github.com/shipengqi/golib v0.2.12
12+
github.com/shipengqi/log v0.2.2
1213
github.com/spf13/cobra v1.8.0
1314
github.com/spf13/pflag v1.0.5
1415
github.com/stretchr/testify v1.9.0
@@ -32,7 +33,6 @@ require (
3233
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
3334
github.com/gosuri/uitable v0.0.4 // indirect
3435
github.com/inconshreveable/mousetrap v1.1.0 // indirect
35-
github.com/kr/text v0.2.0 // indirect
3636
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
3737
github.com/mattn/go-colorable v0.1.13 // indirect
3838
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -45,10 +45,13 @@ require (
4545
github.com/muesli/termenv v0.15.2 // indirect
4646
github.com/pmezard/go-difflib v1.0.0 // indirect
4747
github.com/rivo/uniseg v0.4.7 // indirect
48+
go.uber.org/multierr v1.10.0 // indirect
49+
go.uber.org/zap v1.27.0 // indirect
4850
golang.org/x/net v0.23.0 // indirect
4951
golang.org/x/sync v0.6.0 // indirect
5052
golang.org/x/sys v0.19.0 // indirect
5153
golang.org/x/term v0.18.0 // indirect
5254
golang.org/x/text v0.14.0 // indirect
5355
golang.org/x/tools v0.17.0 // indirect
56+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
5457
)

go.sum

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
4343
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4444
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
4545
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
46+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
47+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
4648
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4749
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
4850
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
@@ -84,14 +86,30 @@ github.com/shipengqi/component-base v0.2.9 h1:4XRB6PzTRgqKkkxkJwnpK8YOqDHRzXviyy
8486
github.com/shipengqi/component-base v0.2.9/go.mod h1:LfbMJtgUW7nNPwmVIi5wJMif/066edkcIJtkDDJgEQQ=
8587
github.com/shipengqi/golib v0.2.12 h1:/0hrev7+J8KChxEvoVdS2kbGQT8VO4C4qFAhtn6ZI8o=
8688
github.com/shipengqi/golib v0.2.12/go.mod h1:PIezev9VXxmhjawpu3j1JgLSNKLMq5AB8gLouJ83mrw=
89+
github.com/shipengqi/log v0.2.2 h1:+JvLIb3Xycl3/XJFVZn+ZzbJF7HeUBhdNvOdUoFHHS0=
90+
github.com/shipengqi/log v0.2.2/go.mod h1:YqXfNjg7aDR/KrXoU5KC3vCQ/YldJltQbyEwnlpJOb4=
8791
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
8892
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
8993
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
9094
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
9195
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
96+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
97+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
98+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
9299
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
100+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
101+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
102+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
103+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
104+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
93105
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
94106
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
107+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
108+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
109+
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
110+
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
111+
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
112+
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
95113
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
96114
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
97115
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
@@ -114,6 +132,8 @@ google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh
114132
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
115133
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
116134
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
135+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
136+
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
117137
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
118138
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
119139
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
const (
22-
RCFilename = ".czrc"
22+
RCFilename = ".ggczrc"
2323
ReservedDefaultName = "default"
2424
FieldKeyTemplateSelect = "template-select"
2525
)

internal/helpers/contains.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// helpers.Contains("Hello World", "World")
1212
// helpers.Contains(["Hello", "World"], "World")
1313
// helpers.Contains({"Hello": "World"}, "Hello")
14-
func Contains(s, contains interface{}, msgAndArgs ...interface{}) bool {
14+
func Contains(s, contains interface{}) bool {
1515
ok, found := containsElement(s, contains)
1616
if !ok {
1717
return false
@@ -26,7 +26,7 @@ func Contains(s, contains interface{}, msgAndArgs ...interface{}) bool {
2626
// helpers.NotContains("Hello World", "Earth")
2727
// helpers.NotContains(["Hello", "World"], "Earth")
2828
// helpers.NotContains({"Hello": "World"}, "Earth")
29-
func NotContains(s, contains interface{}, msgAndArgs ...interface{}) bool {
29+
func NotContains(s, contains interface{}) bool {
3030
ok, found := containsElement(s, contains)
3131
if !ok {
3232
return false

internal/helpers/equal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Equal(expected, actual interface{}) bool {
2727
//
2828
// Pointer variable equality is determined based on the equality of the
2929
// referenced values (as opposed to the memory addresses).
30-
func NotEqual(expected, actual interface{}, msgAndArgs ...interface{}) bool {
30+
func NotEqual(expected, actual interface{}) bool {
3131
if err := validateEqualArgs(expected, actual); err != nil {
3232
// invalid operation
3333
return false

internal/parameter/group.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package parameter
22

33
import (
44
standarderrs "errors"
5-
65
"github.com/charmbracelet/huh"
76
"github.com/shipengqi/golib/strutil"
7+
"github.com/shipengqi/log"
88

99
"github.com/shipengqi/commitizen/internal/errors"
1010
"github.com/shipengqi/commitizen/internal/helpers"
@@ -34,18 +34,23 @@ func (g *Group) Validate() []error {
3434
}
3535

3636
func (g *Group) Render(all map[FieldKey]huh.Field, fields []huh.Field) *huh.Group {
37+
group := huh.NewGroup(fields...)
38+
if len(g.DependsOn.OrConditions) < 1 && len(g.DependsOn.AndConditions) < 1 {
39+
return group
40+
}
41+
3742
for _, v := range g.DependsOn.OrConditions {
3843
v.fields = all
3944
}
4045
for _, v := range g.DependsOn.AndConditions {
4146
v.fields = all
4247
}
4348

44-
group := huh.NewGroup(fields...)
4549
group.WithHideFunc(func() bool {
4650
orCount := len(g.DependsOn.OrConditions)
4751
andCount := len(g.DependsOn.AndConditions)
4852

53+
log.Debugf("OrConditions: %d, AndConditions: %d", orCount, andCount)
4954
if orCount < 1 && andCount < 1 {
5055
return false
5156
}
@@ -64,15 +69,15 @@ func (g *Group) Render(all map[FieldKey]huh.Field, fields []huh.Field) *huh.Grou
6469
andMetCount++
6570
}
6671
}
67-
72+
log.Debugf("orMet: %v, andMetCount: %d", orMet, andMetCount)
6873
if orCount > 0 && andCount < 1 {
69-
return orMet
74+
return !orMet
7075
}
7176
if orCount < 1 && andCount > 0 {
72-
return andCount == andMetCount
77+
return !(andCount == andMetCount)
7378
}
7479
if orCount > 0 && andCount > 0 {
75-
return orMet && (andMetCount == orCount)
80+
return !(orMet && andMetCount == orCount)
7681
}
7782
return false
7883
})
@@ -81,8 +86,8 @@ func (g *Group) Render(all map[FieldKey]huh.Field, fields []huh.Field) *huh.Grou
8186
}
8287

8388
type DependsOn struct {
84-
AndConditions []Condition `yaml:"and_conditions" json:"and_conditions" mapstructure:"and_conditions"`
85-
OrConditions []Condition `yaml:"or_conditions" json:"or_conditions" mapstructure:"or_conditions"`
89+
AndConditions []*Condition `yaml:"and_conditions" json:"and_conditions" mapstructure:"and_conditions"`
90+
OrConditions []*Condition `yaml:"or_conditions" json:"or_conditions" mapstructure:"or_conditions"`
8691
}
8792

8893
type Condition struct {
@@ -96,7 +101,7 @@ type Condition struct {
96101
ValueNotContains interface{} `yaml:"value_not_contains" json:"value_not_contains" mapstructure:"value_not_contains"`
97102
}
98103

99-
func (c Condition) Validate() []error {
104+
func (c *Condition) Validate() []error {
100105
var errs []error
101106
if strutil.IsEmpty(c.ParameterName) {
102107
errs = append(errs, errors.NewMissingErr("parameter_name", "condition"))
@@ -108,50 +113,48 @@ func (c Condition) Validate() []error {
108113
return errs
109114
}
110115

111-
func (c Condition) Match() bool {
116+
func (c *Condition) Match() bool {
117+
field, ok := c.fields[GetFiledKey(c.ParameterName)]
118+
if !ok {
119+
return false
120+
}
121+
val := field.GetValue()
122+
112123
if c.ValueEmpty != nil {
113-
return c.IsEmpty(*c.ValueEmpty)
124+
return c.IsEmpty(*c.ValueEmpty, val)
114125
}
115126
if c.ValueEquals != nil {
116-
return c.Equal()
127+
return c.Equal(val)
117128
}
118129
if c.ValueNotEquals != nil {
119-
return c.NotEqual()
130+
return c.NotEqual(val)
120131
}
121132
if c.ValueContains != nil {
122-
return c.Contains()
133+
return c.Contains(val)
123134
}
124135
if c.ValueNotContains != nil {
125-
return c.NotContains()
136+
return c.NotContains(val)
126137
}
127138
return false
128139
}
129140

130-
func (c Condition) Equal() bool {
131-
val := c.fields[GetFiledKey(c.ParameterName)]
141+
func (c *Condition) Equal(val interface{}) bool {
132142
return helpers.Equal(c.ValueEquals, val)
133143
}
134144

135-
func (c Condition) NotEqual() bool {
136-
val := c.fields[GetFiledKey(c.ParameterName)]
145+
func (c *Condition) NotEqual(val interface{}) bool {
137146
return helpers.NotEqual(c.ValueNotEquals, val)
138147
}
139148

140-
func (c Condition) Contains() bool {
141-
val := c.fields[GetFiledKey(c.ParameterName)]
149+
func (c *Condition) Contains(val interface{}) bool {
142150
return helpers.Contains(val, c.ValueContains)
143151
}
144152

145-
func (c Condition) NotContains() bool {
146-
val := c.fields[GetFiledKey(c.ParameterName)]
153+
func (c *Condition) NotContains(val interface{}) bool {
147154
return helpers.NotContains(val, c.ValueNotContains)
148155
}
149156

150-
func (c Condition) IsEmpty(empty bool) bool {
151-
val, ok := c.fields[GetFiledKey(c.ParameterName)]
152-
if !ok {
153-
return false
154-
}
157+
func (c *Condition) IsEmpty(empty bool, val interface{}) bool {
155158
if empty && helpers.Empty(val) {
156159
return true
157160
}

internal/parameter/types.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package parameter
22

3-
import "github.com/shipengqi/golib/strutil"
3+
import (
4+
"github.com/shipengqi/golib/strutil"
5+
"strings"
6+
)
47

58
const (
69
TypeBoolean = "boolean"
@@ -16,7 +19,7 @@ const UnknownGroup = "unknown"
1619

1720
type FieldKey string
1821

19-
func GetFiledKey(item string, group ...string) FieldKey {
22+
func NewFiledKey(item string, group ...string) FieldKey {
2023
if len(group) < 1 {
2124
return FieldKey(UnknownGroup + "." + item)
2225
}
@@ -25,3 +28,14 @@ func GetFiledKey(item string, group ...string) FieldKey {
2528
}
2629
return FieldKey(group[0] + "." + item)
2730
}
31+
32+
func GetFiledKey(key string) FieldKey {
33+
keys := strings.Split(key, ".")
34+
if len(keys) < 1 {
35+
return ""
36+
}
37+
if len(keys) < 2 {
38+
return FieldKey(UnknownGroup + "." + keys[0])
39+
}
40+
return FieldKey(key)
41+
}

internal/templates/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (t *Template) Initialize() error {
114114
group = param.GetGroup()
115115
param.Render()
116116

117-
t.all[parameter.GetFiledKey(namestr, group)] = param
117+
t.all[parameter.NewFiledKey(namestr, group)] = param
118118

119119
if fields, ok := t.sorted.GetFields(group); !ok {
120120
news := make([]huh.Field, 0)

0 commit comments

Comments
 (0)