Skip to content

Commit

Permalink
feat(changelog): add -num option
Browse files Browse the repository at this point in the history
With this option, it is possible to limit the number of releases (tags)
that are included in history output.
  • Loading branch information
zbindenren committed Jan 12, 2021
1 parent f37a1f1 commit 638ea45
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
initDfltConfigOptName = "init-config"
noPromptOptName = "n"
versionOptName = "v"
numOptName = "num"

dfltChangelogFile = "CHANGELOG.md"
dateFormat = "2006-01-02"
Expand All @@ -49,6 +50,7 @@ type Command struct {
initConfig *bool
noPrompt *bool
version *bool
num *int
}

// New creates a new Command.
Expand All @@ -67,6 +69,7 @@ func New(b BuildInfo) *Command {
initConfig: fs.Bool(initDfltConfigOptName, false, fmt.Sprintf("initialize a default changelog configuration '%s'", config.FileName)),
noPrompt: fs.Bool(noPromptOptName, false, "do not prompt for next version"),
version: fs.Bool(versionOptName, false, "show program version information"),
num: fs.Int(numOptName, 0, fmt.Sprintf("in combination with -%s: the number of tags to go back", historyOptName)),
}
}

Expand Down Expand Up @@ -175,10 +178,18 @@ func (c Command) createChangelog(g *git.Command, cfg config.Changelog, l *flash.
}

func (c Command) validate() error {
if c.sinceTag != nil && *c.sinceTag != "" && !*c.history {
if *c.sinceTag != "" && !*c.history {
return fmt.Errorf("'-%s' option is only allowed in combination '-%s' option", sinceTagOptName, historyOptName)
}

if *c.num > 0 && !*c.history {
return fmt.Errorf("'-%s' option is only allowed in combination '-%s' option", numOptName, historyOptName)
}

if *c.num > 0 && *c.sinceTag != "" {
return fmt.Errorf("'-%s' and '-%s' are mutually exclusive", numOptName, sinceTagOptName)
}

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func setup(t *testing.T, repoName string) (c Command, changelogPath string, clea
toStdOut: newBoolPtr(false),
file: newStrPtr(filepath.Join(tmp, repoName, dfltChangelogFile)),
version: newBoolPtr(false),
num: newIntPtr(0),
sinceTag: newStrPtr(""),
}

cleanup = func() {
Expand All @@ -154,6 +156,10 @@ func newBoolPtr(b bool) *bool {
return &b
}

func newIntPtr(i int) *int {
return &i
}

func newStrPtr(s string) *string {
return &s
}
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (c Command) runHistory(dst io.Writer, l *flash.Logger, cfg config.Changelog

max := len(tags) - 1

if *c.num > 0 && *c.num <= max {
max = *c.num - 1
}

if *c.sinceTag != "" {
max = tags.Index(*c.sinceTag)

Expand Down

0 comments on commit 638ea45

Please sign in to comment.