feat: add --version flag to generate CHANGELOG.md

main
hau 2020-11-10 17:56:26 +07:00
parent dc386b27f4
commit 16bf3ab92a
4 changed files with 51 additions and 10 deletions

View File

@ -1,18 +1,27 @@
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/haunt98/changeloguru/pkg/changelog"
"github.com/haunt98/changeloguru/pkg/convention"
"github.com/haunt98/changeloguru/pkg/git"
"github.com/urfave/cli/v2"
"golang.org/x/mod/semver"
)
const (
currentPath = "."
currentPath = "."
changelogFile = "CHANGELOG.md"
fromFlag = "from"
toFlag = "to"
versionFlag = "version"
verboseFlag = "verbose"
)
@ -33,6 +42,10 @@ func main() {
Name: toFlag,
Usage: "to commit revision",
},
&cli.StringFlag{
Name: versionFlag,
Usage: "version",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
@ -69,6 +82,10 @@ func (a *action) Run(c *cli.Context) error {
conventionalCommits := a.getConventionalCommits(c, commits)
a.log("conventional commits %+v", conventionalCommits)
if err := a.generateChangelog(c, path, conventionalCommits); err != nil {
return err
}
return nil
}
@ -107,6 +124,26 @@ func (a *action) getConventionalCommits(c *cli.Context, commits []git.Commit) []
return conventionalCommits
}
func (a *action) generateChangelog(c *cli.Context, path string, commits []convention.Commit) error {
changelogPath := filepath.Join(path, changelogFile)
version := c.String(versionFlag)
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
if !semver.IsValid(version) {
return fmt.Errorf("invalid semver %s", version)
}
markdownGenerator := changelog.NewMarkdownGenerator(changelogPath, version, time.Now())
if err := markdownGenerator.Generate(commits); err != nil {
return err
}
return nil
}
func (a *action) log(format string, v ...interface{}) {
if a.verbose {
log.Printf(format, v...)

1
go.mod
View File

@ -10,5 +10,6 @@ require (
github.com/spf13/cobra v1.1.1
github.com/urfave/cli/v2 v2.3.0
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
golang.org/x/mod v0.3.0
gopkg.in/src-d/go-git.v4 v4.13.1 // indirect
)

6
go.sum
View File

@ -236,6 +236,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@ -256,7 +257,10 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.0 h1:sfUMP1Gu8qASkorDVjnMuvgJzwFbTZSeXFiGBYAVdl4=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@ -323,7 +327,9 @@ golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=

View File

@ -31,10 +31,7 @@ func NewMarkdownGenerator(path string, version string, t time.Time) *MarkdownGen
func (g *MarkdownGenerator) Generate(commits []convention.Commit) error {
lines := g.getLines(commits)
previousLines, err := g.getPreviousLines()
if err != nil {
return err
}
previousLines := g.getPreviousLines()
lines = append(lines, previousLines...)
@ -85,10 +82,10 @@ func (g *MarkdownGenerator) getLines(commits []convention.Commit) []string {
return lines
}
func (g *MarkdownGenerator) getPreviousLines() ([]string, error) {
func (g *MarkdownGenerator) getPreviousLines() []string {
prevData, err := ioutil.ReadFile(g.path)
if err != nil {
return nil, err
return nil
}
prevLines := strings.Split(string(prevData), "\n")
@ -102,11 +99,11 @@ func (g *MarkdownGenerator) getPreviousLines() ([]string, error) {
finalPrevLines = append(finalPrevLines, prevLine)
}
return finalPrevLines, nil
return finalPrevLines
}
func (g *MarkdownGenerator) writeLines(lines []string) error {
data := strings.Join(lines, "\n")
data := strings.Join(lines, "\n\n")
if err := ioutil.WriteFile(g.path, []byte(data), 0644); err != nil {
return err
}
@ -124,5 +121,5 @@ func (g *MarkdownGenerator) composeTypeHeader(t string) string {
}
func (g *MarkdownGenerator) composeListItem(text string) string {
return fmt.Sprintf("* %s", text)
return fmt.Sprintf("- %s", text)
}