diff --git a/cmd/get.go b/cmd/get.go index f7a88ce..a30ac38 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -13,6 +13,32 @@ import ( "github.com/spf13/cobra" ) +type outputEnum string + +const ( + outputStandard outputEnum = "standard" + outputNotes outputEnum = "notes" +) + +func (e *outputEnum) String() string { + return string(*e) +} + +func (e *outputEnum) Set(v string) error { + switch v { + case string(outputStandard), string(outputNotes): + *e = outputEnum(v) + return nil + default: + return fmt.Errorf(`must be one of %s or %s`, outputStandard, outputNotes) + } +} + +func (e *outputEnum) Type() string { + return "outputEnum" +} + +var outputTemplate = outputStandard var printLatest bool var printVersion string @@ -36,6 +62,7 @@ This command is useful for creating and updating Release notes in GitHub. RunE: func(command *cobra.Command, args []string) error { fileName := configuration.Config.FileName + var tmplSrc string var changelog changelog.Changelog var err error @@ -43,16 +70,25 @@ This command is useful for creating and updating Release notes in GitHub. changelog, err = get.GetLatest(fileName) } else if printVersion != "" { changelog, err = get.GetVersion(fileName, printVersion) + } else if outputTemplate == outputNotes { + err = fmt.Errorf("notes output only supported with latest or version") } else { changelog, err = get.GetAll(fileName) } + switch outputTemplate { + case outputStandard: + tmplSrc = writer.TmplSrcStandard + case outputNotes: + tmplSrc = writer.TmplSrcNotes + } + if err != nil { return err } var buf bytes.Buffer - if err := writer.Write(&buf, changelog); err != nil { + if err := writer.Write(&buf, tmplSrc, changelog); err != nil { return err } @@ -77,5 +113,11 @@ func init() { "Prints a specific version from the changelog to stdout.", ) + getCmd.Flags().Var( + &outputTemplate, + "output", + fmt.Sprintf(`Output template. allowed: "%s" or "%s"`, outputStandard, outputNotes), + ) + getCmd.Flags().SortFlags = false } diff --git a/cmd/new.go b/cmd/new.go index af3ad1c..dd94c95 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -45,7 +45,7 @@ var newCmd = &cobra.Command{ return err } - if err := writer.Write(f, changelog); err != nil { + if err := writer.Write(f, writer.TmplSrcStandard, changelog); err != nil { return err } diff --git a/internal/writer/writer.go b/internal/writer/writer.go index 20fd71b..c4e509f 100644 --- a/internal/writer/writer.go +++ b/internal/writer/writer.go @@ -11,7 +11,7 @@ import ( "github.com/chelnak/gh-changelog/pkg/changelog" ) -var tmplSrc = ` +const tmplStandard = ` # Changelog All notable changes to this project will be documented in this file. @@ -79,7 +79,57 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a {{- end}} ` -func Write(writer io.Writer, changelog changelog.Changelog) error { +const tmplNotes = `{{range .GetEntries }} +{{- if .Security }} +### Security +{{range .Security}} +- {{.}} +{{- end}} +{{end}} +{{- if .Changed }} +### Changed +{{range .Changed}} +- {{.}} +{{- end}} +{{end}} +{{- if .Removed }} +### Removed +{{range .Removed}} +- {{.}} +{{- end}} +{{end}} +{{- if .Deprecated }} +### Deprecated +{{range .Deprecated}} +- {{.}} +{{- end}} +{{end}} +{{- if .Added }} +### Added +{{range .Added}} +- {{.}} +{{- end}} +{{end}} +{{- if .Fixed }} +### Fixed +{{range .Fixed}} +- {{.}} +{{- end}} +{{end}} +{{- if .Other }} +### Other +{{range .Other}} +- {{.}} +{{- end}} +{{end}} +{{- end}}` + +const ( + TmplSrcStandard = tmplStandard + TmplSrcNotes = tmplNotes +) + +func Write(writer io.Writer, tmplSrc string, changelog changelog.Changelog) error { tmpl, err := template.New("changelog").Funcs(template.FuncMap{ "getFirstCommit": func() string { git := gitclient.NewGitClient(exec.Command) diff --git a/internal/writer/writer_test.go b/internal/writer/writer_test.go index 302afa4..c7908b6 100644 --- a/internal/writer/writer_test.go +++ b/internal/writer/writer_test.go @@ -40,7 +40,7 @@ func Test_ItWritesOutAChangelogInTheCorrectFormat(t *testing.T) { mockChangelog.AddUnreleased([]string{"Unreleased 1", "Unreleased 2"}) var buf bytes.Buffer - err := writer.Write(&buf, mockChangelog) + err := writer.Write(&buf, writer.TmplSrcStandard, mockChangelog) assert.NoError(t, err) @@ -58,4 +58,12 @@ func Test_ItWritesOutAChangelogInTheCorrectFormat(t *testing.T) { assert.Regexp(t, "### Other", buf.String()) assert.Regexp(t, "- Other 1", buf.String()) assert.Regexp(t, "- Other 2", buf.String()) + + buf.Reset() + err = writer.Write(&buf, writer.TmplSrcNotes, mockChangelog) + + assert.NoError(t, err) + + assert.NotRegexp(t, regexp.MustCompile(`## \[v1.0.0\]\(https:\/\/github.com\/repo-owner\/repo-name\/tree\/v1.0.0\)`), buf.String()) + assert.NotRegexp(t, regexp.MustCompile(`\[Full Changelog\]\(https:\/\/github.com\/repo-owner\/repo-name\/compare\/v0.9.0\.\.\.v1.0.0\)`), buf.String()) }