Esempio n. 1
0
// Resolve template variables to a string value and cache the value
func (e *ExecEnv) Resolve(tmpl string) (string, error) {
	if val, ok := e.tmplCache[tmpl]; ok {
		return val, nil
	}

	template, err := fasttmpl.NewTemplate(tmpl, startTag, endTag)
	if err != nil {
		return "", err
	}

	buff := &bytes.Buffer{}
	_, err = template.ExecuteFunc(buff, e.templateContext)
	if err == nil {
		e.tmplCache[tmpl] = buff.String()
	}
	return buff.String(), err
}
Esempio n. 2
0
func FqdnFromTemplate(template, serviceName, stackName, environmentName, rootDomainName string) string {
	t, err := fasttemplate.NewTemplate(template, "%{{", "}}")
	if err != nil {
		logrus.Fatalf("error while parsing fqdn template: %s", err)
	}

	fqdn := t.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
		switch tag {
		case "service_name":
			return w.Write([]byte(sanitizeLabel(serviceName)))
		case "stack_name":
			return w.Write([]byte(sanitizeLabel(stackName)))
		case "environment_name":
			return w.Write([]byte(sanitizeLabel(environmentName)))
		default:
			return 0, fmt.Errorf("invalid placeholder '%q' in fqdn template", tag)
		}
	})

	labels := []string{fqdn, rootDomainName}
	return strings.ToLower(strings.Join(labels, "."))
}
Esempio n. 3
0
func _template(s string, context *gabs.Container, modify bool) (string, error) {
	t, err := fasttemplate.NewTemplate(s, "{{", "}}")
	if err != nil {
		return "", err
	}

	w := bytesBufferPool.Get().(*bytes.Buffer)

	if _, err := t.ExecuteFunc(w, func(w io.Writer, tag string) (int, error) {
		tag = strings.TrimSpace(tag)
		if value := context.Path(tag).Data(); value != nil {
			if valueArr, ok := value.([]interface{}); ok && len(valueArr) > 0 {
				value = valueArr[0]
			}

			return w.Write([]byte(fmt.Sprintf("%v", value)))
		} else if modify && strings.Contains(tag, "|") {
			if v, err := ModifyExec(tag, context); err == nil {
				return w.Write([]byte(fmt.Sprintf("%v", v)))
			}
		}

		if strings.HasPrefix(tag, "vars.") || context.ExistsP(tag) {
			return 0, nil
		}

		return 0, fmt.Errorf("Undefined template variable: '%s'", tag)
	}); err != nil {
		return "", err
	}

	out := string(w.Bytes())
	w.Reset()
	bytesBufferPool.Put(w)

	return out, nil
}