Skip to content

Commit dc922b1

Browse files
authored
feat(options): add a new option --template/-t (#38)
1 parent e9c0fef commit dc922b1

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

cmd/cz/cz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func New() *cobra.Command {
2727
}
2828

2929
conf := config.New()
30-
tmpl, err := conf.Run(o.NoTTY)
30+
tmpl, err := conf.Run(o)
3131
if err != nil {
3232
return err
3333
}

internal/config/config.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/shipengqi/golib/sysutil"
1414
"gopkg.in/yaml.v3"
1515

16+
"github.com/shipengqi/commitizen/internal/options"
1617
"github.com/shipengqi/commitizen/internal/render"
1718
"github.com/shipengqi/commitizen/internal/ui"
1819
)
@@ -24,7 +25,7 @@ const (
2425

2526
type Config struct {
2627
defaultTmpl *render.Template
27-
others []*render.Template
28+
more []*render.Template
2829
}
2930

3031
func New() *Config {
@@ -65,7 +66,7 @@ func (c *Config) initialize() error {
6566
}
6667

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

82-
func (c *Config) Run(noTTY bool) (*render.Template, error) {
83+
func (c *Config) Run(opts *options.Options) (*render.Template, error) {
8384
err := c.initialize()
8485
if err != nil {
8586
return nil, err
8687
}
87-
if len(c.others) > 0 {
88+
// find the given template
89+
if len(opts.Template) > 0 {
90+
if opts.Template == c.defaultTmpl.Name {
91+
return c.defaultTmpl, nil
92+
}
93+
for _, v := range c.more {
94+
if v.Name == opts.Template {
95+
return v, nil
96+
}
97+
}
98+
return nil, fmt.Errorf("template '%s' not found", opts.Template)
99+
}
100+
101+
if len(c.more) > 0 {
88102
model := c.createTemplatesSelect("Select a template to use for this commit:")
89-
if _, err := ui.Run(model, noTTY); err != nil {
103+
if _, err = ui.Run(model, opts.NoTTY); err != nil {
90104
return nil, err
91105
}
92106
if model.Canceled() {
@@ -96,7 +110,7 @@ func (c *Config) Run(noTTY bool) (*render.Template, error) {
96110
if val == c.defaultTmpl.Name {
97111
return c.defaultTmpl, nil
98112
}
99-
for _, v := range c.others {
113+
for _, v := range c.more {
100114
if v.Name == val {
101115
return v, nil
102116
}
@@ -108,7 +122,7 @@ func (c *Config) Run(noTTY bool) (*render.Template, error) {
108122
func (c *Config) createTemplatesSelect(label string) *ui.SelectModel {
109123
var choices ui.Choices
110124
var all []*render.Template
111-
all = append(all, c.others...)
125+
all = append(all, c.more...)
112126
all = append(all, c.defaultTmpl)
113127
// list custom templates and default templates
114128
for _, v := range all {

internal/options/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
type Options struct {
1010
DryRun bool
1111
NoTTY bool
12+
Template string
1213
GitOptions *git.Options
1314
}
1415

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

2526
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.")
27+
f.StringVarP(&o.Template, "template", "t", o.Template, "template name to use when multiple templates exist.")
2628
f.BoolVar(&o.NoTTY, "no-tty", o.NoTTY, "make sure that the TTY (terminal) is never used for any output.")
2729

2830
_ = f.MarkHidden("no-tty")

0 commit comments

Comments
 (0)