2020-11-07 16:52:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
currentPath = "."
|
|
|
|
fromFlag = "from"
|
|
|
|
toFlag = "to"
|
|
|
|
verboseFlag = "verbose"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-11-10 08:33:19 +00:00
|
|
|
a := &action{
|
|
|
|
verbose: false,
|
|
|
|
}
|
|
|
|
|
2020-11-07 16:52:09 +00:00
|
|
|
app := &cli.App{
|
|
|
|
Name: "changeloguru",
|
|
|
|
Usage: "description",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: fromFlag,
|
|
|
|
Usage: "from commit revision",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: toFlag,
|
|
|
|
Usage: "to commit revision",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "verbose",
|
|
|
|
Aliases: []string{"v"},
|
|
|
|
Usage: "show what is going on",
|
|
|
|
},
|
|
|
|
},
|
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 {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:33:19 +00:00
|
|
|
type action struct {
|
|
|
|
verbose bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) Run(c *cli.Context) error {
|
|
|
|
a.verbose = c.Bool(verboseFlag)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
|
|
|
path := currentPath
|
|
|
|
if c.NArg() > 0 {
|
|
|
|
path = c.Args().Get(0)
|
|
|
|
}
|
2020-11-10 08:33:19 +00:00
|
|
|
a.log("path %s", path)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-11-10 08:33:19 +00:00
|
|
|
commits, err := a.getCommits(c, path)
|
|
|
|
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-10 08:33:19 +00:00
|
|
|
conventionalCommits := a.getConventionalCommits(c, commits)
|
|
|
|
a.log("conventional commits %+v", conventionalCommits)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) getCommits(c *cli.Context, path string) ([]git.Commit, error) {
|
2020-11-07 16:52:09 +00:00
|
|
|
r, err := git.NewRepository(path)
|
|
|
|
if err != nil {
|
2020-11-10 08:33:19 +00:00
|
|
|
return nil, err
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fromRev := c.String(fromFlag)
|
2020-11-10 08:33:19 +00:00
|
|
|
a.log("from revision %s", fromRev)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
|
|
|
toRev := c.String(toFlag)
|
2020-11-10 08:33:19 +00:00
|
|
|
a.log("to revision %s", toRev)
|
2020-11-07 16:52:09 +00:00
|
|
|
|
|
|
|
commits, err := r.LogExcludeTo(fromRev, toRev)
|
|
|
|
if err != nil {
|
2020-11-10 08:33:19 +00:00
|
|
|
return nil, err
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|
2020-11-10 08:33:19 +00:00
|
|
|
return commits, nil
|
|
|
|
}
|
2020-11-07 16:52:09 +00:00
|
|
|
|
2020-11-10 08:33:19 +00:00
|
|
|
func (a *action) getConventionalCommits(c *cli.Context, commits []git.Commit) []convention.Commit {
|
2020-11-09 08:46:42 +00:00
|
|
|
conventionalCommits := make([]convention.Commit, 0, len(commits))
|
2020-11-10 08:33:19 +00:00
|
|
|
|
2020-11-09 08:46:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) log(format string, v ...interface{}) {
|
|
|
|
if a.verbose {
|
|
|
|
log.Printf(format, v...)
|
|
|
|
}
|
2020-11-07 16:52:09 +00:00
|
|
|
}
|