fstore

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2023 License: MIT Imports: 3 Imported by: 4

README

fstore

fstore have bunch of go-template functions.

go get github.com/rytsh/mugo/pkg/fstore

Usage

NOTE sprig functions added directly (direct group). Other functions added with struct.

Disable or just enable specific functions use options.

tpl := template.New("test").Funcs(fstore.FuncMap())

output := &bytes.Buffer{}
tplParsed, err := tpl.Parse(`{{b64dec "TWVyaGFiYQ=="}}`)
if err != nil {
    log.Fatal(err)
}

if tplParsed.Execute(output, nil); err != nil {
    log.Fatal(err)
}

fmt.Printf("%s", output)
// Output:
// Merhaba

Documentation

Overview

Example
package main

import (
	"bytes"
	"fmt"
	"log"
	"text/template"

	"github.com/rytsh/mugo/pkg/fstore"
)

func main() {
	tpl := template.New("test").Funcs(fstore.FuncMap())

	output := &bytes.Buffer{}
	tplParsed, err := tpl.Parse(`{{ $v := codec.JsonDecode (codec.StringToByte .) }}{{ $v.data.name }}`)
	if err != nil {
		log.Fatal(err)
	}

	if err := tplParsed.Execute(output, `{"data": {"name": "Hatay"}}`); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", output)
}
Output:

Hatay
Example (ExecTemplate)
package main

import (
	"bytes"
	"fmt"
	"log"
	"text/template"

	"github.com/rytsh/mugo/pkg/fstore"
)

func main() {
	tpl := template.New("test")
	tpl.Funcs(fstore.FuncMap(
		fstore.WithSpecificFuncs("execTemplate"),
		fstore.WithExecuteTemplate(tpl),
	))

	output := &bytes.Buffer{}
	tplParsed, err := tpl.Parse(`{{ define "ochtend" }}Dag!{{ end }}{{ execTemplate "ochtend" nil | printf }}`)
	if err != nil {
		log.Fatal(err)
	}

	if err := tplParsed.Execute(output, nil); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", output)
}
Output:

Dag!
Example (Simple)
package main

import (
	"bytes"
	"fmt"
	"log"
	"text/template"

	"github.com/rytsh/mugo/pkg/fstore"
)

func main() {
	tpl := template.New("test").Funcs(fstore.FuncMap())

	output := &bytes.Buffer{}
	tplParsed, err := tpl.Parse(`{{nothing "nothing for nothing" true 42}}`)
	if err != nil {
		log.Fatal(err)
	}

	if err := tplParsed.Execute(output, nil); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", output)
}
Output:

Example (Sprig)
package main

import (
	"bytes"
	"fmt"
	"log"
	"text/template"

	"github.com/rytsh/mugo/pkg/fstore"
)

func main() {
	tpl := template.New("test").Funcs(fstore.FuncMap(
		fstore.WithSpecificGroups("sprig"),
	))

	output := &bytes.Buffer{}
	tplParsed, err := tpl.Parse(`{{b64dec "TWVyaGFiYQ=="}}`)
	if err != nil {
		log.Fatal(err)
	}

	if err := tplParsed.Execute(output, nil); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", output)
}
Output:

Merhaba

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuncMap

func FuncMap(opts ...Option) map[string]interface{}

func FuncMapTpl

func FuncMapTpl(opts ...Option) func(t registry.ExecuteTemplate) map[string]interface{}

Types

type Option

type Option func(options *options)

func WithDisableFuncs

func WithDisableFuncs(disableFuncs ...string) Option

WithDisableFuncs is a option for disableFuncs.

WithDisableFuncs("exec", "execTemplate")

func WithDisableGroups

func WithDisableGroups(disableGroups ...string) Option

WithDisableGroups is a option for disable direct groups.

WithDisableGroups("sprig")

func WithExecuteTemplate

func WithExecuteTemplate(t registry.ExecuteTemplate) Option

func WithLog

func WithLog(log logz.Adapter) Option

func WithSpecificFuncs

func WithSpecificFuncs(specificFuncs ...string) Option

WithSpecificFuncs is a option for just enable specific functions.

func WithSpecificGroups

func WithSpecificGroups(specificGroups ...string) Option

WithSpecificGroups is a option for just enable specific direct add groups.

WithSpecificGroups("sprig")

func WithTrust

func WithTrust(trust bool) Option

WithTrust is a option for trust. Some functions are not safe to use such as "exec".

func WithWorkDir

func WithWorkDir(workDir string) Option

WithWorkDir is a option for workDir.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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