0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-25 19:40:39 +00:00
wp-plugin-go/template/template.go

93 lines
2.3 KiB
Go

// Copyright (c) 2018, Drone.IO Inc
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package template
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"text/template"
)
// Render parses and executes a template, returning the results in string
// format. Trailing or leading spaces or new-lines are not getting truncated. It
// is able to read templates from remote paths, local files or directly from the
// string.
func Render(ctx context.Context, client http.Client, templateString string, payload interface{}) (string, error) {
var outString bytes.Buffer
tpl := new(template.Template).Funcs(LoadFuncMap())
templateURL, err := url.Parse(templateString)
if err == nil {
switch templateURL.Scheme {
case "http", "https":
req, err := http.NewRequestWithContext(ctx, http.MethodGet, templateURL.String(), nil)
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
res, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to fetch: %w", err)
}
defer res.Body.Close()
out, err := io.ReadAll(res.Body)
if err != nil {
return "", fmt.Errorf("failed to read: %w", err)
}
templateString = string(out)
case "file":
out, err := os.ReadFile(templateURL.Path)
if err != nil {
return "", fmt.Errorf("failed to read: %w", err)
}
templateString = string(out)
}
}
tpl, err = tpl.Parse(templateString)
if err != nil {
return "", err
}
err = tpl.Execute(&outString, payload)
return outString.String(), err
}
// RenderTrim parses and executes a template, returning the results in string
// format. The result is trimmed to remove left and right padding and newlines
// that may be added unintentially in the template markup.
func RenderTrim(ctx context.Context, client http.Client, template string, playload interface{}) (string, error) {
out, err := Render(ctx, client, template, playload)
return strings.Trim(out, " \n"), err
}
// AddPrefix is a helper function to ensure a string has a defined prefix.
func AddPrefix(prefix, input string) string {
if strings.TrimSpace(input) == "" {
return input
}
if strings.HasPrefix(input, prefix) {
return input
}
return prefix + input
}