github.com/ijc/docker-app@v0.6.1-0.20181012090447-c7ca8bc483fb/internal/renderer/gotemplate/driver.go (about)

     1  // +build experimental
     2  
     3  package gotemplate
     4  
     5  import (
     6  	"bytes"
     7  	"text/template"
     8  
     9  	"github.com/docker/app/internal/renderer"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func init() {
    14  	renderer.Register("gotemplate", &Driver{})
    15  }
    16  
    17  // Driver is the gotemplate implementation of rendered drivers.
    18  type Driver struct{}
    19  
    20  // Apply applies the settings to the string
    21  func (d *Driver) Apply(s string, settings map[string]interface{}) (string, error) {
    22  	tmpl, err := template.New("compose").Parse(s)
    23  	if err != nil {
    24  		return "", err
    25  	}
    26  	tmpl.Option("missingkey=error")
    27  	buf := bytes.NewBuffer(nil)
    28  	if err := tmpl.Execute(buf, settings); err != nil {
    29  		return "", errors.Wrap(err, "failed to execute go template")
    30  	}
    31  	return buf.String(), nil
    32  }