Skip to content

Commit 26779e7

Browse files
committed
feat(parameter): support trim for the string/secret type
1 parent 482f1e9 commit 26779e7

File tree

11 files changed

+54
-40
lines changed

11 files changed

+54
-40
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Properties:
189189
| required | no | `false` | Whether a string value is required or not. |
190190
| fqdn | no | `false` | Add a preset FQDN regex to validate string. |
191191
| ip | no | `false` | Add a preset IPv4/IPv6 regex to validate string. |
192+
| trim | no | `false` | If true, will remove the leading and trailing blank characters before submit. |
192193
| default_value | no | - | The default value for this item. |
193194
| regex | no | - | A regex used to validate the string. |
194195
| min_length | no | - | The minimum length of the string. If the value is not required and no value has been given, this is ignored. |
@@ -239,6 +240,7 @@ Properties:
239240
| Property | Required | Default Value | Description |
240241
|:--------------|:---------|:--------------|:-------------------------------------------------------------------------------------------------------------|
241242
| required | no | `false` | Whether the secret is required or not. |
243+
| trim | no | `false` | If true, will remove the leading and trailing blank characters before submit. |
242244
| default_value | no | - | The default value for this item. |
243245
| regex | no | - | A regex used to validate the secret. |
244246
| min_length | no | - | The minimum length of the secret. If the value is not required and no value has been given, this is ignored. |

internal/config/default.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ items:
3131
group: page2
3232
label: "Scope. What is the scope of this change? (class or file name):"
3333
type: string
34+
trim: true
3435
- name: subject
3536
group: page2
3637
label: "Subject. Write a short and imperative summary of the code change (lower case and no period):"
3738
type: string
3839
required: true
40+
trim: true
3941
- name: body
4042
group: page3
4143
label: "Body. Provide additional contextual information about the code changes:"

internal/parameter/boolean/bool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Param struct {
1212
DefaultValue bool `yaml:"default_value" json:"default_value" mapstructure:"default_value"`
1313
}
1414

