Beispiel #1
0
func NewTheme(tpath string) *Theme {
	var err error
	var pattern string
	var pages []string
	var base *template.Template
	var tmap = map[string]*template.Template{}

	pattern = path.Join(tpath, "*.html")
	base = template.Must(template.ParseGlob(pattern))
	pattern = path.Join(tpath, "pages", "*.html")
	if pages, err = filepath.Glob(pattern); err != nil {
		panic(err)
	}

	for _, tpath := range pages {
		var ts *template.Template

		if ts, err = base.Clone(); err != nil {
			panic(err)
		}
		if _, err = ts.ParseFiles(tpath); err != nil {
			panic(err)
		}
		tmap[path.Base(tpath)] = ts
	}

	return &Theme{
		Path:      tpath,
		Templates: tmap,
	}
}
Beispiel #2
0
func (g *Generator) parseTemplates(pathGlob string, baseTpl *template.Template) (fileNames []string, templates *template.Template, err error) {
	fileNames, err = filepath.Glob(pathGlob)
	if err != nil {
		return nil, nil, errors.Annotate(err, "when reading files in "+pathGlob)
	}
	templates, err = template.Must(baseTpl.Clone()).ParseFiles(fileNames...)
	if err != nil {
		return nil, nil, errors.Annotate(err, "when parsing service templates files in "+pathGlob)
	}
	return
}
Beispiel #3
0
// runTemplateCommand executes the given template with the given data,
// which generates a command to execute. If exitNonZeroOK is true, no
// error is returned if the exit code is not 0, otherwise an error is
// returned.
func runTemplateCommand(t *template.Template, exitNonZeroOK bool, data interface{}) (
	exitCode int, err error,
) {
	// Clone the template to ensure the original won't be changed.
	cloned, err := t.Clone()
	if err != nil {
		return -1, errors.Annotatef(err, "cannot clone command template %q", t.Name())
	}
	var buf bytes.Buffer
	if err := cloned.Execute(&buf, data); err != nil {
		return -1, errors.Annotatef(err, "cannot execute command template %q", t.Name())
	}
	command := buf.String()
	logger.Debugf("running command %q", command)
	result, err := exec.RunCommands(exec.RunParams{Commands: command})
	if err != nil {
		return -1, errors.Annotatef(err, "cannot run command %q", command)
	}
	exitCode = result.Code
	stdout := string(result.Stdout)
	stderr := string(result.Stderr)
	logger.Debugf(
		"command %q returned code=%d, stdout=%q, stderr=%q",
		command, exitCode, stdout, stderr,
	)
	if exitCode != 0 {
		if exitNonZeroOK {
			return exitCode, nil
		}
		return exitCode, errors.Errorf(
			"command %q failed with exit code %d",
			command, exitCode,
		)
	}
	return 0, nil
}
Beispiel #4
0
// Template uses the provided template definitions.
func (r *Router) Template(t *template.Template) {
	r.Lock()
	defer r.Unlock()
	r.views = template.Must(t.Clone())
}