github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/errors/resolver/template.go (about)

     1  // Copyright 2021 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package resolver
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"strings"
    21  	"text/template"
    22  )
    23  
    24  var baseTemplate = func() *template.Template {
    25  	tmpl := template.New("base")
    26  	tmpl = template.Must(tmpl.Parse(detailsHelperTemplate))
    27  	tmpl = template.Must(tmpl.Parse(nestedErrTemplate))
    28  	return tmpl
    29  }()
    30  
    31  var (
    32  	// detailsHelperTemplate is a helper subtemplate that is available to
    33  	// the top-level templates. It is useful when including information from
    34  	// execing other commands in the error message.
    35  	detailsHelperTemplate = `
    36  {{- define "ExecOutputDetails" }}
    37  {{- if or (gt (len .stdout) 0) (gt (len .stderr) 0)}}
    38  {{ printf "\nDetails:" }}
    39  {{- end }}
    40  
    41  {{- if gt (len .stdout) 0 }}
    42  {{ printf "%s" .stdout }}
    43  {{- end }}
    44  
    45  {{- if gt (len .stderr) 0 }}
    46  {{ printf "%s" .stderr }}
    47  {{- end }}
    48  {{ end }}
    49  `
    50  
    51  	// nestedErrTemplate is a helper subtemplate for printing details from
    52  	// a nested error.
    53  	nestedErrTemplate = `
    54  {{- define "NestedErrDetails" }}
    55  {{- if .err  }}
    56  {{- if .err.Err }}
    57  {{- if gt (len .err.Err.Error) 0 }}
    58  {{ printf "\nDetails:" }}
    59  {{ printf "%s" .err.Err.Error }}
    60  {{- end }}
    61  {{- end }}
    62  {{- end }}
    63  {{ end }}
    64  `
    65  )
    66  
    67  // ExecuteTemplate takes the provided template string and data, and renders
    68  // the template. If something goes wrong, it panics.
    69  func ExecuteTemplate(text string, data interface{}) string {
    70  	tmpl := template.Must(baseTemplate.Clone())
    71  	template.Must(tmpl.Parse(text))
    72  
    73  	var b bytes.Buffer
    74  	execErr := tmpl.Execute(&b, data)
    75  	if execErr != nil {
    76  		panic(fmt.Errorf("error executing template: %w", execErr))
    77  	}
    78  	return strings.TrimSpace(b.String())
    79  }