Przeglądaj źródła

Merge pull request #729 from smallstep/funcmap

Use the common method for template functions
Mariano Cano 5 lat temu
rodzic
commit
d68090ebce
2 zmienionych plików z 5 dodań i 9 usunięć
  1. 2 6
      pki/helm.go
  2. 3 3
      templates/templates.go

+ 2 - 6
pki/helm.go

@@ -4,11 +4,11 @@ import (
 	"io"
 	"io"
 	"text/template"
 	"text/template"
 
 
-	"github.com/Masterminds/sprig/v3"
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 	"github.com/smallstep/certificates/authority"
 	"github.com/smallstep/certificates/authority"
 	authconfig "github.com/smallstep/certificates/authority/config"
 	authconfig "github.com/smallstep/certificates/authority/config"
 	"github.com/smallstep/certificates/authority/provisioner"
 	"github.com/smallstep/certificates/authority/provisioner"
+	"github.com/smallstep/certificates/templates"
 	"go.step.sm/linkedca"
 	"go.step.sm/linkedca"
 )
 )
 
 
@@ -24,11 +24,7 @@ type helmVariables struct {
 // WriteHelmTemplate a helm template to configure the
 // WriteHelmTemplate a helm template to configure the
 // smallstep/step-certificates helm chart.
 // smallstep/step-certificates helm chart.
 func (p *PKI) WriteHelmTemplate(w io.Writer) error {
 func (p *PKI) WriteHelmTemplate(w io.Writer) error {
-	funcs := sprig.TxtFuncMap()
-	delete(funcs, "env")
-	delete(funcs, "expandenv")
-
-	tmpl, err := template.New("helm").Funcs(funcs).Parse(helmTemplate)
+	tmpl, err := template.New("helm").Funcs(templates.StepFuncMap()).Parse(helmTemplate)
 	if err != nil {
 	if err != nil {
 		return errors.Wrap(err, "error writing helm template")
 		return errors.Wrap(err, "error writing helm template")
 	}
 	}

+ 3 - 3
templates/templates.go

@@ -183,7 +183,7 @@ func (t *Template) Load() error {
 // the template fails.
 // the template fails.
 func (t *Template) LoadBytes(b []byte) error {
 func (t *Template) LoadBytes(b []byte) error {
 	t.backfill(b)
 	t.backfill(b)
-	tmpl, err := template.New(t.Name).Funcs(getFuncMap()).Parse(string(b))
+	tmpl, err := template.New(t.Name).Funcs(StepFuncMap()).Parse(string(b))
 	if err != nil {
 	if err != nil {
 		return errors.Wrapf(err, "error parsing template %s", t.Name)
 		return errors.Wrapf(err, "error parsing template %s", t.Name)
 	}
 	}
@@ -271,9 +271,9 @@ func mkdir(path string, perm os.FileMode) error {
 	return nil
 	return nil
 }
 }
 
 
-// getFuncMap returns sprig.TxtFuncMap but removing the "env" and "expandenv"
+// StepFuncMap returns sprig.TxtFuncMap but removing the "env" and "expandenv"
 // functions to avoid any leak of information.
 // functions to avoid any leak of information.
-func getFuncMap() template.FuncMap {
+func StepFuncMap() template.FuncMap {
 	m := sprig.TxtFuncMap()
 	m := sprig.TxtFuncMap()
 	delete(m, "env")
 	delete(m, "env")
 	delete(m, "expandenv")
 	delete(m, "expandenv")