2020-11-07 16:52:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-10 10:56:26 +00:00
|
|
|
"fmt"
|
2020-11-27 17:36:34 +00:00
|
|
|
"io/ioutil"
|
2020-11-07 16:52:09 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-11-10 10:56:26 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-12-24 10:52:21 +00:00
|
|
|
"github.com/fatih/color"
|
2020-11-10 10:56:26 +00:00
|
|
|
"github.com/haunt98/changeloguru/pkg/changelog"
|
2020-11-09 08:46:42 +00:00
|
|
|
"github.com/haunt98/changeloguru/pkg/convention"
|
2020-11-07 16:52:09 +00:00
|
|
|
"github.com/haunt98/changeloguru/pkg/git"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-11-10 10:56:26 +00:00
|
|
|
"golang.org/x/mod/semver"
|
2020-11-07 16:52:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-05 08:48:09 +00:00
|
|
|
name = "changeloguru"
|
2020-11-10 16:16:51 +00:00
|
|
|
|
2020-12-18 08:47:08 +00:00
|
|
|
currentDir = "."
|
2020-12-17 09:54:32 +00:00
|
|
|
markdownFiletype = "md"
|
2020-12-17 10:52:04 +00:00
|
|
|
|
2020-12-18 08:47:08 +00:00
|
|
|
defaultRepositry = currentDir
|
2021-01-05 08:51:03 +00:00
|
|
|
defaultOutput = currentDir
|
2020-12-18 08:47:08 +00:00
|
|
|
defaultFilename = "CHANGELOG"
|
|
|
|
defaultFiletype = markdownFiletype
|
|
|
|
defaultVersion = "0.1.0"
|
|
|
|
|
|
|
|
fromFlag = "from"
|
2021-01-05 07:16:00 +00:00
|
|
|
toFlag = "to"
|
2020-12-18 08:47:08 +00:00
|
|
|
versionFlag = "version"
|
|
|
|
repositoryFlag = "repository"
|
2021-01-05 08:51:03 +00:00
|
|
|
outputFlag = "output"
|
2020-12-18 08:47:08 +00:00
|
|
|
filenameFlag = "filename"
|
|
|
|
filetypeFlag = "filetype"
|
2021-01-05 07:02:49 +00:00
|
|
|
debugFlag = "debug"
|
2020-11-07 16:52:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-11-10 08:33:19 +00:00
|
|
|
a := &action{
|
2020-12-18 08:47:08 +00:00
|
|
|
flags: make(map[string]string),
|
|
|
|
args: make(map[string]string),
|
2020-11-10 08:33:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 16:52:09 +00:00
|
|
|
app := &cli.App{
|
2021-01-05 08:48:09 +00:00
|
|
|
Name: name,
|
|
|
|
Usage: "generate changelog from conventional commits",
|
2020-11-07 16:52:09 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: fromFlag,
|
2020-12-17 10:34:50 +00:00
|
|
|
Usage: "generate from `COMMIT`",
|
2020-11-07 16:52:09 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2021-01-05 07:16:00 +00:00
|
|
|
Name: toFlag,
|
2021-01-05 08:48:09 +00:00
|
|
|
Usage: "generate to `COMMIT`",
|
2020-11-07 16:52:09 +00:00
|
|
|
},
|
2020-11-10 10:56:26 +00:00
|
|
|
&cli.StringFlag{
|
2020-12-18 09:21:59 +00:00
|
|
|
Name: versionFlag,
|
|
|
|
Usage: "`VERSION` to generate, follow Semantic Versioning, example 1.0.0",
|
|
|
|
DefaultText: defaultVersion,
|
2020-12-18 08:47:08 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-12-18 09:21:59 +00:00
|
|
|
Name: repositoryFlag,
|
2021-01-05 08:51:03 +00:00
|
|
|
Usage: "`REPOSITORY` directory path, example /go/src/myapp",
|
2020-12-18 09:21:59 +00:00
|
|
|
DefaultText: defaultRepositry,
|
2020-12-18 08:47:08 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2021-01-05 08:51:03 +00:00
|
|
|
Name: outputFlag,
|
|
|
|
Usage: "`OUTPUT` directory path, example /go/src/myapp",
|
|
|
|
DefaultText: defaultOutput,
|
2020-11-10 10:56:26 +00:00
|
|
|
},
|
2020-12-17 09:54:32 +00:00
|
|
|
&cli.StringFlag{
|
2020-12-18 09:21:59 +00:00
|
|
|
Name: filenameFlag,
|
|
|
|
Usage: "output `FILENAME`",
|
|
|
|
DefaultText: defaultFilename,
|
2020-12-17 09:54:32 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-12-18 09:21:59 +00:00
|
|
|
Name: filetypeFlag,
|
2021-01-05 08:55:52 +00:00
|
|
|
Usage: "output `FILETYPE`",
|
2020-12-18 09:21:59 +00:00
|
|
|
DefaultText: defaultFiletype,
|
2020-12-17 09:54:32 +00:00
|
|
|
},
|
2020-11-07 16:52:09 +00:00
|
|
|
&cli.BoolFlag{
|
2021-01-05 07:02:49 +00:00
|
|
|
Name: debugFlag,
|
|
|
|
Aliases: []string{"-d"},
|
|
|
|
Usage: "Show debugging info",
|
2020-11-07 16:52:09 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-10 08:33:19 +00:00
|
|
|
Action: a.Run,
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
2020-12-24 10:52:21 +00:00
|
|
|
// Highlight error
|
|
|
|
fmtErr := color.New(color.FgRed)
|
|
|
|
fmtErr.Printf("[%s error]: ", name)
|
|
|
|
fmt.Printf("%s\n", err.Error())
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:33:19 +00:00
|
|
|
type action struct {
|
2021-01-05 07:02:49 +00:00
|
|
|
debug bool
|
|
|
|
flags map[string]string
|
|
|
|
args map[string]string
|
2020-11-10 08:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) Run(c *cli.Context) error {
|
2020-12-18 09:46:17 +00:00
|
|
|
// Show help if there is nothing
|
2021-01-05 08:51:39 +00:00
|
|
|
if c.NumFlags() == 0 {
|
2020-12-18 09:46:17 +00:00
|
|
|
return cli.ShowAppHelp(c)
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:47:08 +00:00
|
|
|
// Set up
|
2020-11-30 11:19:05 +00:00
|
|
|
a.getFlags(c)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
commits, err := a.getCommits()
|
2020-11-10 08:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|
2021-01-05 07:02:49 +00:00
|
|
|
a.logDebug("commits %+v", commits)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
conventionalCommits := a.getConventionalCommits(commits)
|
2021-01-05 07:02:49 +00:00
|
|
|
a.logDebug("conventional commits %+v", conventionalCommits)
|
2020-11-10 08:33:19 +00:00
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
if err := a.generateChangelog(conventionalCommits); err != nil {
|
2020-11-10 10:56:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:33:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
func (a *action) getFlags(c *cli.Context) {
|
2021-01-05 07:02:49 +00:00
|
|
|
a.debug = c.Bool(debugFlag)
|
2020-11-30 11:19:05 +00:00
|
|
|
a.flags[fromFlag] = c.String(fromFlag)
|
2021-01-05 07:16:00 +00:00
|
|
|
a.flags[toFlag] = c.String(toFlag)
|
2020-12-18 08:47:08 +00:00
|
|
|
a.flags[versionFlag] = a.getFlagValue(c, versionFlag, defaultVersion)
|
|
|
|
a.flags[repositoryFlag] = a.getFlagValue(c, repositoryFlag, defaultRepositry)
|
2021-01-05 08:51:03 +00:00
|
|
|
a.flags[outputFlag] = a.getFlagValue(c, outputFlag, defaultOutput)
|
2020-12-18 08:47:08 +00:00
|
|
|
a.flags[filenameFlag] = a.getFlagValue(c, filenameFlag, defaultFilename)
|
|
|
|
a.flags[filetypeFlag] = a.getFlagValue(c, filetypeFlag, defaultFiletype)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) getFlagValue(c *cli.Context, flag, fallback string) string {
|
|
|
|
value := c.String(flag)
|
|
|
|
if value == "" {
|
|
|
|
value = fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
2020-11-30 11:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) getCommits() ([]git.Commit, error) {
|
2020-12-18 08:47:08 +00:00
|
|
|
repository := a.flags[repositoryFlag]
|
2021-01-05 07:02:49 +00:00
|
|
|
a.logDebug("repository %s", repository)
|
2020-11-30 11:19:05 +00:00
|
|
|
|
2020-12-18 08:47:08 +00:00
|
|
|
r, err := git.NewRepository(repository)
|
2020-11-07 16:52:09 +00:00
|
|
|
if err != nil {
|
2020-11-10 08:33:19 +00:00
|
|
|
return nil, err
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
fromRev := a.flags[fromFlag]
|
2021-01-05 07:02:49 +00:00
|
|
|
a.logDebug("from revision %s", fromRev)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2021-01-05 07:16:00 +00:00
|
|
|
includeToRev := a.flags[toFlag]
|
|
|
|
a.logDebug("to revision %s", includeToRev)
|
2020-11-10 16:28:58 +00:00
|
|
|
|
2020-11-10 16:33:35 +00:00
|
|
|
if includeToRev != "" {
|
|
|
|
return r.LogIncludeTo(fromRev, includeToRev)
|
2020-11-10 16:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r.Log(fromRev)
|
2020-11-10 08:33:19 +00:00
|
|
|
}
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
func (a *action) getConventionalCommits(commits []git.Commit) []convention.Commit {
|
2020-11-09 08:46:42 +00:00
|
|
|
conventionalCommits := make([]convention.Commit, 0, len(commits))
|
|
|
|
for _, commit := range commits {
|
|
|
|
conventionalCommit, err := convention.NewCommit(commit)
|
|
|
|
if err != nil {
|
2021-01-05 07:02:49 +00:00
|
|
|
a.logDebug("failed to new conventional commits %+v: %s", commit, err)
|
2020-11-09 08:46:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
conventionalCommits = append(conventionalCommits, conventionalCommit)
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:33:19 +00:00
|
|
|
return conventionalCommits
|
|
|
|
}
|
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
func (a *action) generateChangelog(commits []convention.Commit) error {
|
2021-01-05 08:55:08 +00:00
|
|
|
realOutput, _, filetype := a.getRealOutput()
|
2020-12-17 10:52:04 +00:00
|
|
|
|
|
|
|
version, err := a.getVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch filetype {
|
|
|
|
case markdownFiletype:
|
2021-01-05 08:55:08 +00:00
|
|
|
return a.generateMarkdownChangelog(realOutput, version, commits)
|
2020-12-17 10:52:04 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown filetype %s", filetype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:55:08 +00:00
|
|
|
func (a *action) getRealOutput() (string, string, string) {
|
|
|
|
output := a.flags[outputFlag]
|
2020-12-17 09:54:32 +00:00
|
|
|
filename := a.flags[filenameFlag]
|
|
|
|
filetype := a.flags[filetypeFlag]
|
2020-12-17 10:53:38 +00:00
|
|
|
|
2020-12-18 08:47:08 +00:00
|
|
|
nameWithExt := filename + "." + filetype
|
2021-01-05 08:55:08 +00:00
|
|
|
realOutput := filepath.Join(output, nameWithExt)
|
|
|
|
a.logDebug("output path %s", realOutput)
|
2020-12-17 10:52:04 +00:00
|
|
|
|
2021-01-05 08:55:08 +00:00
|
|
|
return realOutput, filename, filetype
|
2020-12-17 10:52:04 +00:00
|
|
|
}
|
2020-11-10 10:56:26 +00:00
|
|
|
|
2020-12-17 10:52:04 +00:00
|
|
|
func (a *action) getVersion() (string, error) {
|
2020-11-30 11:19:05 +00:00
|
|
|
version := a.flags[versionFlag]
|
2020-11-10 10:56:26 +00:00
|
|
|
if !strings.HasPrefix(version, "v") {
|
|
|
|
version = "v" + version
|
|
|
|
}
|
2020-12-17 10:52:04 +00:00
|
|
|
|
2020-11-10 10:56:26 +00:00
|
|
|
if !semver.IsValid(version) {
|
2020-12-17 10:52:04 +00:00
|
|
|
return "", fmt.Errorf("invalid semver %s", version)
|
2020-11-10 10:56:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 07:02:49 +00:00
|
|
|
a.logDebug("version %s", version)
|
2020-12-17 10:53:38 +00:00
|
|
|
|
2020-12-17 10:52:04 +00:00
|
|
|
return version, nil
|
2020-12-17 09:54:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 08:55:08 +00:00
|
|
|
func (a *action) generateMarkdownChangelog(output, version string, commits []convention.Commit) error {
|
2020-12-18 08:47:08 +00:00
|
|
|
// If CHANGELOG file already exist
|
2020-11-27 17:36:34 +00:00
|
|
|
var oldData string
|
2021-01-05 08:55:08 +00:00
|
|
|
bytes, err := ioutil.ReadFile(output)
|
2020-11-27 17:36:34 +00:00
|
|
|
if err == nil {
|
|
|
|
oldData = string(bytes)
|
|
|
|
}
|
2020-11-10 10:56:26 +00:00
|
|
|
|
2020-11-27 17:36:34 +00:00
|
|
|
markdownGenerator := changelog.NewMarkdownGenerator(oldData, version, time.Now())
|
|
|
|
newData := markdownGenerator.Generate(commits)
|
|
|
|
|
2021-01-05 08:55:08 +00:00
|
|
|
if err := ioutil.WriteFile(output, []byte(newData), 0o644); err != nil {
|
|
|
|
return fmt.Errorf("failed to write file %s: %w", output, err)
|
2020-11-10 10:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:02:49 +00:00
|
|
|
func (a *action) logDebug(format string, v ...interface{}) {
|
|
|
|
if a.debug {
|
2020-11-10 08:33:19 +00:00
|
|
|
log.Printf(format, v...)
|
|
|
|
}
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|