Skip to content

Commit 472aa27

Browse files
authored
Merge pull request #66 from shipengqi/feat/group-type
feat(parameter): add dependson for the parameter group
2 parents 43cf589 + 8cf8266 commit 472aa27

File tree

18 files changed

+824
-104
lines changed

18 files changed

+824
-104
lines changed

cmd/cz/cz.go

Lines changed: 29 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,26 @@ func New() *cobra.Command {
1924
c := &cobra.Command{
2025
Use: "commitizen",
2126
Long: `Command line utility to standardize git commit messages.`,
27+
PreRun: func(_ *cobra.Command, _ []string) {
28+
if !o.Debug {
29+
return
30+
}
31+
opts := &log.Options{
32+
DisableRotate: true,
33+
DisableFileCaller: true,
34+
DisableConsoleCaller: true,
35+
DisableConsoleLevel: true,
36+
DisableConsoleTime: true,
37+
Output: filepath.Join(os.TempDir(), "commitizen/logs"),
38+
FileLevel: log.DebugLevel.String(),
39+
FilenameEncoder: filenameEncoder,
40+
}
41+
err := fsutil.MkDirAll(opts.Output)
42+
if err != nil {
43+
panic(err)
44+
}
45+
log.Configure(opts)
46+
},
2247
RunE: func(_ *cobra.Command, _ []string) error {
2348
isRepo, err := git.IsGitRepo()
2449
if err != nil {
@@ -80,3 +105,7 @@ func New() *cobra.Command {
80105

81106
return c
82107
}
108+
109+
func filenameEncoder() string {
110+
return fmt.Sprintf("%s.%s.log", filepath.Base(os.Args[0]), time.Now().Format("20060102150405"))
111+
}

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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package helpers
2+
3+
import (
4+
"reflect"
5+
"strings"
6+
)
7+
8+
// Contains asserts that the specified string, list(array, slice...) or map contains the
9+
// specified substring or element.
10+
//
11+
// helpers.Contains("Hello World", "World")
12+
// helpers.Contains(["Hello", "World"], "World")
13+
// helpers.Contains({"Hello": "World"}, "Hello")
14+
func Contains(s, contains interface{}) bool {
15+
ok, found := containsElement(s, contains)
16+
if !ok {
17+
return false
18+
}
19+
20+
return found
21+
}
22+
23+
// NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the
24+
// specified substring or element.
25+
//
26+
// helpers.NotContains("Hello World", "Earth")
27+
// helpers.NotContains(["Hello", "World"], "Earth")
28+
// helpers.NotContains({"Hello": "World"}, "Earth")
29+
func NotContains(s, contains interface{}) bool {
30+
ok, found := containsElement(s, contains)
31+
if !ok {
32+
return false
33+
}
34+
35+
return !found
36+
}
37+
38+
// containsElement try loop over the list check if the list includes the element.
39+
// return (false, false) if impossible.
40+
// return (true, false) if element was not found.
41+
// return (true, true) if element was found.
42+
func containsElement(list interface{}, element interface{}) (ok, found bool) {
43+
44+
listValue := reflect.ValueOf(list)
45+
listType := reflect.TypeOf(list)
46+
if listType == nil {
47+
return false, false
48+
}
49+
listKind := listType.Kind()
50+
defer func() {
51+
if e := recover(); e != nil {
52+
ok = false
53+
found = false
54+
}
55+
}()
56+
57+
if listKind == reflect.String {
58+
elementValue := reflect.ValueOf(element)
59+
return true, strings.Contains(listValue.String(), elementValue.String())
60+
}
61+
62+
if listKind == reflect.Map {
63+
mapKeys := listValue.MapKeys()
64+
for i := 0; i < len(mapKeys); i++ {
65+
if ObjectsAreEqual(mapKeys[i].Interface(), element) {
66+
return true, true
67+
}
68+
}
69+
return true, false
70+
}
71+
72+
for i := 0; i < listValue.Len(); i++ {
73+
if ObjectsAreEqual(listValue.Index(i).Interface(), element) {
74+
return true, true
75+
}
76+
}
77+
return true, false
78+
79+
}

internal/helpers/contains_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package helpers
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestContainsNotContains(t *testing.T) {
9+
10+
type A struct {
11+
Name, Value string
12+
}
13+
list := []string{"Foo", "Bar"}
14+
15+
complexList := []*A{
16+
{"b", "c"},
17+
{"d", "e"},
18+
{"g", "h"},
19+
{"j", "k"},
20+
}
21+
simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
22+
var zeroMap map[interface{}]interface{}
23+
24+
cases := []struct {
25+
expected interface{}
26+
actual interface{}
27+
result bool
28+
}{
29+
{"Hello World", "Hello", true},
30+
{"Hello World", "Salut", false},
31+
{list, "Bar", true},
32+
{list, "Salut", false},
33+
{complexList, &A{"g", "h"}, true},
34+
{complexList, &A{"g", "e"}, false},
35+
{simpleMap, "Foo", true},
36+
{simpleMap, "Bar", false},
37+
{zeroMap, "Bar", false},
38+
}
39+
40+
for _, c := range cases {
41+
t.Run(fmt.Sprintf("Contains(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
42+
res := Contains(c.expected, c.actual)
43+
44+
if res != c.result {
45+
if res {
46+
t.Errorf("Contains(%#v, %#v) should return true:\n\t%#v contains %#v", c.expected, c.actual, c.expected, c.actual)
47+
} else {
48+
t.Errorf("Contains(%#v, %#v) should return false:\n\t%#v does not contain %#v", c.expected, c.actual, c.expected, c.actual)
49+
}
50+
}
51+
})
52+
}
53+
54+
for _, c := range cases {
55+
t.Run(fmt.Sprintf("NotContains(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
56+
res := NotContains(c.expected, c.actual)
57+
58+
// NotContains should be inverse of Contains. If it's not, something is wrong
59+
if res == Contains(c.expected, c.actual) {
60+
if res {
61+
t.Errorf("NotContains(%#v, %#v) should return true:\n\t%#v does not contains %#v", c.expected, c.actual, c.expected, c.actual)
62+
} else {
63+
t.Errorf("NotContains(%#v, %#v) should return false:\n\t%#v contains %#v", c.expected, c.actual, c.expected, c.actual)
64+
}
65+
}
66+
})
67+
}
68+
}

internal/helpers/empty.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package helpers
2+
3+
import "reflect"
4+
5+
// Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either
6+
// a slice or a channel with len == 0.
7+
//
8+
// helpers.Empty(obj)
9+
func Empty(object interface{}) bool {
10+
return isEmpty(object)
11+
}
12+
13+
// NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either
14+
// a slice or a channel with len == 0.
15+
//
16+
// if helpers.NotEmpty(obj) {
17+
// helpers.Equal("two", obj[1])
18+
// }
19+
func NotEmpty(object interface{}) bool {
20+
return !isEmpty(object)
21+
}
22+
23+
// isEmpty gets whether the specified object is considered empty or not.
24+
func isEmpty(object interface{}) bool {
25+
26+
// get nil case out of the way
27+
if object == nil {
28+
return true
29+
}
30+
31+
objValue := reflect.ValueOf(object)
32+
33+
switch objValue.Kind() {
34+
// collection types are empty when they have no element
35+
case reflect.Chan, reflect.Map, reflect.Slice:
36+
return objValue.Len() == 0
37+
// pointers are empty if nil or if the value they point to is empty
38+
case reflect.Ptr:
39+
if objValue.IsNil() {
40+
return true
41+
}
42+
deref := objValue.Elem().Interface()
43+
return isEmpty(deref)
44+
// for all other types, compare against the zero value
45+
// array types are empty when they match their zero-initialized state
46+
default:
47+
zero := reflect.Zero(objValue.Type())
48+
return reflect.DeepEqual(object, zero.Interface())
49+
}
50+
}

0 commit comments

Comments
 (0)