-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parameters): refactor parameters and validators (#62)
* <no value>: <no value> * feat(parameters): add parameters and validators * feat(template): update default template
- Loading branch information
Showing
26 changed files
with
995 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,48 @@ | ||
package config | ||
|
||
const DefaultCommitTemplate = `--- | ||
version: v2 | ||
name: default | ||
default: true | ||
items: | ||
- name: type | ||
desc: "Select the type of change that you're committing:" | ||
type: select | ||
group: page1 | ||
label: "Select the type of change that you're committing:" | ||
type: list | ||
options: | ||
- name: feat | ||
desc: "A new feature" | ||
- name: fix | ||
desc: "A bug fix" | ||
- name: docs | ||
desc: "Documentation only changes" | ||
- name: test | ||
desc: "Adding missing or correcting existing tests" | ||
- name: chore | ||
desc: "Changes to the build process or auxiliary tools and\n libraries such as documentation generation" | ||
- name: style | ||
desc: "Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)" | ||
- name: refactor | ||
desc: "A code change that neither fixes a bug nor adds a feature" | ||
- name: perf | ||
desc: "A code change that improves performance" | ||
- name: revert | ||
desc: "Reverts a previous commit" | ||
- value: feat | ||
key: "feat: A new feature" | ||
- value: fix | ||
key: "fix: A bug fix" | ||
- value: docs | ||
key: "docs: Documentation only changes" | ||
- value: test | ||
key: "test: Adding missing or correcting existing tests" | ||
- value: chore | ||
key: "chore: Changes to the build process or auxiliary tools and\n libraries such as documentation generation" | ||
- value: style | ||
key: "style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)" | ||
- value: refactor | ||
key: "refactor: A code change that neither fixes a bug nor adds a feature" | ||
- value: perf | ||
key: "perf: A code change that improves performance" | ||
- value: revert | ||
key: "revert: Reverts a previous commit" | ||
- name: scope | ||
desc: "Scope. What is the scope of this change? (class or file name):" | ||
type: input | ||
group: page2 | ||
label: "Scope. What is the scope of this change? (class or file name):" | ||
type: string | ||
- name: subject | ||
desc: "Subject. Write a short and imperative summary of the code change (lower case and no period):" | ||
type: input | ||
group: page2 | ||
label: "Subject. Write a short and imperative summary of the code change (lower case and no period):" | ||
type: string | ||
required: true | ||
- name: body | ||
desc: "Body. Provide additional contextual information about the code changes:" | ||
type: textarea | ||
group: page3 | ||
label: "Body. Provide additional contextual information about the code changes:" | ||
type: text | ||
- name: footer | ||
desc: "Footer. Information about Breaking Changes and reference issues that this commit closes:" | ||
type: textarea | ||
group: page3 | ||
label: "Footer. Information about Breaking Changes and reference issues that this commit closes:" | ||
type: text | ||
format: "{{.type}}{{with .scope}}({{.}}){{end}}: {{.subject}}{{with .body}}\n\n{{.}}{{end}}{{with .footer}}\n\n{{.}}{{end}}"` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package errors | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrType = errors.New("type error") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type RequiredErr struct { | ||
field string | ||
} | ||
|
||
func (e RequiredErr) Error() string { | ||
return fmt.Sprintf("%s is required", e.field) | ||
} | ||
|
||
func NewRequiredErr(field string) error { | ||
return RequiredErr{field: field} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package boolean | ||
|
||
import ( | ||
"github.com/charmbracelet/huh" | ||
|
||
"github.com/shipengqi/commitizen/internal/parameter" | ||
) | ||
|
||
type Param struct { | ||
parameter.Parameter `mapstructure:",squash"` | ||
|
||
DefaultValue bool `yaml:"default_value" json:"default_value" mapstructure:"default_value"` | ||
} | ||
|
||
func (p Param) Render() huh.Field { | ||
param := huh.NewConfirm().Key(p.Name). | ||
Title(p.Label) | ||
|
||
if len(p.Description) > 0 { | ||
param.Description(p.Description) | ||
} | ||
|
||
param.Value(&p.DefaultValue) | ||
|
||
return param | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package integer | ||
|
||
import ( | ||
"github.com/charmbracelet/huh" | ||
|
||
"github.com/shipengqi/commitizen/internal/parameter" | ||
"github.com/shipengqi/commitizen/internal/parameter/validators" | ||
) | ||
|
||
type Param struct { | ||
parameter.Parameter `mapstructure:",squash"` | ||
|
||
DefaultValue string `yaml:"default_value" json:"default_value" mapstructure:"default_value"` | ||
Required bool `yaml:"required" json:"required" mapstructure:"required"` | ||
Min *int `yaml:"min" json:"min" mapstructure:"min"` | ||
Max *int `yaml:"max" json:"max" mapstructure:"max"` | ||
} | ||
|
||
func (p Param) Render() huh.Field { | ||
param := huh.NewInput().Key(p.Name). | ||
Title(p.Label) | ||
|
||
if len(p.Description) > 0 { | ||
param.Description(p.Description) | ||
} | ||
|
||
param.Value(&p.DefaultValue) | ||
|
||
var group []validators.Validator | ||
if p.Required { | ||
group = append(group, validators.Required(p.Name, true)) | ||
} | ||
if p.Min != nil { | ||
group = append(group, validators.Min(*p.Min)) | ||
} | ||
if p.Max != nil { | ||
group = append(group, validators.Max(*p.Max)) | ||
} | ||
|
||
if len(group) > 0 { | ||
param.Validate(validators.Group(group...)) | ||
} | ||
return param | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package list | ||
|
||
import ( | ||
"github.com/charmbracelet/huh" | ||
|
||
"github.com/shipengqi/commitizen/internal/errors" | ||
"github.com/shipengqi/commitizen/internal/parameter" | ||
"github.com/shipengqi/commitizen/internal/parameter/validators" | ||
) | ||
|
||
type Param struct { | ||
parameter.Parameter `mapstructure:",squash"` | ||
|
||
Options []huh.Option[string] `yaml:"options" json:"options" mapstructure:"options"` | ||
DefaultValue string `yaml:"default_value" json:"default_value" mapstructure:"default_value"` | ||
Required bool `yaml:"required" json:"required" mapstructure:"required"` | ||
} | ||
|
||
func (p Param) Validate() []error { | ||
errs := p.Parameter.Validate() | ||
if len(p.Options) < 1 { | ||
errs = append(errs, errors.NewRequiredErr("parameter.options")) | ||
} | ||
return errs | ||
} | ||
|
||
func (p Param) Render() huh.Field { | ||
param := huh.NewSelect[string]().Key(p.Name). | ||
Options(p.Options...). | ||
Title(p.Label) | ||
if len(p.Description) > 0 { | ||
param.Description(p.Description) | ||
} | ||
|
||
param.Value(&p.DefaultValue) | ||
|
||
var group []validators.Validator | ||
if p.Required { | ||
group = append(group, validators.Required(p.Name, false)) | ||
} | ||
|
||
if len(group) > 0 { | ||
param.Validate(validators.Group(group...)) | ||
} | ||
return param | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package multilist | ||
|
||
import ( | ||
"github.com/charmbracelet/huh" | ||
|
||
"github.com/shipengqi/commitizen/internal/errors" | ||
"github.com/shipengqi/commitizen/internal/parameter" | ||
"github.com/shipengqi/commitizen/internal/parameter/validators" | ||
) | ||
|
||
type Param struct { | ||
parameter.Parameter `mapstructure:",squash"` | ||
|
||
Options []huh.Option[string] `yaml:"options" json:"options" mapstructure:"options"` | ||
DefaultValue []string `yaml:"default_value" json:"default_value" mapstructure:"default_value"` | ||
Required bool `yaml:"required" json:"required" mapstructure:"required"` | ||
} | ||
|
||
func (p Param) Validate() []error { | ||
errs := p.Parameter.Validate() | ||
if len(p.Options) < 1 { | ||
errs = append(errs, errors.NewRequiredErr("parameter.options")) | ||
} | ||
return errs | ||
} | ||
|
||
func (p Param) Render() huh.Field { | ||
param := huh.NewMultiSelect[string]().Key(p.Name). | ||
Options(p.Options...). | ||
Title(p.Label) | ||
if len(p.Description) > 0 { | ||
param.Description(p.Description) | ||
} | ||
|
||
param.Value(&p.DefaultValue) | ||
|
||
if p.Required { | ||
param.Validate(validators.MultiRequired(p.Name)) | ||
} | ||
|
||
return param | ||
} |
Oops, something went wrong.