Skip to content

Commit

Permalink
refactor options as Config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 8, 2024
1 parent bf9a087 commit 50a59be
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
38 changes: 21 additions & 17 deletions changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,25 @@ 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
}

i := 0
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++
Expand All @@ -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 {
Expand Down Expand Up @@ -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}
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 50a59be

Please sign in to comment.