template

package
v0.0.0-...-9f34937 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2023 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFuncs = map[string]any{
	"toUpper": strings.ToUpper,
	"toLower": strings.ToLower,
	"title": func(text string) string {

		return cases.Title(language.AmericanEnglish).String(text)
	},
	"trimSpace": strings.TrimSpace,

	"join": func(sep string, s []string) string {
		return strings.Join(s, sep)
	},
	"match": regexp.MatchString,
	"safeHtml": func(text string) tmplhtml.HTML {
		return tmplhtml.HTML(text)
	},
	"reReplaceAll": func(pattern, repl, text string) string {
		re := regexp.MustCompile(pattern)
		return re.ReplaceAllString(text, repl)
	},
	"stringSlice": func(s ...string) []string {
		return s
	},
	"markdown": markdownEscapeString,
	"toJSON":   toJson,
}

Functions

func CopyFile

func CopyFile(dstName, srcName string) (written int64, err error)

Types

type Alert

type Alert struct {
	Status       string    `json:"status"`
	Labels       KV        `json:"labels"`
	Annotations  KV        `json:"annotations"`
	StartsAt     time.Time `json:"startsAt"`
	EndsAt       time.Time `json:"endsAt"`
	GeneratorURL string    `json:"generatorURL"`
	Fingerprint  string    `json:"fingerprint"`
}

Alert holds one alert for notification templates.

type Alerts

type Alerts []Alert

Alerts is a list of Alert objects.

func (Alerts) Firing

func (as Alerts) Firing() (res []Alert)

Firing returns the subset of alerts that are firing.

func (Alerts) Resolved

func (as Alerts) Resolved() (res []Alert)

Resolved returns the subset of alerts that are resolved.

type Config

type Config struct {
	// path for custom template
	BaseDir string
	// path for temporary template
	TmpDir string
	// path for active template dir
	DataDir string
	// path for attachment
	AttachmentDir string
}

Config stores configuration for template.

type Data

type Data struct {
	Receiver string `json:"receiver"`
	Status   string `json:"status"`
	Alerts   Alerts `json:"alerts"`

	GroupLabels       KV `json:"groupLabels"`
	CommonLabels      KV `json:"commonLabels"`
	CommonAnnotations KV `json:"commonAnnotations"`

	ExternalURL string `json:"externalURL"`
}

Data is the data passed to notification templates and webhook pushes.

End-users should not be exposed to Go's type system, as this will confuse them and prevent simple things like simple equality checks to fail. Map everything to float64/string.

func (Data) LabelKV

func (d Data) LabelKV(alt alert.Alert) KV

type KV

type KV map[string]string

func (KV) Names

func (kv KV) Names() []string

Names returns the names of the label names in the LabelSet.

func (KV) Remove

func (kv KV) Remove(keys []string) KV

Remove returns a copy of the key/value set without the given keys.

func (KV) SortedPairs

func (kv KV) SortedPairs() Pairs

SortedPairs returns a sorted list of key/value pairs.

func (KV) String

func (kv KV) String() string

func (KV) Values

func (kv KV) Values() []string

Values returns a list of the values in the LabelSet.

type Option

type Option func(text *tmpltext.Template, html *tmplhtml.Template)

Option is generic modifier of the text and html templates used by a Template.

type Pair

type Pair struct {
	Name, Value string
}

Pair is a key/value string pair.

type Pairs

type Pairs []Pair

Pairs is a list of key/value string pairs.

func (Pairs) Names

func (ps Pairs) Names() []string

Names returns a list of names of the pairs.

func (Pairs) String

func (ps Pairs) String() string

func (Pairs) Values

func (ps Pairs) Values() []string

Values returns a list of values of the pairs.

type Template

type Template struct {
	Config

	ExternalURL *url.URL
	// contains filtered or unexported fields
}

Template bundles a text and a html template instance.

func MustParse

func MustParse(t *Template, err error) *Template

MustParse is a helper that wraps a call to a function returning (*Template, error) and panics if the error is non-nil.

func New

func New(options ...Option) (*Template, error)

New returns a new Template with the DefaultFuncs added. The DefaultFuncs have precedence over any added custom functions. Options allow customization of the text and html templates in given order.

func (*Template) AddParseTree

func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error)

AddParseTree adds the given parse tree to the template.

func (*Template) Data

func (t *Template) Data(recv string, groupLabels label.LabelSet, alerts ...*alert.Alert) *Data

Data assembles data for template expansion.

func (*Template) EnableTplDataFile

func (t *Template) EnableTplDataFile(tplPath string) error

EnableTplDataFile 启用模板文件 tplPath 为temp文件路径

func (*Template) ExecuteHTMLString

func (t *Template) ExecuteHTMLString(html string, data interface{}) (string, error)

ExecuteHTMLString needs a meaningful doc comment (TODO(fabxc)).

func (*Template) ExecuteTextString

func (t *Template) ExecuteTextString(text string, data interface{}) (string, error)

ExecuteTextString needs a meaningful doc comment (TODO(fabxc)).

func (*Template) GetTplDataPath

func (t *Template) GetTplDataPath(tempPath string) string

GetTplDataPath 将tpl临时文件路径转为正式存储路径

func (*Template) GetTplTempPath

func (t *Template) GetTplTempPath(tempPath string) string

GetTplTempPath 获取tpl正式文件路径

func (*Template) Parse

func (t *Template) Parse(text string) (*Template, error)

Parse parses text as a template body for t.

func (*Template) ParseDir

func (t *Template) ParseDir(path string) (*Template, error)

ParseDir walks on the given dir path and parses the given matches with aren't Go files.

func (*Template) ParseFS

func (t *Template) ParseFS(fsys fs.FS, patterns ...string) (*Template, error)

ParseFS is like ParseFiles or ParseGlob but reads from the file system fsys instead of the host operating system's file system.

func (*Template) ParseFiles

func (t *Template) ParseFiles(filenames ...string) (*Template, error)

ParseFiles parses a list of files as templates and associate them with t. Each file can be a standalone template.

func (*Template) ParseGlob

func (t *Template) ParseGlob(pattern string) (*Template, error)

ParseGlob parses the files that match the given pattern as templates and associate them with t.

func (*Template) RemoveTplDataFile

func (t *Template) RemoveTplDataFile(tplPath string) error

RemoveTplDataFile 移除data目录模板 tplPath 为temp文件路径

func (*Template) ValidateFilePath

func (t *Template) ValidateFilePath(ctx context.Context, path string, kind TplPathKind) error

ValidateFilePath Verify compliance with file service conventions. path rule: {PathKind}/{tenantId}/{xxx}/{filename}

type TplPathKind

type TplPathKind int
const (
	TplPathKindData TplPathKind = iota
	TplPathKindTmp
	TplPathKindAttachment
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL