functions

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package functions provides funcutions to stencil templates.

Description: Holds all context for a rendered template file

Description: Implements the stencil function passed to templates

Index

Constants

This section is empty.

Variables

View Source
var Default = template.FuncMap{
	"Dereference": func(i interface{}) interface{} {
		infType := reflect.TypeOf(i)

		if infType.Kind() != reflect.Ptr {
			return i
		}

		return reflect.ValueOf(i).Elem().Interface()
	},
	"QuoteJoinStrings": func(elems []string, sep string) string {
		for i := range elems {
			elems[i] = fmt.Sprintf("%q", elems[i])
		}
		return strings.Join(elems, sep)
	},
}

Default are stock template functions that don't impact the generation of a file. Anything that does that should be located in the scope of the file renderer function instead

Functions

This section is empty.

Types

type RenderedTemplate

type RenderedTemplate struct {
	io.Reader

	// Skipped marks if this file should be skipped or not
	Skipped bool

	// SkipReason is the reason this file was skipped
	// only valid when Skip is true and is optional
	SkipReason string

	// Deleted marks if this file has been deleted or not
	Deleted bool

	// Path is the path this template should be written to
	// Note: This is a relative path, b.Dir is injected
	// at the writeFile step as the base.
	Path string

	// Warnings is an array of warnings that were created
	// while rendering this template
	Warnings []string
}

RenderedTemplate is a file that has been processed by stencil

func (*RenderedTemplate) AddDeprecationNotice

func (rt *RenderedTemplate) AddDeprecationNotice(msg string) string

AddDeprecationNotice adds a warning to the rendered template file that will be returned to the renderer. When ran via stencil this will be outputted to the console.

 {{- if thingWeShouldNotSupport }}
	{{- file.AddDeprecationNotice "No longer supported"}}
 {{- end }}

func (*RenderedTemplate) Delete

func (rt *RenderedTemplate) Delete() string

Delete marks this rendered template as being deleted and will be removed by the renderer if it exists

{{- file.Delete }}

func (*RenderedTemplate) MarkStatic

func (rt *RenderedTemplate) MarkStatic() string

MarkStatic marks this rendered template as being static. This file will only be written once, and future changes to this template will not be reflected.

{{- file.MarkStatic }}

func (*RenderedTemplate) SetPath

func (rt *RenderedTemplate) SetPath(name string) string

SetPath sets the path of this template which changes where the renderer will write this file in the context of the running directory

{{- $appName := (stencil.Arg "name") }}
{{- file.SetPath (printf "cmd/%s/%s.go" $appName $appName)}}

func (*RenderedTemplate) Skip

func (rt *RenderedTemplate) Skip(reason string) string

Skip marks this template as skipped with a user-providable reason.

{{- if someReason }}
{{- file.Skip "LEEEEEEERROOOYYYY JENKIIIINNNSSS" }}
{{- end }}

type Stencil

type Stencil struct {
	*template.Template

	// Files is a list of files that this rendered produced
	Files []*RenderedTemplate

	// File is the current file that is being rendered by this
	// renderer.
	File *RenderedTemplate
	// contains filtered or unexported fields
}

Stencil provides the basic functions for stencil templates

func NewStencil

NewStencil creates a new, fully initialized Stencil

func (*Stencil) ApplyTemplate

func (s *Stencil) ApplyTemplate(name string) (string, error)

ApplyTemplate executes a template inside of the current rendered template.

{{- define "command"}}
package main

import "fmt"

func main() {
  fmt.Println("hello, world!")
}

{{- end }}

{{- stencil.ApplyTemplate "command" | stencil.InstallFile "cmd/main.go" }}

func (*Stencil) Arg

func (s *Stencil) Arg(name string) interface{}

Arg returns an argument from the ServiceManifest. Note: Only the top-level arguments are supported.

{{- stencil.Arg "name" }}

func (*Stencil) Args added in v1.3.0

func (s *Stencil) Args() interface{}

Args returns the arguments from the ServiceManifest.

{{- (stencil.Args).name }}

func (*Stencil) InstallFile

func (s *Stencil) InstallFile(name, contents string) string

InstallFile changes the current active rendered file and writes the provided contents to it. This changes the scope of the current "File" being rendered.

{{- file.Skip "Virtual file that generates X files" }}
{{- define "command"}}
package main

import "fmt"

func main() {
  fmt.Println("hello, world!")
}

{{- end }}

{{- /* Generate X number of files based on arguments.commands list of strings */}}
{{- range (stencil.Arg "commands") }}
{{- stencil.ApplyTemplate "command" | stencil.InstallFile (printf "cmd/%s.go" .) }}
{{- end }}

Jump to

Keyboard shortcuts

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