func()

in mmv1/provider/template_data.go [203:244]


func (td *TemplateData) GenerateFile(filePath, templatePath string, input any, goFormat bool, templates ...string) {
	templateFileName := filepath.Base(templatePath)

	funcMap := template.FuncMap{
		"TemplatePath": func() string { return templatePath },
	}
	for k, v := range google.TemplateFunctions {
		funcMap[k] = v
	}

	tmpl, err := template.New(templateFileName).Funcs(funcMap).ParseFiles(templates...)
	if err != nil {
		glog.Exit(fmt.Sprintf("error parsing %s for filepath %s ", templateFileName, filePath), err)
	}

	contents := bytes.Buffer{}
	if err = tmpl.ExecuteTemplate(&contents, templateFileName, input); err != nil {
		glog.Exit(fmt.Sprintf("error executing %s for filepath %s ", templateFileName, filePath), err)
	}

	sourceByte := contents.Bytes()
	if len(sourceByte) == 0 {
		return
	}

	if goFormat {
		formattedByte, err := format.Source(sourceByte)
		if err != nil {
			glog.Error(fmt.Errorf("error formatting %s: %s", filePath, err))
		} else {
			sourceByte = formattedByte
		}
		if !strings.Contains(templatePath, "third_party/terraform") {
			goimportFiles.Store(filePath, struct{}{})
		}
	}

	err = os.WriteFile(filePath, sourceByte, 0644)
	if err != nil {
		glog.Exit(err)
	}
}