embedx

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2021 License: Apache-2.0 Imports: 4 Imported by: 4

README

embedx

Build Status GoDoc sourcegraph

Just an extension for go:embed

Usage
%> cd demo # change to your golang project root directory; cd <your-project-dir>
%> go get github.com/alimy/embedx
%> tree
 |- public
    |- ...
    |- index.html
    |- ...
 |- conf
    |- app.ini
    |- ...
    |- conf.go
 |- ...
 |- main.go
 |- go.mod
 |- ...
// file: conf/conf.go

package conf

import (
	"embed"
	
	"github.com/alimy/embedx"
)

func NewConfigFS() embedx.EmbedFS {
	//go:embed app.ini
	var content embed.FS

	// attach a root to conf dir then access files in this returned FS will
	// need add  'conf' prefix. eg: access app.ini need FS.ReadFile("conf/app.ini").
	return embedx.AttachRoot(content, "conf")
}
// file: main.go

package main

import (
	"embed"
	
	"github.com/alimy/embedx"
)

func newPublicFS() embedx.EmbedFS {
	//go:embed public
	var content embed.FS
	
	// change the root to public dir then access files in this returned FS will
	// not need  'public' prefix. eg: access public/index.html just need FS.ReadFile("index.html").
	return embedx.ChangeRoot(content, "public")
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Naming added in v0.6.0

func Naming(name string) string

Naming rename give name to a new name use default namer

func ParseFS added in v0.5.0

func ParseFS(fsys fs.FS, patterns ...string) (*template.Template, error)

ParseFS creates a new text/Template and parses the template definitions from the files identified by the pattern. The files are matched according to the semantics of filepath.Match, and the pattern must match at least one file. The returned template will have the (base) name and (parsed) contents of the first file matched by the pattern.

func ParseWith added in v0.5.1

func ParseWith(t *template.Template, fsys fs.FS, patterns ...string) (*template.Template, error)

ParseWith like ParseFS but need provide a *template.Template instance as parameter.

Example
embedFS := ChangeRoot(content, "html/testdata")
funcTmpl := template.New("embedx").Funcs(template.FuncMap{
	"notEmptyStr": notEmptyStr,
})
tmpl, err := ParseWith(funcTmpl, embedFS, "templates/*.tmpl", "templates/b/*.tmpl", "templates/d/*.tmpl")
if err != nil {
	log.Fatal(err)
}

for _, ctx := range []struct {
	Name string
}{
	{"templates/a.tmpl"},
	{"templates/b/c.tmpl"},
	{"templates/d/e.tmpl"},
	{"templates/d/f.tmpl"},
} {
	bs := &bytes.Buffer{}
	if err = tmpl.ExecuteTemplate(bs, ctx.Name, ctx); err != nil {
		log.Fatal(err)
	}
	fmt.Println(bs)
}
Output:

func RegisterNamer added in v0.6.0

func RegisterNamer(n Namer)

RegisterNamer register a new namer replace default. Note: this function is not concurrent safe.

Types

type EmbedFS added in v0.3.0

type EmbedFS interface {
	fs.FS
	ReadDir(name string) ([]fs.DirEntry, error)
	ReadFile(name string) ([]byte, error)
}

EmbedFS embed.FS public method re-defined as interface

func AttachRoot added in v0.2.0

func AttachRoot(fs EmbedFS, root string) EmbedFS

AttachRoot attach to a virtual root directory like mount to a new directory.

func ChangeRoot added in v0.2.0

func ChangeRoot(fs EmbedFS, root string) EmbedFS

ChangeRoot change to a new directory like cd cmd in shell but not check whether this directory is exist.

type Namer added in v0.6.0

type Namer interface {
	Naming(string) string
}

Namer make a new name from an old name.

type NamerFunc added in v0.6.0

type NamerFunc func(string) string

NamerFunc wrap a func as Namer

func (NamerFunc) Naming added in v0.6.0

func (f NamerFunc) Naming(name string) string

Naming rename give name to a new name.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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