Files
Dominik Schulz 7281ca8ab4 [chore] Migrate to golangci-lint v2 (#3104)
* [chore] Migrate to golangci-lint v2

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [chore] Fix more lint issues

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [chore] Fix more lint issue

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [chore] Fix more lint issues

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [chore] Add more package comments.

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [chore] Fix golangci-lint config and the remaining checks

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [fix] Use Go 1.24

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [fix] Fix container builds

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Fix more failing tests

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Fix test failure

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Fix another len assertion

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Move location tests

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [fix] Fix most remaining lint issues

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [fix] Only run XDG specific tests on linux

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* [fix] Attempt to address on source of flaky failures

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

---------

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
2025-04-17 08:05:43 +02:00

53 lines
1.1 KiB
Go

// Package tpl provides functions to handle templates.
// It can parse templates from various formats and generate output for them.
package tpl
import (
"bytes"
"context"
"fmt"
"path/filepath"
"text/template"
"github.com/gopasspw/gopass/pkg/gopass"
)
type kvstore interface {
Get(context.Context, string) (gopass.Secret, error)
}
type payload struct {
Dir string
DirName string
Path string
Name string
Content string
}
// Execute executes the given template.
func Execute(ctx context.Context, tpl, name string, content []byte, s kvstore) ([]byte, error) {
funcs := funcMap(ctx, s)
dir := filepath.Dir(name)
pl := payload{
Dir: dir,
DirName: filepath.Base(dir),
Path: name,
Name: filepath.Base(name),
Content: string(content),
}
tmpl, err := template.New(tpl).Funcs(funcs).Parse(tpl)
if err != nil {
return []byte{}, fmt.Errorf("failed to parse template: %w", err)
}
buff := &bytes.Buffer{}
if err := tmpl.Execute(buff, pl); err != nil {
return []byte{}, fmt.Errorf("failed to execute template: %w", err)
}
return buff.Bytes(), nil
}