fstore

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 2 Imported by: 7

README

fstore

fstore have bunch of go-template functions.

import "github.com/rytsh/mugo/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/fstore"
	_ "github.com/rytsh/mugo/fstore/registry"
)

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/fstore"
	_ "github.com/rytsh/mugo/fstore/registry"
)

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/fstore"
	_ "github.com/rytsh/mugo/fstore/registry"
)

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)
}
Example (Sprig)
package main

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

	"github.com/rytsh/mugo/fstore"
	_ "github.com/rytsh/mugo/fstore/registry"
)

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

View Source
var (
	ErrInvalidType   = fmt.Errorf("invalid type")
	ErrTrustRequired = fmt.Errorf("trust required")
)

Functions

func AddFunc added in v0.9.0

func AddFunc(name string, fn any)

func AddFuncWithOptions added in v0.9.0

func AddFuncWithOptions(fn func(o Option) (string, any))

func AddGroup added in v0.9.0

func AddGroup(name string, fn func() map[string]any)

func AddGroupWithOptions added in v0.9.0

func AddGroupWithOptions(fn func(o Option) (string, func() map[string]any))

func AddStruct added in v0.9.0

func AddStruct(name string, s any)

func AddStructWithOptions added in v0.9.0

func AddStructWithOptions(fn func(o Option) (string, any))

func FuncMap

func FuncMap(opts ...OptionFunc) map[string]any

func GetRegistry added in v0.9.0

func GetRegistry() registry

Types

type Adapter added in v0.9.0

type Adapter interface {
	Error(msg string, keysAndValues ...any)
	Info(msg string, keysAndValues ...any)
	Debug(msg string, keysAndValues ...any)
	Warn(msg string, keysAndValues ...any)
}

type ExecuteTemplate

type ExecuteTemplate interface {
	ExecuteTemplate(wr io.Writer, name string, data any) error
}

type Noop added in v0.9.0

type Noop struct{}

func (Noop) Debug added in v0.9.0

func (Noop) Debug(_ string, _ ...any)

func (Noop) Error added in v0.9.0

func (Noop) Error(_ string, _ ...any)

func (Noop) Info added in v0.9.0

func (Noop) Info(_ string, _ ...any)

func (Noop) Warn added in v0.9.0

func (Noop) Warn(_ string, _ ...any)

type Option

type Option struct {
	Template ExecuteTemplate
	WorkDir  string
	Trust    bool
	Log      Adapter
}

type OptionFunc added in v0.9.0

type OptionFunc func(option *option)

func WithDisableFuncs

func WithDisableFuncs(disableFuncs ...string) OptionFunc

WithDisableFuncs is a option for disableFuncs.

WithDisableFuncs("exec", "execTemplate")

func WithDisableGroups

func WithDisableGroups(disableGroups ...string) OptionFunc

WithDisableGroups is a option for disable direct groups.

WithDisableGroups("sprig")

func WithExecuteTemplate

func WithExecuteTemplate(t ExecuteTemplate) OptionFunc

func WithLog

func WithLog(log Adapter) OptionFunc

func WithSpecificFuncs

func WithSpecificFuncs(specificFuncs ...string) OptionFunc

WithSpecificFuncs is a option for just enable specific functions.

func WithSpecificGroups

func WithSpecificGroups(specificGroups ...string) OptionFunc

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

WithSpecificGroups("sprig")

func WithTrust

func WithTrust(trust bool) OptionFunc

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

func WithWorkDir

func WithWorkDir(workDir string) OptionFunc

WithWorkDir is a option for workDir.

Directories

Path Synopsis
log
os

Jump to

Keyboard shortcuts

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