Esempio n. 1
0
func (in Include) Process(tree *gabs.Container) error {
	path := ConfigPath
	if customPath, ok := tree.Path("vars.config-path").Data().(string); ok {
		path = customPath
	}

	if tree.ExistsP("include") {
		items, err := tree.Path("include").Children()
		if err != nil {
			return err
		}

		for i, _ := range items {
			inc := items[len(items)-i-1] // loop in reverse order for merge priority

			if file, ok := inc.Search("file").Data().(string); ok {
				if !strings.HasPrefix(file, "/") {
					file = path + "/" + file
				}

				if err := includeFile(file, tree); err != nil {
					return err
				}
			}
		}
	}

	// include all base configs
	if files, err := filepath.Glob(path + "/conf.d/*.yml"); err == nil {
		for _, file := range files {
			if err := includeFile(file, tree); err != nil {
				return err
			}
		}
	}

	return nil
}
Esempio n. 2
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
}