templates

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NotStrictlyPrivate = errors.New("the private temp file is not strictly accessible by owners only")

NotStrictlyPrivate indicates that a PrivateTempFile was successfully created but the OS reports that it can be accessed by others

Functions

func EnvFileFunc added in v0.27.0

func EnvFileFunc(receiverFunc EnvFileReceiverFunc) map[string]any

EnvFileFunc creates a template func to retrieve a profile .env file that can be used to pass variables between shells

func New

func New(name string, funcs ...map[string]any) (tpl *template.Template)

New returns a new Template instance with configured funcs (including TemplateFuncs)

func PrivateTempFile added in v0.27.0

func PrivateTempFile(name string) (filename string, err error)

PrivateTempFile is like TempFile but guarantees that the returned file can be accessed by owners only when err is nil

func TempDir

func TempDir() string

TempDir returns the volatile temporary directory that is returned by template function tempDir

func TempFile

func TempFile(name string) (filename string)

TempFile returns the volatile temporary file that is returned by template function tempFile

func TemplateFuncs

func TemplateFuncs(funcs ...map[string]any) (templateFuncs map[string]any)

TemplateFuncs declares a few standard functions to simplify working with templates

Available functions:

  • {{ "some string" | contains "some" }} => true
  • {{ "some string" | matches "^.+str.+$" }} => true
  • {{ "some old string" | replace "old" "new" }} => "some new string"
  • {{ "some old string" | replaceR "(old)" "$1 and new" }} => "some old and new string"
  • {{ "some old string" | regex "(old)" "$1 and new" }} => "some old and new string" (alias to "replaceR")
  • {{ "ABC" | lower }} => "abc"
  • {{ "abc" | upper }} => "ABC"
  • {{ " A " | trim }} => "A"
  • {{ "--A-" | trimPrefix " " }} => "A-"
  • {{ "--A-" | trimSuffix " " }} => "--A"
  • {{ "A,B,C" | split "," }} => ["A", "B", "C"]
  • {{ "A,B,C" | split "," | join ";" }} => "A;B;C"
  • {{ "A ,B, C" | splitR "\\s*,\\s*" | join ";" }} => "A;B;C"
  • {{ list "A" "B" "C" }} => ["A", "B", "C"]
  • {{ with $v := map "k1" "v1" "k2" "v2" }} {{ .k1 }}-{{ .k2 }} {{ end }} => " v1-v2 "
  • {{ with $v := list "A" "B" "C" "D" | map }} {{ ._0 }}-{{ ._1 }}-{{ ._3 }} {{ end }} => " A-B-D "
  • {{ with $v := list "A" "B" "C" "D" | map "key" }} {{ .key | join "-" }} {{ end }} => " A-B-C-D "
  • {{ "plain" | hex }} => "706c61696e"
  • {{ "plain" | base64 }} => "cGxhaW4="
  • {{ tempDir }} => "/path/to/unique-tempdir"
  • {{ tempFile "filename" }} => "/path/to/unique-tempdir/filename"

Types

type DefaultData

type DefaultData struct {
	Now        time.Time
	CurrentDir string
	StartupDir string
	TempDir    string
	BinaryDir  string
	Hostname   string
	OS         string
	Arch       string
	Env        map[string]string
}

DefaultData provides default variables for templates

func NewDefaultData

func NewDefaultData(env map[string]string) (data DefaultData)

NewDefaultData returns an initialized DefaultData

func (*DefaultData) InitDefaults

func (d *DefaultData) InitDefaults()

InitDefaults initializes DefaultData if not yet initialized

type EnvFileReceiverFunc added in v0.27.0

type EnvFileReceiverFunc func() (profileKey string, receiveFile func(string))

EnvFileReceiverFunc declares the backend interface for the "{{env}}" template function

Jump to

Keyboard shortcuts

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