|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + tea "github.com/charmbracelet/bubbletea" |
| 11 | + "github.com/shipengqi/golib/convutil" |
| 12 | + "github.com/shipengqi/golib/fsutil" |
| 13 | + "github.com/shipengqi/golib/sysutil" |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | + |
| 16 | + "github.com/shipengqi/commitizen/internal/render" |
| 17 | + "github.com/shipengqi/commitizen/internal/ui" |
| 18 | +) |
| 19 | + |
| 20 | +const RCFilename = ".git-czrc" |
| 21 | + |
| 22 | +type Config struct { |
| 23 | + defaultTmpl *render.Template |
| 24 | + others []*render.Template |
| 25 | +} |
| 26 | + |
| 27 | +func New() *Config { |
| 28 | + return &Config{} |
| 29 | +} |
| 30 | + |
| 31 | +func (c *Config) Initialize() error { |
| 32 | + var fpath string |
| 33 | + if fsutil.IsExists(RCFilename) { |
| 34 | + fpath = RCFilename |
| 35 | + } else { |
| 36 | + home := sysutil.HomeDir() |
| 37 | + p := filepath.Join(home, RCFilename) |
| 38 | + if fsutil.IsExists(p) { |
| 39 | + fpath = p |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + tmpls, err := LoadTemplates(fpath) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + for _, v := range tmpls { |
| 48 | + if v.Default { |
| 49 | + c.defaultTmpl = v |
| 50 | + continue |
| 51 | + } |
| 52 | + c.others = append(c.others, v) |
| 53 | + } |
| 54 | + // If the user has not configured a default template, the built-in template is used |
| 55 | + if c.defaultTmpl == nil { |
| 56 | + defaults, err := Load(convutil.S2B(DefaultCommitTemplate)) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + c.defaultTmpl = defaults[0] |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (c *Config) Run() (*render.Template, error) { |
| 67 | + if len(c.others) > 0 { |
| 68 | + model := c.createTemplatesSelect("Select the template to be used for this commit") |
| 69 | + if _, err := tea.NewProgram(model).Run(); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + if model.Canceled() { |
| 73 | + return nil, render.ErrCanceled |
| 74 | + } |
| 75 | + val := model.Value() |
| 76 | + if val == c.defaultTmpl.Name { |
| 77 | + return c.defaultTmpl, nil |
| 78 | + } |
| 79 | + for _, v := range c.others { |
| 80 | + if v.Name == val { |
| 81 | + return v, nil |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return c.defaultTmpl, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (c *Config) createTemplatesSelect(label string) *ui.SelectModel { |
| 89 | + var choices ui.Choices |
| 90 | + var all []*render.Template |
| 91 | + all = append(all, c.others...) |
| 92 | + all = append(all, c.defaultTmpl) |
| 93 | + // list custom templates and default templates |
| 94 | + for _, v := range all { |
| 95 | + choices = append(choices, ui.Choice(v.Name)) |
| 96 | + } |
| 97 | + m := ui.NewSelect(label, choices) |
| 98 | + return m |
| 99 | +} |
| 100 | + |
| 101 | +func LoadTemplates(file string) ([]*render.Template, error) { |
| 102 | + if len(file) == 0 { |
| 103 | + return nil, nil |
| 104 | + } |
| 105 | + fd, err := os.Open(file) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + defer func() { _ = fd.Close() }() |
| 110 | + return load(fd) |
| 111 | +} |
| 112 | + |
| 113 | +func Load(data []byte) ([]*render.Template, error) { |
| 114 | + return load(bytes.NewReader(data)) |
| 115 | +} |
| 116 | + |
| 117 | +func load(reader io.Reader) ([]*render.Template, error) { |
| 118 | + var templates []*render.Template |
| 119 | + d := yaml.NewDecoder(reader) |
| 120 | + for { |
| 121 | + tmpl := new(render.Template) |
| 122 | + err := d.Decode(tmpl) |
| 123 | + if err != nil { |
| 124 | + if errors.Is(err, io.EOF) { |
| 125 | + break |
| 126 | + } |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + if tmpl == nil { |
| 130 | + continue |
| 131 | + } |
| 132 | + templates = append(templates, tmpl) |
| 133 | + } |
| 134 | + |
| 135 | + return templates, nil |
| 136 | +} |
0 commit comments