2020-11-07 16:52:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-10 16:28:58 +00:00
|
|
|
"errors"
|
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 (
|
2020-11-10 16:16:51 +00:00
|
|
|
name = "changeloguru"
|
|
|
|
description = "generate changelog from conventional commits"
|
|
|
|
|
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
|
|
|
|
defaultOutputDir = currentDir
|
|
|
|
defaultFilename = "CHANGELOG"
|
|
|
|
defaultFiletype = markdownFiletype
|
|
|
|
defaultVersion = "0.1.0"
|
|
|
|
|
|
|
|
fromFlag = "from"
|
|
|
|
excludeToFlag = "exclude-to"
|
|
|
|
includeToFlag = "include-to"
|
|
|
|
versionFlag = "version"
|
|
|
|
repositoryFlag = "repository"
|
|
|
|
outputDirFlag = "output-dir"
|
|
|
|
filenameFlag = "filename"
|
|
|
|
filetypeFlag = "filetype"
|
|
|
|
verboseFlag = "verbose"
|
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 04:19:11 +00:00
|
|
|
Name: name,
|
|
|
|
Description: description,
|
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{
|
2020-11-10 16:28:58 +00:00
|
|
|
Name: excludeToFlag,
|
2020-12-17 10:34:50 +00:00
|
|
|
Usage: "generate to `COMMIT` (exclude that commit)",
|
2020-11-10 16:28:58 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: includeToFlag,
|
2020-12-17 10:34:50 +00:00
|
|
|
Usage: "generate to `COMMIT` (include that 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,
|
|
|
|
Usage: "`REPOSITORY` path, example /go/src/myapp",
|
|
|
|
DefaultText: defaultRepositry,
|
2020-12-18 08:47:08 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-12-18 09:21:59 +00:00
|
|
|
Name: outputDirFlag,
|
|
|
|
Usage: "`OUTPUT_DIR` path, example /go/src/myapp",
|
|
|
|
DefaultText: defaultOutputDir,
|
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,
|
|
|
|
Usage: "output `FILETYPE` support md (markdown)",
|
|
|
|
DefaultText: defaultFiletype,
|
2020-12-17 09:54:32 +00:00
|
|
|
},
|
2020-11-07 16:52:09 +00:00
|
|
|
&cli.BoolFlag{
|
2020-12-18 09:30:58 +00:00
|
|
|
Name: verboseFlag,
|
|
|
|
Usage: "show what is going on",
|
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 {
|
|
|
|
verbose bool
|
2020-11-30 11:19:05 +00:00
|
|
|
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
|
|
|
|
if c.NArg() == 0 && c.NumFlags() == 0 {
|
|
|
|
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
|
|
|
}
|
2020-11-10 08:33:19 +00:00
|
|
|
a.log("commits %+v", commits)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
conventionalCommits := a.getConventionalCommits(commits)
|
2020-11-10 08:33:19 +00:00
|
|
|
a.log("conventional commits %+v", conventionalCommits)
|
|
|
|
|
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) {
|
|
|
|
a.verbose = c.Bool(verboseFlag)
|
|
|
|
a.flags[fromFlag] = c.String(fromFlag)
|
|
|
|
a.flags[excludeToFlag] = c.String(excludeToFlag)
|
|
|
|
a.flags[includeToFlag] = c.String(includeToFlag)
|
2020-12-18 08:47:08 +00:00
|
|
|
a.flags[versionFlag] = a.getFlagValue(c, versionFlag, defaultVersion)
|
|
|
|
a.flags[repositoryFlag] = a.getFlagValue(c, repositoryFlag, defaultRepositry)
|
|
|
|
a.flags[outputDirFlag] = a.getFlagValue(c, outputDirFlag, defaultOutputDir)
|
|
|
|
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]
|
|
|
|
a.log("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]
|
2020-11-10 08:33:19 +00:00
|
|
|
a.log("from revision %s", fromRev)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
excludeToRev := a.flags[excludeToFlag]
|
2020-11-10 16:48:41 +00:00
|
|
|
a.log("exclude to revision %s", excludeToRev)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-11-30 11:19:05 +00:00
|
|
|
includeToRev := a.flags[includeToFlag]
|
2020-11-10 16:48:41 +00:00
|
|
|
a.log("include to revision %s", includeToRev)
|
2020-11-10 16:28:58 +00:00
|
|
|
|
|
|
|
if excludeToRev != "" && includeToRev != "" {
|
|
|
|
return nil, errors.New("excludeToFlag and includeToFlag can not appear same time")
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|
2020-11-10 16:28:58 +00:00
|
|
|
|
2020-11-10 16:33:35 +00:00
|
|
|
if excludeToRev != "" {
|
2020-11-10 16:28:58 +00:00
|
|
|
return r.LogExcludeTo(fromRev, excludeToRev)
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-10 08:33:19 +00:00
|
|
|
a.log("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 {
|
2020-12-18 08:47:08 +00:00
|
|
|
outputPath, _, filetype := a.getOutputPath()
|
2020-12-17 10:52:04 +00:00
|
|
|
|
|
|
|
version, err := a.getVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch filetype {
|
|
|
|
case markdownFiletype:
|
2020-12-18 08:47:08 +00:00
|
|
|
return a.generateMarkdownChangelog(outputPath, version, commits)
|
2020-12-17 10:52:04 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown filetype %s", filetype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:47:08 +00:00
|
|
|
func (a *action) getOutputPath() (string, string, string) {
|
|
|
|
outputDir := a.flags[outputDirFlag]
|
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
|
|
|
|
path := filepath.Join(outputDir, nameWithExt)
|
|
|
|
a.log("output path %s", path)
|
2020-12-17 10:52:04 +00:00
|
|
|
|
2020-12-18 08:47:08 +00:00
|
|
|
return path, 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
|
|
|
}
|
|
|
|
|
2020-12-17 10:53:38 +00:00
|
|
|
a.log("version %s", version)
|
|
|
|
|
2020-12-17 10:52:04 +00:00
|
|
|
return version, nil
|
2020-12-17 09:54:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:47:08 +00:00
|
|
|
func (a *action) generateMarkdownChangelog(outputPath, version string, commits []convention.Commit) error {
|
|
|
|
// If CHANGELOG file already exist
|
2020-11-27 17:36:34 +00:00
|
|
|
var oldData string
|
2020-12-18 08:47:08 +00:00
|
|
|
bytes, err := ioutil.ReadFile(outputPath)
|
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)
|
|
|
|
|
2020-12-24 10:52:21 +00:00
|
|
|
if err := ioutil.WriteFile(outputPath, []byte(newData), 0o644); err != nil {
|
2020-12-18 08:47:08 +00:00
|
|
|
return fmt.Errorf("failed to write file %s: %w", outputPath, err)
|
2020-11-10 10:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:33:19 +00:00
|
|
|
func (a *action) log(format string, v ...interface{}) {
|
|
|
|
if a.verbose {
|
|
|
|
log.Printf(format, v...)
|
|
|
|
}
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|