Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fragment summary autocompletion from title #94

Merged
merged 6 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions internal/changelog/fragment/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ var fragmentPerm = os.FileMode(0660)

// Create marshal changelog fragment and persist it to file.
func (c FragmentCreator) Create(slug string) error {
data, err := Template()
if err != nil {
return err
}

if err := c.fs.MkdirAll(c.location, fragmentLocPerm); err != nil {
return fmt.Errorf("cannot create fragment location folder: %v", err)
}

filePath := path.Join(c.location, c.filename(slug))
if err := afero.WriteFile(c.fs, filePath, data, fragmentPerm); err != nil {

template, err := Template(c.fs, filePath, slug)
if err != nil {
return err
}

if err := afero.WriteFile(c.fs, filePath, template, fragmentPerm); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/changelog/fragment/creator_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestCreate(t *testing.T) {
kind: feature

# Change summary; a 80ish characters long description of the change.
summary:
summary: foobar

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
Expand Down
25 changes: 22 additions & 3 deletions internal/changelog/fragment/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,37 @@
package fragment

import (
"bytes"
"embed"
"fmt"
txttempl "text/template"

"github.com/spf13/afero"
)

//go:embed template.yaml
var template embed.FS

func Template() ([]byte, error) {
func Template(fs afero.Fs, filePath, slug string) ([]byte, error) {
data, err := template.ReadFile("template.yaml")
if err != nil {
return []byte{}, fmt.Errorf("cannot read embedded template: %w", err)
return nil, fmt.Errorf("cannot read embedded template: %w", err)
}

t1, err := txttempl.New("template").Parse(string(data))
if err != nil {
return nil, fmt.Errorf("cannot parse template: %w", err)
}

vars := make(map[string]interface{})
vars["Summary"] = slug

buf := bytes.NewBuffer(nil)

err = t1.Execute(buf, vars)
if err != nil {
return nil, fmt.Errorf("cannot execute template: %w", err)
}

return data, nil
return buf.Bytes(), nil
}
2 changes: 1 addition & 1 deletion internal/changelog/fragment/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
kind: feature

# Change summary; a 80ish characters long description of the change.
summary:
summary: {{.Summary}}

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
Expand Down