2023-10-15 19:29:29 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-15 22:29:02 +00:00
|
|
|
"os"
|
2023-10-15 19:29:29 +00:00
|
|
|
"time"
|
|
|
|
|
2023-10-16 19:41:33 +00:00
|
|
|
"github.com/thegeeklab/git-sv/app"
|
2023-10-15 19:29:29 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
func CommitNotesFlags(settings *app.CommitNotesSettings) []cli.Flag {
|
2023-10-15 19:29:29 +00:00
|
|
|
return []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "r", Aliases: []string{"range"},
|
2023-10-15 22:29:02 +00:00
|
|
|
Usage: "type of range of commits, use: tag, date or hash",
|
|
|
|
Required: true,
|
|
|
|
Destination: &settings.Range,
|
2023-10-15 19:29:29 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2023-10-15 22:29:02 +00:00
|
|
|
Name: "s",
|
|
|
|
Aliases: []string{"start"},
|
|
|
|
Usage: "start range of git log revision range, if date, the value is used on since flag instead",
|
|
|
|
Destination: &settings.Start,
|
2023-10-15 19:29:29 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2023-10-15 22:29:02 +00:00
|
|
|
Name: "e",
|
|
|
|
Aliases: []string{"end"},
|
|
|
|
Usage: "end range of git log revision range, if date, the value is used on until flag instead",
|
|
|
|
Destination: &settings.End,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "o",
|
|
|
|
Aliases: []string{"output"},
|
|
|
|
Usage: "output file name. Omit to use standard output.",
|
|
|
|
Destination: &settings.Out,
|
2023-10-15 19:29:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 07:52:28 +00:00
|
|
|
func CommitNotesHandler(g app.GitSV, settings *app.CommitNotesSettings) cli.ActionFunc {
|
2024-02-12 08:09:42 +00:00
|
|
|
return func(_ *cli.Context) error {
|
2023-10-15 19:29:29 +00:00
|
|
|
var date time.Time
|
|
|
|
|
2023-10-17 07:52:28 +00:00
|
|
|
lr, err := logRange(g, settings.Range, settings.Start, settings.End)
|
2023-10-15 19:29:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
commits, err := g.Log(lr)
|
|
|
|
if err != nil {
|
2023-10-17 07:52:28 +00:00
|
|
|
return fmt.Errorf("error getting git log from range: %s: %w", settings.Range, err)
|
2023-10-15 19:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(commits) > 0 {
|
|
|
|
date, _ = time.Parse("2006-01-02", commits[0].Date)
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := g.OutputFormatter.FormatReleaseNote(g.ReleasenotesProcessor.Create(nil, "", date, commits))
|
|
|
|
if err != nil {
|
2023-10-15 22:29:02 +00:00
|
|
|
return fmt.Errorf("could not format commit notes: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-10-17 07:52:28 +00:00
|
|
|
if settings.End == "" {
|
2023-10-15 22:29:02 +00:00
|
|
|
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-17 07:52:28 +00:00
|
|
|
w, err := os.Create(settings.End)
|
2023-10-15 22:29:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write commit notes: %w", err)
|
2023-10-15 19:29:29 +00:00
|
|
|
}
|
2023-10-15 22:29:02 +00:00
|
|
|
defer w.Close()
|
2023-10-15 19:29:29 +00:00
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
if _, err := w.Write(output); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-15 19:29:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|