Skip to content

Commit

Permalink
Add command 'added' to manipulate changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmachado committed Jul 3, 2019
1 parent 53e9325 commit f0345c7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
19 changes: 19 additions & 0 deletions chg/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ func (c *Changelog) Version(version string) *Version {
return nil
}

// AddItem includes the message under the proper section of Unreleased version
func (c *Changelog) AddItem(section ChangeType, message string) {
v := c.Version("Unreleased")
if v == nil {
v = &Version{Name: "Unreleased"}
c.Versions = append([]*Version{v}, c.Versions...)
}

s := v.Change(section)
if s == nil {
s = NewChangeList("Added")
v.Changes = append(v.Changes, s)
}
item := &Item{
Description: message,
}
s.Items = append(s.Items, item)
}

// Release transforms Unreleased into the version informed
func (c *Changelog) Release(newVersion Version) (*Version, error) {
oldUnreleased := c.Version("Unreleased")
Expand Down
10 changes: 10 additions & 0 deletions chg/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func TestChangelogVersion(t *testing.T) {
})
}

func TestChangelogAddItem(t *testing.T) {
t.Run("empty-changelog", func(t *testing.T) {
c := Changelog{}
c.AddItem(Added, "my message")

assert.NotNil(t, c.Version("Unreleased"))
assert.NotNil(t, c.Version("Unreleased").Change(Added))
})
}

func TestChangelogRelease(t *testing.T) {
c := Changelog{
Versions: []*Version{
Expand Down
32 changes: 32 additions & 0 deletions cmd/added.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"bytes"
"strings"

"github.com/rcmachado/changelog/chg"
"github.com/rcmachado/changelog/parser"
"github.com/spf13/cobra"
)

var addedCmd = &cobra.Command{
Use: "added",
Short: "Add item under 'Added' section",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
input := readChangelog()

changelog := parser.Parse(input)
changelog.AddItem(chg.Added, strings.Join(args, " "))

var buf bytes.Buffer
changelog.Render(&buf)
output := buf.Bytes()

writeChangelog(output)
},
}

func init() {
rootCmd.AddCommand(addedCmd)
}

0 comments on commit f0345c7

Please sign in to comment.