Exemple #1
0
func (s *Service) loadUnitTemplate(filename string) (*template.Templating, error) {
	path := s.path + filename
	source, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, errors.Annotate(err, "Cannot read unit template file")
	}
	template, err := template.NewTemplating(s.GetEnv().Partials, path, string(source))
	if err != nil {
		return nil, errs.WithEF(err, s.fields, "Failed to load unit template")
	}
	return template, nil
}
Exemple #2
0
func ProcessManifestTemplate(manifestContent string, data2 interface{}, checkNoValue bool) (*AciManifest, error) {
	manifest := AciManifest{Aci: AciDefinition{}}
	fields := data.WithField("source", manifestContent)

	template, err := template.NewTemplating(nil, "", manifestContent)
	if err != nil {
		return nil, errs.WithEF(err, fields, "Failed to load templating of manifest")
	}

	var b bytes.Buffer
	writer := bufio.NewWriter(&b)
	if err := template.Execute(writer, data2); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to template manifest")
	}
	if err := writer.Flush(); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to flush buffer")
	}

	templated := b.Bytes()
	if logs.IsDebugEnabled() {
		logs.WithField("content", string(templated)).Debug("Templated manifest")
	}

	if checkNoValue {
		scanner := bufio.NewScanner(bytes.NewReader(templated))
		scanner.Split(bufio.ScanLines)
		for i := 1; scanner.Scan(); i++ {
			text := scanner.Text()
			if bytes.Contains([]byte(text), []byte("<no value>")) {
				return nil, errs.WithF(fields.WithField("line", i).WithField("text", text), "Templating result of manifest have <no value>")
			}
		}
	}

	err = yaml.Unmarshal(templated, &manifest)
	if err != nil {
		return nil, errs.WithEF(err, fields, "Cannot unmarshall manifest")
	}

	return &manifest, nil
}
func (r *RouterTemplate) Init(s *Synapse) error {
	if err := r.commonInit(r, s); err != nil {
		return errs.WithEF(err, r.fields, "Failed to init common router")
	}

	if r.DestinationFile == "" {
		return errs.WithF(r.fields, "DestinationFile is mandatory")
	}
	r.fields = r.fields.WithField("file", r.DestinationFile)
	if r.DestinationFileMode == 0 {
		r.DestinationFileMode = 0644
	}
	if r.Template == "" && r.TemplateFile == "" {
		return errs.WithF(r.fields, "Template or TemplateFile are mandatory")
	}
	if r.Template != "" && r.TemplateFile != "" {
		return errs.WithF(r.fields, "use Template or TemplateFile")
	}
	if r.PostTemplateCommandTimeoutInMilli == 0 {
		r.PostTemplateCommandTimeoutInMilli = 2000
	}

	if r.TemplateFile != "" {
		content, err := ioutil.ReadFile(r.TemplateFile)
		if err != nil {
			return errs.WithEF(err, r.fields.WithField("template", r.TemplateFile), "Failed to read template file")
		}
		r.Template = string(content)
	}

	tmpl, err := template.NewTemplating(nil, r.DestinationFile, r.Template)
	if err != nil {
		return err
	}
	r.tmpl = tmpl
	return nil
}