github.com/devtron-labs/ci-runner@v0.0.0-20240518055909-b2672f3349d7/executor/util/TemplatePrint.go (about)

     1  /*
     2   *  Copyright 2020 Devtron Labs
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package util
    19  
    20  import (
    21  	"bytes"
    22  	"text/template"
    23  )
    24  
    25  // Tprintf passed template string is formatted usign its operands and returns the resulting string.
    26  // Spaces are added between operands when neither is a string.
    27  func Tprintf(tmpl string, data interface{}) (string, error) {
    28  	t := template.Must(template.New("tpl").Parse(tmpl))
    29  	buf := &bytes.Buffer{}
    30  	if err := t.Execute(buf, data); err != nil {
    31  		return "", err
    32  	}
    33  	return buf.String(), nil
    34  }