template

package
v0.0.0-...-5c29306 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2022 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// FuncMap contains the functions exposed to templating engine.
	FuncMap = template.FuncMap{

		"hostname": func() string { return os.Getenv("HOSTNAME") },
		"username": func() string {
			t, err := user.Current()
			if err != nil {
				return "Unknown"
			}

			return t.Name
		},
		"toBinary": func(s string) string {
			n, err := strconv.Atoi(s)
			if err != nil {
				return s
			}

			return fmt.Sprintf("%b", n)
		},

		"formatFilesize": func(value interface{}) string {
			var size float64

			v := reflect.ValueOf(value)
			switch v.Kind() {
			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
				size = float64(v.Int())
			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
				size = float64(v.Uint())
			case reflect.Float32, reflect.Float64:
				size = v.Float()
			default:
				return ""
			}

			var KB float64 = 1 << 10
			var MB float64 = 1 << 20
			var GB float64 = 1 << 30
			var TB float64 = 1 << 40
			var PB float64 = 1 << 50

			filesizeFormat := func(filesize float64, suffix string) string {
				return strings.Replace(fmt.Sprintf("%.1f %s", filesize, suffix), ".0", "", -1)
			}

			var result string
			if size < KB {
				result = filesizeFormat(size, "bytes")
			} else if size < MB {
				result = filesizeFormat(size/KB, "KB")
			} else if size < GB {
				result = filesizeFormat(size/MB, "MB")
			} else if size < TB {
				result = filesizeFormat(size/GB, "GB")
			} else if size < PB {
				result = filesizeFormat(size/TB, "TB")
			} else {
				result = filesizeFormat(size/PB, "PB")
			}

			return result
		},

		"toTitle": strings.ToTitle,

		"password": func(length, numDigits, numSymbols int, noUpper, allowRepeat bool) string {
			generator, err := password.NewGenerator(&password.GeneratorInput{Symbols: customSymbols})

			if err != nil {
				return fmt.Sprintf("failed to generate password generator: %s", err)
			}

			res, err := generator.Generate(length, numDigits, numSymbols, noUpper, allowRepeat)
			if err != nil {
				return fmt.Sprintf("failed to generate password: %s", err)
			}

			return res
		},

		"randomBase64": func(length int) string {
			b := make([]byte, length)
			_, err := rand.Read(b)

			if err != nil {
				return fmt.Sprintf("failed to generate randomBase64: %s", err)
			}

			return base64.StdEncoding.EncodeToString(b)
		},
	}

	// Options contain the default options for the template execution.
	Options = []string{

		"missingkey=invalid",
	}
)

Functions

func CurrentTimeInFmt

func CurrentTimeInFmt(fmt string) string

CurrentTimeInFmt returns the current time in the given format. See time.Time.Format for more details on the format string.

Types

type Interface

type Interface interface {
	// Executes the template on the given target directory path.
	Execute(string, string) error

	// If used, the template will execute using default values.
	UseDefaultValues()

	// Returns the metadata of the template.
	Info() Metadata
}

Interface is contains the behavior of boilr templates.

func Get

func Get(path string) (Interface, error)

Get retrieves the template from a path.

type JSONTime

type JSONTime time.Time

JSONTime is time.Time with JSON marshaling and unmarshaling implementations.

func NewTime

func NewTime() JSONTime

NewTime returns a new JSONTime containing the current time.

func (*JSONTime) MarshalJSON

func (t *JSONTime) MarshalJSON() ([]byte, error)

MarshalJSON marshals JSONTime to JSON.

func (JSONTime) String

func (t JSONTime) String() string

String returns the string form of JSONTime.

func (*JSONTime) UnmarshalJSON

func (t *JSONTime) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals JSON to JSONTime.

type Metadata

type Metadata struct {
	Tag        string
	Repository string

	Created JSONTime
}

Metadata contains the information for a template.

func (Metadata) String

func (m Metadata) String() []string

String returns the string slice form of Metadata.

Jump to

Keyboard shortcuts

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