From 50a59be0437ec616f789222ebc0db3c4b2ab691b Mon Sep 17 00:00:00 2001 From: rhysd Date: Tue, 8 Oct 2024 23:12:15 +0900 Subject: [PATCH] refactor options as `Config` struct --- changelog.go | 38 +++++++++++++++++++++----------------- main.go | 8 +++++++- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/changelog.go b/changelog.go index 63c049c..63b76d6 100644 --- a/changelog.go +++ b/changelog.go @@ -17,18 +17,15 @@ type link struct { url string } -// ChangeLog is a struct to generate changelog output from given repository URL -type ChangeLog struct { - repoURL string - out io.Writer - level int - drafts bool - ignore *regexp.Regexp - extract *regexp.Regexp +type Config struct { + Level int + Drafts bool + Ignore *regexp.Regexp + Extract *regexp.Regexp } -func (cl *ChangeLog) filterReleases(rels []*github.RepositoryRelease) []*github.RepositoryRelease { - if cl.drafts && cl.ignore == nil && cl.extract == nil { +func (c *Config) filterReleases(rels []*github.RepositoryRelease) []*github.RepositoryRelease { + if c.Drafts && c.Ignore == nil && c.Extract == nil { return rels } @@ -36,9 +33,9 @@ func (cl *ChangeLog) filterReleases(rels []*github.RepositoryRelease) []*github. for i < len(rels) { r := rels[i] t := r.GetTagName() - if !cl.drafts && r.GetDraft() || - cl.ignore != nil && cl.ignore.MatchString(t) || - cl.extract != nil && !cl.extract.MatchString(t) { + if !c.Drafts && r.GetDraft() || + c.Ignore != nil && c.Ignore.MatchString(t) || + c.Extract != nil && !c.Extract.MatchString(t) { rels = append(rels[:i], rels[i+1:]...) } else { i++ @@ -48,12 +45,19 @@ func (cl *ChangeLog) filterReleases(rels []*github.RepositoryRelease) []*github. return rels } +// ChangeLog is a struct to generate changelog output from given repository URL +type ChangeLog struct { + repoURL string + out io.Writer + cfg *Config +} + // Generate generates changelog text from given releases and outputs it to its writer func (cl *ChangeLog) Generate(rels []*github.RepositoryRelease, links []*github.Autolink) error { - rels = cl.filterReleases(rels) + rels = cl.cfg.filterReleases(rels) out := bufio.NewWriter(cl.out) - heading := strings.Repeat("#", cl.level) + heading := strings.Repeat("#", cl.cfg.Level) linker := NewReflinker(cl.repoURL) for _, l := range links { @@ -120,8 +124,8 @@ func (cl *ChangeLog) Generate(rels []*github.RepositoryRelease, links []*github. } // NewChangeLog creates a new ChangeLog instance -func NewChangeLog(w io.Writer, u *url.URL, l int, d bool, i, e *regexp.Regexp) *ChangeLog { +func NewChangeLog(w io.Writer, u *url.URL, c *Config) *ChangeLog { // Strip credentials in the repository URL (#9) u.User = nil - return &ChangeLog{strings.TrimSuffix(u.String(), ".git"), w, l, d, i, e} + return &ChangeLog{strings.TrimSuffix(u.String(), ".git"), w, c} } diff --git a/main.go b/main.go index 9bb72ae..23a0e24 100644 --- a/main.go +++ b/main.go @@ -92,7 +92,13 @@ func main() { fail(err) } - cl := NewChangeLog(os.Stdout, url, *heading, *drafts, reIgnore, reExtract) + cfg := &Config{ + Level: *heading, + Drafts: *drafts, + Ignore: reIgnore, + Extract: reExtract, + } + cl := NewChangeLog(os.Stdout, url, cfg) ls, _ := gh.CustomAutolinks() if err := cl.Generate(rels, ls); err != nil { fail(err)