Skip to content

Commit

Permalink
feat(options): add a new option --template/-t (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
shipengqi authored Mar 19, 2024
1 parent e9c0fef commit dc922b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/cz/cz.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func New() *cobra.Command {
}

conf := config.New()
tmpl, err := conf.Run(o.NoTTY)
tmpl, err := conf.Run(o)
if err != nil {
return err
}
Expand Down
28 changes: 21 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/shipengqi/golib/sysutil"
"gopkg.in/yaml.v3"

"github.com/shipengqi/commitizen/internal/options"
"github.com/shipengqi/commitizen/internal/render"
"github.com/shipengqi/commitizen/internal/ui"
)
Expand All @@ -24,7 +25,7 @@ const (

type Config struct {
defaultTmpl *render.Template
others []*render.Template
more []*render.Template
}

func New() *Config {
Expand Down Expand Up @@ -65,7 +66,7 @@ func (c *Config) initialize() error {
}

exists[v.Name] = struct{}{}
c.others = append(c.others, v)
c.more = append(c.more, v)
}
// If the user has not configured a default template, use the built-in template as the default template
if c.defaultTmpl == nil {
Expand All @@ -79,14 +80,27 @@ func (c *Config) initialize() error {
return nil
}

func (c *Config) Run(noTTY bool) (*render.Template, error) {
func (c *Config) Run(opts *options.Options) (*render.Template, error) {
err := c.initialize()
if err != nil {
return nil, err
}
if len(c.others) > 0 {
// find the given template
if len(opts.Template) > 0 {
if opts.Template == c.defaultTmpl.Name {
return c.defaultTmpl, nil
}
for _, v := range c.more {
if v.Name == opts.Template {
return v, nil
}
}
return nil, fmt.Errorf("template '%s' not found", opts.Template)
}

if len(c.more) > 0 {
model := c.createTemplatesSelect("Select a template to use for this commit:")
if _, err := ui.Run(model, noTTY); err != nil {
if _, err = ui.Run(model, opts.NoTTY); err != nil {
return nil, err
}
if model.Canceled() {
Expand All @@ -96,7 +110,7 @@ func (c *Config) Run(noTTY bool) (*render.Template, error) {
if val == c.defaultTmpl.Name {
return c.defaultTmpl, nil
}
for _, v := range c.others {
for _, v := range c.more {
if v.Name == val {
return v, nil
}
Expand All @@ -108,7 +122,7 @@ func (c *Config) Run(noTTY bool) (*render.Template, error) {
func (c *Config) createTemplatesSelect(label string) *ui.SelectModel {
var choices ui.Choices
var all []*render.Template
all = append(all, c.others...)
all = append(all, c.more...)
all = append(all, c.defaultTmpl)
// list custom templates and default templates
for _, v := range all {
Expand Down
2 changes: 2 additions & 0 deletions internal/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Options struct {
DryRun bool
NoTTY bool
Template string
GitOptions *git.Options
}

Expand All @@ -23,6 +24,7 @@ func (o *Options) AddFlags(f *pflag.FlagSet) {
o.GitOptions.AddFlags(f)

f.BoolVar(&o.DryRun, "dry-run", o.DryRun, "you can use the --dry-run flag to preview the message that would be committed, without really submitting it.")
f.StringVarP(&o.Template, "template", "t", o.Template, "template name to use when multiple templates exist.")
f.BoolVar(&o.NoTTY, "no-tty", o.NoTTY, "make sure that the TTY (terminal) is never used for any output.")

_ = f.MarkHidden("no-tty")
Expand Down

0 comments on commit dc922b1

Please sign in to comment.