Skip to content

Commit 3da2dd4

Browse files
authored
Provide custom template support for render (#118)
1 parent 7376d54 commit 3da2dd4

File tree

9 files changed

+116
-20
lines changed

9 files changed

+116
-20
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: feature
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Custom template support for render
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
#description:
19+
20+
# Affected component; a word indicating the component this changeset affects.
21+
component:
22+
23+
# PR number; optional; the PR number that added the changeset.
24+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
25+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
26+
# Please provide it if you are adding a fragment for a different PR.
27+
#pr: 1234
28+
29+
# Issue number; optional; the GitHub issue related to this changeset (either closes or is part of).
30+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
31+
#issue: 1234

cmd/render.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,27 @@ import (
88
"fmt"
99
"log"
1010

11+
"github.com/elastic/elastic-agent-changelog-tool/internal/assets"
1112
"github.com/elastic/elastic-agent-changelog-tool/internal/changelog"
1213
"github.com/spf13/afero"
1314
"github.com/spf13/cobra"
1415
"github.com/spf13/viper"
1516
)
1617

18+
var RenderLongDescription = fmt.Sprintf(`Use this command to render the consolidated changelog.
19+
20+
--version is required. Consolidated changelog version (x.y.z) in 'changelogs' folder
21+
--template is optional. Specify full path to your template file or use predefined templates. Default: asciidoc-embedded
22+
23+
Predefined templates:
24+
%s
25+
`, assets.GetEmbeddedTemplates().String())
26+
1727
func RenderCmd(fs afero.Fs) *cobra.Command {
1828
renderCmd := &cobra.Command{
1929
Use: "render",
2030
Short: "Render a changelog in an asciidoc file",
21-
Long: "Render a changelog in an asciidoc file",
31+
Long: RenderLongDescription,
2232
Args: func(cmd *cobra.Command, args []string) error {
2333
return nil
2434
},
@@ -31,12 +41,17 @@ func RenderCmd(fs afero.Fs) *cobra.Command {
3141
return fmt.Errorf("error parsing flag 'version': %w", err)
3242
}
3343

44+
template, err := cmd.Flags().GetString("template")
45+
if err != nil {
46+
return fmt.Errorf("error parsing flag 'template': %w", err)
47+
}
48+
3449
c, err := changelog.FromFile(fs, fmt.Sprintf("./%s/%s.yaml", dest, version))
3550
if err != nil {
3651
return fmt.Errorf("error loading changelog from file: %w", err)
3752
}
3853

39-
r := changelog.NewRenderer(fs, c, renderedDest)
54+
r := changelog.NewRenderer(fs, c, renderedDest, template)
4055

4156
if err := r.Render(); err != nil {
4257
return fmt.Errorf("cannot build asciidoc file: %w", err)
@@ -46,6 +61,7 @@ func RenderCmd(fs afero.Fs) *cobra.Command {
4661
},
4762
}
4863

64+
renderCmd.Flags().String("template", viper.GetString("template"), "The template used to generate the changelog")
4965
renderCmd.Flags().String("version", "", "The version of the consolidated changelog being created")
5066
err := renderCmd.MarkFlagRequired("version")
5167
if err != nil {

config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
owner: elastic
22
repo: elastic-agent-changelog-tool
3-
components: [elastic-agent-changelog-tool]
3+
template: asciidoc-embedded
4+
components: [elastic-agent-changelog-tool]

docs/usage.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ From the repository root folder build the consolidated changelog with:
3939
$ elastic-agent-changelog-tool build --version=next --owner <owner> --repo <repo>
4040
```
4141

42-
the render the consolidated changelog with:
43-
42+
then render the consolidated changelog with:
4443
```
45-
$ elastic-agent-changelog-tool render --version=next --owner <owner> --repo <repo>
44+
$ elastic-agent-changelog-tool render --version=next --template <template>
4645
```
4746

47+
The template value can be chosen from a predefined internal list of templates (`render --help`) or use a full path to your template file.
48+
4849
An example is [`../changelog/0.1.0.yaml`](../changelog/0.1.0.yaml).
4950

5051
### My PR does not need a changelog
@@ -73,11 +74,11 @@ From the repository root folder build the consolidated changelog with:
7374
$ elastic-agent-changelog-tool build --version=next --owner <owner> --repo <repo>
7475
```
7576

76-
the render the consolidated changelog with:
77-
77+
then render the consolidated changelog with:
7878
```
79-
$ $ elastic-agent-changelog-tool render --version=next --owner <owner> --repo <repo>
79+
$ $ elastic-agent-changelog-tool render --version=next --template <template>
8080
```
81+
The template value can be chosen from a predefined internal list of templates (`render --help`) or use a full path to your template file.
8182

8283
An example is [`../changelog/0.1.0.yaml`](../changelog/0.1.0.yaml).
8384

@@ -88,7 +89,7 @@ The side effect is that the changelog will include all entries from latest stabl
8889

8990
1. Create consolidated changelog with `$ elastic-agent-changelog-tool build --version <version> --owner <owner> --repo <repo>`;
9091
* This will create `./changelog/x.y.z.yaml`;
91-
2. Create rendered changelog with `$ elastic-agent-changelog-tool render --version <version>`;
92+
2. Create rendered changelog with `$ elastic-agent-changelog-tool render --version <version> --template <template>`;
9293
* This will generate an asciidoc file in the `changelog/` directory;
9394
3. Use the rendered changelog.
9495

@@ -119,7 +120,7 @@ $ elastic-agent-changelog-tool cleanup
119120
* Commit the previous changes (consolidated changelod and removed files)
120121
* From the root folder of the repository run:
121122
```
122-
$ elastic-agent-changelog-tool render --version x.y.z
123+
$ elastic-agent-changelog-tool render --version x.y.z --template <template>
123124
```
124125
* This will generate an asciidoc fragment in the `changelog/` directory.
125126
* Integrate the generated fragment into the changelog. If the changelog is stored in the same repository, commit the changes in this same branch.
File renamed without changes.

internal/assets/assets.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
package assets
6+
7+
import (
8+
"embed"
9+
"fmt"
10+
"strings"
11+
)
12+
13+
// Binds strings to the actual template file name
14+
// These strings can be used in the config template field or renderer template flag
15+
func GetEmbeddedTemplates() embeddedTemplates {
16+
return map[string]string{
17+
"asciidoc-embedded": "asciidoc-template.asciidoc",
18+
}
19+
}
20+
21+
//go:embed asciidoc-template.asciidoc
22+
var AsciidocTemplate embed.FS
23+
24+
type embeddedTemplates map[string]string
25+
26+
func (t embeddedTemplates) String() string {
27+
var sb strings.Builder
28+
29+
for k := range t {
30+
sb.WriteString(fmt.Sprintf("- %s\n", k))
31+
}
32+
33+
return sb.String()
34+
}

internal/changelog/renderer.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package changelog
66

77
import (
88
"bytes"
9-
"embed"
109
"fmt"
1110
"html/template"
1211
"log"
1312
"path"
1413
"strings"
1514

15+
"github.com/elastic/elastic-agent-changelog-tool/internal/assets"
1616
"github.com/spf13/afero"
1717
"github.com/spf13/viper"
1818
"golang.org/x/text/cases"
@@ -23,14 +23,16 @@ type Renderer struct {
2323
changelog Changelog
2424
fs afero.Fs
2525
// dest is the destination location where the changelog is written to
26-
dest string
26+
dest string
27+
templ string
2728
}
2829

29-
func NewRenderer(fs afero.Fs, c Changelog, dest string) *Renderer {
30+
func NewRenderer(fs afero.Fs, c Changelog, dest string, templ string) *Renderer {
3031
return &Renderer{
3132
changelog: c,
3233
fs: fs,
3334
dest: dest,
35+
templ: templ,
3436
}
3537
}
3638

@@ -132,13 +134,22 @@ func (r Renderer) Render() error {
132134
return afero.WriteFile(r.fs, outFile, data.Bytes(), changelogFilePerm)
133135
}
134136

135-
//go:embed asciidoc-template.asciidoc
136-
var asciidocTemplate embed.FS
137-
138137
func (r Renderer) Template() ([]byte, error) {
139-
data, err := asciidocTemplate.ReadFile("asciidoc-template.asciidoc")
138+
var data []byte
139+
var err error
140+
141+
if embeddedFileName, ok := assets.GetEmbeddedTemplates()[r.templ]; ok {
142+
data, err = assets.AsciidocTemplate.ReadFile(embeddedFileName)
143+
if err != nil {
144+
return []byte{}, fmt.Errorf("cannot read embedded template: %s %w", embeddedFileName, err)
145+
}
146+
147+
return data, nil
148+
}
149+
150+
data, err = afero.ReadFile(r.fs, r.templ)
140151
if err != nil {
141-
return []byte{}, fmt.Errorf("cannot read embedded template: %w", err)
152+
return []byte{}, fmt.Errorf("cannot read custom template: %w", err)
142153
}
143154

144155
return data, nil

internal/changelog/renderer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestRenderer(t *testing.T) {
3434
c, err := changelog.FromFile(fs, inFile)
3535
require.NoError(t, err)
3636

37-
r := changelog.NewRenderer(fs, c, dest)
37+
r := changelog.NewRenderer(fs, c, dest, "asciidoc-embedded")
3838

3939
err = r.Render()
4040
require.Nil(t, err)

internal/settings/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func setDefaults() {
6060

6161
viper.SetDefault("changelog_destination", "changelog")
6262
viper.SetDefault("rendered_changelog_destination", "changelog")
63+
64+
viper.SetDefault("template", "asciidoc-embedded")
6365
}
6466

6567
func setConstants() {

0 commit comments

Comments
 (0)