Exemplo n.º 1
0
// ParseIfAddrsTemplate parses input as template input using the IfAddrs inputs,
// then returns the string output if there are no errors.
func ParseIfAddrsTemplate(input string, ifAddrs sockaddr.IfAddrs, tmplIn *template.Template) (string, error) {
	// Create a template, add the function map, and parse the text.
	tmpl, err := tmplIn.Option("missingkey=error").
		Funcs(SourceFuncs).
		Funcs(SortFuncs).
		Funcs(FilterFuncs).
		Funcs(HelperFuncs).
		Parse(input)
	if err != nil {
		return "", errwrap.Wrapf(fmt.Sprintf("unable to parse template %+q: {{err}}", input), err)
	}

	var outWriter bytes.Buffer
	err = tmpl.Execute(&outWriter, ifAddrs)
	if err != nil {
		return "", errwrap.Wrapf(fmt.Sprintf("unable to execute sockaddr input %+q: {{err}}", input), err)
	}

	return outWriter.String(), nil
}
Exemplo n.º 2
0
func (pkg *Package) processTemplateFile(configDirectory string, name string, value string, funcMap GoTemplate.FuncMap) error {
	var tmpl *GoTemplate.Template

	// Template files can be absolute or live locally under the
	// configuration directory in /tpl
	if filepath.IsAbs(value) {
		if _, err := os.Stat(value); err != nil {
			return err
		}
		tmpl = GoTemplate.Must(GoTemplate.New(name).Funcs(funcMap).ParseFiles(value))
	} else {
		value = configDirectory + "/tpl/" + value
		if _, err := os.Stat(value); err != nil {
			return err
		}
		tmpl = GoTemplate.Must(GoTemplate.New(name).Funcs(funcMap).ParseFiles(value))
	}

	// See above
	tmpl.Option("missingkey=error")
	pkg.ProcessedTemplates[name] = tmpl
	return nil
}