15-
func (p Param) Render() huh.Field {
15+
func (p *Param) Render() {
1616
param := huh.NewConfirm().Key(p.Name).
1717
Title(p.Label)
1818

@@ -22,5 +22,5 @@ func (p Param) Render() huh.Field {
2222

2323
param.Value(&p.DefaultValue)
2424

25-
return param
25+
p.Field = param
2626
}

internal/parameter/integer/int.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Param struct {
1616
Max *int `yaml:"max" json:"max" mapstructure:"max"`
1717
}
1818

19-
func (p Param) Render() huh.Field {
19+
func (p *Param) Render() {
2020
param := huh.NewInput().Key(p.Name).
2121
Title(p.Label)
2222

@@ -40,5 +40,5 @@ func (p Param) Render() huh.Field {
4040
if len(group) > 0 {
4141
param.Validate(validators.Group(group...))
4242
}
43-
return param
43+
p.Field = param
4444
}

internal/parameter/list/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ type Param struct {
1616
Required bool `yaml:"required" json:"required" mapstructure:"required"`
1717
}
1818

19-
func (p Param) Validate() []error {
19+
func (p *Param) Validate() []error {
2020
errs := p.Parameter.Validate()
2121
if len(p.Options) < 1 {
2222
errs = append(errs, errors.NewMissingErr("options", p.Name))
2323
}
2424
return errs
2525
}
2626

27-
func (p Param) Render() huh.Field {
27+
func (p *Param) Render() {
2828
param := huh.NewSelect[string]().Key(p.Name).
2929
Options(p.Options...).
3030
Title(p.Label)
@@ -42,5 +42,5 @@ func (p Param) Render() huh.Field {
4242
if len(group) > 0 {
4343
param.Validate(validators.Group(group...))
4444
}
45-
return param
45+
p.Field = param
4646
}

internal/parameter/multilist/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ type Param struct {
1717
Limit *int `yaml:"limit" json:"limit" mapstructure:"limit"`
1818
}
1919

20-
func (p Param) Validate() []error {
20+
func (p *Param) Validate() []error {
2121
errs := p.Parameter.Validate()
2222
if len(p.Options) < 1 {
2323
errs = append(errs, errors.NewMissingErr("options", p.Name))
2424
}
2525
return errs
2626
}
2727

28-
func (p Param) Render() huh.Field {
28+
func (p *Param) Render() {
2929
param := huh.NewMultiSelect[string]().Key(p.Name).
3030
Options(p.Options...).
3131
Title(p.Label)
@@ -47,5 +47,5 @@ func (p Param) Render() huh.Field {
4747
param.Validate(validators.Group(group...))
4848
}
4949

50-
return param
50+
p.Field = param
5151
}

internal/parameter/param.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
)
99

1010
type Interface interface {
11+
huh.Field
1112
GetGroup() string
12-
Render() huh.Field
13+
Render()
1314
Validate() []error
1415
}
1516

1617
type Parameter struct {
18+
huh.Field `mapstructure:"-"`
1719
Name string `yaml:"name" json:"name" mapstructure:"name"`
1820
Group string `yaml:"group" json:"group" mapstructure:"group"`
1921
Label string `yaml:"label" json:"label" mapstructure:"label"`
@@ -22,15 +24,13 @@ type Parameter struct {
2224
// DependsOn DependsOn `yaml:"depends_on" json:"depends_on" mapstructure:"depends_on"`
2325
}
2426

25-
func (p Parameter) GetGroup() string {
27+
func (p *Parameter) GetGroup() string {
2628
return p.Group
2729
}
2830

29-
func (p Parameter) Render() huh.Field {
30-
return nil
31-
}
31+
func (p *Parameter) Render() {}
3232

33-
func (p Parameter) Validate() []error {
33+
func (p *Parameter) Validate() []error {
3434
var errs []error
3535
if strutil.IsEmpty(p.Name) {
3636
errs = append(errs, errors.NewMissingErr("parameter.name"))

internal/parameter/secret/secret.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package secret
22

33
import (
4-
"github.com/charmbracelet/huh"
5-
"github.com/shipengqi/commitizen/internal/parameter/validators"
6-
74
"github.com/shipengqi/commitizen/internal/parameter/str"
5+
"github.com/shipengqi/commitizen/internal/parameter/validators"
86
)
97

108
type Param struct {
119
str.Param `mapstructure:",squash"`
1210
}
1311

14-
func (p Param) Render() huh.Field {
12+
func (p *Param) Render() {
1513
param := p.Param.RenderInput()
1614
param.Password(true)
1715

@@ -34,5 +32,5 @@ func (p Param) Render() huh.Field {
3432
param.Validate(validators.Group(group...))
3533
}
3634

37-
return param
35+
p.Field = param
3836
}

internal/parameter/str/str.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package str
22

33
import (
4+
"strings"
5+
46
"github.com/charmbracelet/huh"
57

68
"github.com/shipengqi/commitizen/internal/parameter"
@@ -13,18 +15,29 @@ type Param struct {
1315
Required bool `yaml:"required" json:"required" mapstructure:"required"`
1416
FQDN bool `yaml:"fqdn" json:"fqdn" mapstructure:"fqdn"`
1517
IP bool `yaml:"ip" json:"ip" mapstructure:"ip"`
16-
Trim bool `yaml:"trim" json:"trim" mapstructure:"trim"` // Todo implement trim??
18+
Trim bool `yaml:"trim" json:"trim" mapstructure:"trim"`
1719
DefaultValue string `yaml:"default_value" json:"default_value" mapstructure:"default_value"`
1820
Regex string `yaml:"regex" json:"regex" mapstructure:"regex"`
1921
MinLength *int `yaml:"min_length" json:"min_length" mapstructure:"min_length"`
2022
MaxLength *int `yaml:"max_length" json:"max_length" mapstructure:"max_length"`
2123
}
2224

23-
func (p Param) Render() huh.Field {
24-
return p.RenderInput()
25+
func (p *Param) Render() {
26+
p.Field = p.RenderInput()
27+
}
28+
29+
func (p *Param) GetValue() any {
30+
if !p.Trim {
31+
return p.Field.GetValue()
32+
}
33+
val := p.Field.GetValue()
34+
if str, ok := val.(string); ok {
35+
return strings.TrimSpace(str)
36+
}
37+
return p.Field.GetValue()
2538
}
2639

27-
func (p Param) RenderInput() *huh.Input {
40+
func (p *Param) RenderInput() *huh.Input {
2841
param := huh.NewInput().Key(p.Name).
2942
Title(p.Label)
3043

internal/parameter/text/text.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Param struct {
1818
Height *int `yaml:"height" json:"height" mapstructure:"height"`
1919
}
2020

21-
func (p Param) Render() huh.Field {
21+
func (p *Param) Render() {
2222
param := huh.NewText().Key(p.Name).
2323
Title(p.Label)
2424

@@ -51,5 +51,5 @@ func (p Param) Render() huh.Field {
5151
param.Validate(validators.Group(group...))
5252
}
5353

54-
return param
54+
p.Field = param
5555
}

0 commit comments

Comments
 (0)