func TrimTemplate()

in mmv1/google/template_utils.go [80:129]


func TrimTemplate(templatePath string, e any) string {
	templates := []string{
		fmt.Sprintf("templates/terraform/%s", templatePath),
		"templates/terraform/expand_resource_ref.tmpl",
	}
	templateFileName := filepath.Base(templatePath)

	// Need to remake TemplateFunctions, referencing it directly here
	// causes a declaration loop
	var templateFunctions = template.FuncMap{
		"title":         SpaceSeparatedTitle,
		"replace":       strings.Replace,
		"replaceAll":    strings.ReplaceAll,
		"camelize":      Camelize,
		"underscore":    Underscore,
		"plural":        Plural,
		"contains":      strings.Contains,
		"join":          strings.Join,
		"lower":         strings.ToLower,
		"upper":         strings.ToUpper,
		"dict":          wrapMultipleParams,
		"format2regex":  Format2Regex,
		"hasPrefix":     strings.HasPrefix,
		"sub":           subtract,
		"plus":          plus,
		"firstSentence": FirstSentence,
		"trimTemplate":  TrimTemplate,
	}

	tmpl, err := template.New(templateFileName).Funcs(templateFunctions).ParseFiles(templates...)
	if err != nil {
		glog.Exit(err)
	}

	contents := bytes.Buffer{}
	if err = tmpl.ExecuteTemplate(&contents, templateFileName, e); err != nil {
		glog.Exit(err)
	}

	rs := contents.String()

	if rs == "" {
		return rs
	}

	for strings.HasSuffix(rs, "\n") {
		rs = strings.TrimSuffix(rs, "\n")
	}
	return fmt.Sprintf("%s\n", rs)
}