Skip to content

Commit

Permalink
feat: allow to override file template (#37)
Browse files Browse the repository at this point in the history
* feat: add option to clean files not generated by releasepost

Signed-off-by: Olivier Vernin <[email protected]>

* feat: allow to override file template

Signed-off-by: Olivier Vernin <[email protected]>

---------

Signed-off-by: Olivier Vernin <[email protected]>
  • Loading branch information
olblak authored May 17, 2024
1 parent 8a3e211 commit 684e49e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 65 deletions.
50 changes: 45 additions & 5 deletions config.example.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
changelogs:
- kind: github
dir: dist
dir: dist/0.1
formats:
- extension: asciidoc
frontmatters: |
Expand All @@ -24,13 +24,53 @@ changelogs:
title: "Index Changelog"
---
spec:
# No release
# owner: updatecli-test
#owner: olblak
#repository: nocode
owner: updatecli
repository: udash
versionfilter:
kind: semver
pattern: ~0.1
- kind: github
dir: dist/0.2
formats:
- extension: asciidoc
frontmatters: |
---
title: "{{ .Changelog.Name }}"
date: {{ .Changelog.PublishedAt }}
---
filetemplate: |
{{ .FrontMatters }}
// Disclaimer: this file is generated, do not edit it manually.
---
{{ if .Changelog.DescriptionHTML }}
++++
{{ .Changelog.DescriptionHTML }}
++++
{{ else if .Changelog.Description}}
{{ .Changelog.Description }}
{{ end}}
---
indexfrontmatters: |
---
title: "Index Changelog"
---
indexfiletemplate: |
{{ .FrontMatters }}
// Disclaimer: this file is generated, do not edit it manually.
[cols="1,1,1" options="header" frame="ends" grid="rows"]
|===
| Name | Author | Published Time
{{ range $pos, $release := .Changelogs }}
| link:{{ $release.Tag }}[{{ $release.Name}}{{ if (eq $pos 0) }}(latest){{ end}}] | {{ $release.Author }} | {{ $release.PublishedAt }}
{{ end }}
|===
spec:
owner: updatecli
repository: udash
versionfilter:
kind: semver
pattern: ~0.2

48 changes: 17 additions & 31 deletions internal/core/changelog/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,25 @@ var (
nameAsciidoc = "asciidoc"
)

/*
Config contains various information used to configure the way changelogs are retrieved
*/
// Config contains various information used to configure the way changelogs are retrieved
type Config struct {
/*
Name specifies the name of the changelog to retrieve. It is only used for identification purposes.
*/
// Name specifies the name of the changelog to retrieve. It is only used for identification purposes.
Name string
/*
dir specifies the directory where changelog files will be repost.
*/
// dir specifies the directory where changelog files will be repost.
Dir string
/*
kind specifies the kind of changelog to retrieve.
accepted values:
* github
*/
// kind specifies the kind of changelog to retrieve.
//
// accepted values:
// * github
//
Kind string
/*
format specifies the format of the changelog to generate.
*/
// format specifies the format of the changelog to generate.
Formats []ConfigFormat
/*
spec specifies the configuration input for a specific kind of changelog.
*/
// spec specifies the configuration input for a specific kind of changelog.
Spec interface{}
}

/*
Sanitize ensures that the configuration is valid and that all required fields are set.
*/
// Sanitize ensures that the configuration is valid and that all required fields are set.
func (c *Config) Sanitize(configFile string) error {

if c.Name != "" {
Expand Down Expand Up @@ -101,9 +88,7 @@ func (c *Config) Sanitize(configFile string) error {
return nil
}

/*
SaveToDisk saves one changelog per file per format to disk
*/
// SaveToDisk saves one changelog per file per format to disk
func (c Config) SaveToDisk(changelogs []Spec) error {

fmt.Println("Generating changelogs")
Expand Down Expand Up @@ -134,7 +119,7 @@ func (c Config) SaveToDisk(changelogs []Spec) error {
defaultChangelogDir,
changelogs[i].Tag+".adoc",
)
if err = toAsciidocFile(data, filename); err != nil {
if err = toAsciidocFile(data, filename, format.FileTemplate); err != nil {
fmt.Printf("creating asciidoc file %s: %v", filename, err)
continue
}
Expand All @@ -156,7 +141,7 @@ func (c Config) SaveToDisk(changelogs []Spec) error {
defaultChangelogDir,
changelogs[i].Tag+".md",
)
if err = toMarkdownFile(data, filename); err != nil {
if err = toMarkdownFile(data, filename, format.FileTemplate); err != nil {
fmt.Printf("creating markdown file %s: %v", filename, err)
continue
}
Expand Down Expand Up @@ -205,8 +190,9 @@ func (c Config) SaveIndexToDisk(changelogs []Spec) error {

switch format.Extension {
case nameAsciidoc:

indexFileName := format.IndexFileName + ".adoc"
if err := toIndexAsciidocFile(data, filepath.Join(c.Dir, indexFileName)); err != nil {
if err := toIndexAsciidocFile(data, filepath.Join(c.Dir, indexFileName), format.IndexFileTemplate); err != nil {
fmt.Printf("creating index asciidoc file %s: %v\n", filepath.Join(c.Dir, indexFileName), err)
}

Expand Down Expand Up @@ -235,7 +221,7 @@ func (c Config) SaveIndexToDisk(changelogs []Spec) error {

case nameMarkdown:
indexFileName := format.IndexFileName + ".md"
if err := toIndexMarkdownFile(data, filepath.Join(c.Dir, indexFileName)); err != nil {
if err := toIndexMarkdownFile(data, filepath.Join(c.Dir, indexFileName), format.IndexFileTemplate); err != nil {
fmt.Printf("creating index markdown file %s: %v", filepath.Join(c.Dir, indexFileName), err)
continue
}
Expand Down
57 changes: 36 additions & 21 deletions internal/core/changelog/configFormat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,45 @@ var (
)

type ConfigFormat struct {
/*
Extension is the file extension used for the changelog file.
accepted values:
* markdown
* json
default:
* markdown
*/
//
// Extension is the file extension used for the changelog file.
//
// accepted values:
// * markdown
// * json
// * asciidoc
//
// default: markdown
//
Extension string
/*
indexFileName is the name of the index file name without the extension.
default:
* _index
*/
// FileTemplate is the template used to generate the changelog file
//
// default: depends on the extension
//
// remark: This setting is useless for json files
//
FileTemplate string
// indexFileName is the name of the index file name without the extension.
//
// default: '_index'
//
IndexFileName string
/*
indexFrontMatters is the front matters using yaml syntax to add to the index file.
*/
// IndexFileTemplate is the template used to generate the index file
//
// default: depends on the extension
//
// remark: This setting is useless for json files
//
IndexFileTemplate string
// indexFrontMatters is the front matters using yaml syntax to add to the index file.
//
// default: empty
//
IndexFrontMatters string
/*
frontmatters is the front matters using yaml syntax to add to the changelog file.
*/
// frontmatters is the front matters using yaml syntax to add to the changelog file.
//
// default: empty
//
FrontMatters string
}

Expand Down
16 changes: 12 additions & 4 deletions internal/core/changelog/format_asciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ __Information retrieved from link:{{ .Changelog.URL }}[here]__
`
)

func toAsciidocFile(data ReleaseData, filename string) error {
func toAsciidocFile(data ReleaseData, filename string, fileTemplate string) error {

tmpl, err := template.New("asciidoc").Parse(asciidocTemplate)
if fileTemplate == "" {
fileTemplate = asciidocTemplate
}

tmpl, err := template.New("asciidoc").Parse(fileTemplate)
if err != nil {
return fmt.Errorf("parsing template: %v", err)
}
Expand All @@ -83,11 +87,15 @@ func toAsciidocFile(data ReleaseData, filename string) error {
return nil
}

func toIndexAsciidocFile(data IndexData, filename string) error {
func toIndexAsciidocFile(data IndexData, filename string, fileTemplate string) error {

if fileTemplate == "" {
fileTemplate = indexAsciidocTemplate
}

tmpl, err := template.New("asciidoc").
Funcs(sprig.FuncMap()).
Parse(indexAsciidocTemplate)
Parse(fileTemplate)
if err != nil {
return fmt.Errorf("parsing template: %v", err)
}
Expand Down
16 changes: 12 additions & 4 deletions internal/core/changelog/format_markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ var (
`
)

func toMarkdownFile(data ReleaseData, filename string) error {
func toMarkdownFile(data ReleaseData, filename string, fileTemplate string) error {

if fileTemplate == "" {
fileTemplate = markdownTemplate
}

tmpl, err := template.New("markdown").
Funcs(sprig.FuncMap()).
Parse(markdownTemplate)
Parse(fileTemplate)
if err != nil {
return fmt.Errorf("parsing template: %v", err)
}
Expand All @@ -67,9 +71,13 @@ func toMarkdownFile(data ReleaseData, filename string) error {
return nil
}

func toIndexMarkdownFile(data IndexData, filename string) error {
func toIndexMarkdownFile(data IndexData, filename string, fileTemplate string) error {

if fileTemplate == "" {
fileTemplate = indexMarkownTemplate
}

tmpl, err := template.New("markdown").Parse(indexMarkownTemplate)
tmpl, err := template.New("markdown").Parse(fileTemplate)
if err != nil {
return fmt.Errorf("parsing template: %v", err)
}
Expand Down

0 comments on commit 684e49e

Please sign in to comment.