scaffold

package
v1.19.22 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 15 Imported by: 3

Documentation

Overview

Package scaffold provides file and directory generation based on templates.

A template must be UTF8 without byte order marker and have \n (linefeed) as line terminator. It has a head and a body, separated by an empty line:

  1. head (must not contain an empty line)
  2. empty line
  3. body

The head might contain anything but empty lines, but it is recommended to put some annotated json string as example for the usage into it. Also authorship of the template and contact infos can be put there.

The syntax of the body is a superset of the Go text/template package (http://golang.org/pkg/text/template). The available functions inside the body are extended by the functions defined in the FuncMap variable.

Additionally to the functionality provide via the text/template package there are contexts.

Context

The template body can have folder and file contexts. A context is started by a line with the prefix ">>>" and ends by a line with the prefix "<<<". Each context has a name that follows after ">>>" or "<<<" until the end of the line. For example the context ">>>a" is ended by "<<<a" and the name of the context would be "a".

If the name of the context ends with a slash (/), the context is a folder context otherwise it is a file context.

The name of a file context defines the name of the file into which the content of the context will be saved. The folder of the file is defined by the surrounding folder contexts. The name of a folder context defines the name of the folder inside which the inner folders and files (as defined by the inner contexts) are saved.

The outermost folder context is the baseDir parameter of the Run function (defaults to the current working directory in the CLI tool).

The following would create the file "fileZ.txt" inside the folder "[baseDir]/folder1/folderA". Any missing directories a created on the fly.

>>>folder1/
>>>folderA/
>>>fileZ.txt
Hello World
<<<fileZ.txt
<<<folderA/
<<<folder1/

The placeholders inside the body are organized as a json object / map. When the Run function is called, the json objects is mixed to the template and after that the folders and files are created as defined in the result. That makes it possible to use placeholders as parts of folder or file names.

Escaping of double curly braces and dollar chars

Curly braces and dollar chars are part of syntax of the go template engine and there is no syntax to replace them from inside the template. However, the scaffold package has included some helper functions that return them.

{{doubleCurlyOpen}}{{dollar}}{{doubleCurlyClose}}

will result in the string "{{$}}".

Most of the time this package will be used via the scaffold command sub package.

It can be installed via

go get gopkg.in/metakeule/scaffold.v1/lib/scaffold

Run

scaffold help

to see the available options.

For a complete example have a look at the example directory. The make.sh file contains the needed CLI command.

Index

Constants

This section is empty.

Variables

View Source
var FuncMap = template.FuncMap{
	"user":             User,
	"args":             Args,
	"now":              Now,
	"host":             Host,
	"replace":          Replace,
	"filename":         FileName,
	"filenameLower":    FileNameLower,
	"camelCase1":       CamelCase1,
	"camelCase2":       CamelCase2,
	"title":            strings.Title,
	"toLower":          strings.ToLower,
	"toUpper":          strings.ToUpper,
	"trim":             strings.Trim,
	"doubleCurlyOpen":  DoubleCurlyOpen,
	"doubleCurlyClose": DoubleCurlyClose,
	"curlyOpen":        CurlyOpen,
	"curlyClose":       CurlyClose,
	"dollar":           Dollar,
}

FuncMap provides functions to the template. New functions can be added as needed. The usual restrictions for text/template.FuncMap apply (see http://golang.org/pkg/text/template/#FuncMap)

Functions

func Args

func Args() string

func CamelCase1

func CamelCase1(src string) string

CamelCase1 converts a string in snake_case to CamelCase where the first letter of each word is capitalized

func CamelCase2

func CamelCase2(src string) string

CamelCase2 converts a string in snake_case to camelCase where the first letter of each but the first word is capitalized

func CurlyClose

func CurlyClose() string

CurlyClose returns one closed curly brace

func CurlyOpen

func CurlyOpen() string

CurlyOpen returns one open curly brace

func Dollar

func Dollar() string

Dollar returns a dollar char

func DoubleCurlyClose

func DoubleCurlyClose() string

DoubleCurlyClose returns two closed curly braces

func DoubleCurlyOpen

func DoubleCurlyOpen() string

DoubleCurlyOpen returns two open curly braces

func FileName

func FileName(in string) (out string)

FileName converts a string to a safe filename. First leading and trailing spaces are removed Every other space is replaced by - Multiple following - are replaced by on single - Every character other than [-a-zA-Z_0-9] is removed

func FileNameLower

func FileNameLower(src string) string

same as FileName but transforms string to lowercase

func Host

func Host() string

func Now

func Now() string

func Replace

func Replace(s, old, new string) string

Replace replaces every occurence of old in s by new

func Run

func Run(baseDir path.Local, body string, json io.Reader, log io.Writer, verbose bool) (err error)

Run mixes the properties of the json object to the template body. The result is then used to create files and directories beneath baseDir. If log is not nil a list of files that will be created is written to log.

func RunFS

func RunFS(fsys fs.FS, body string, json io.Reader, log io.Writer, verbose bool) (err error)

RunFS is

func Scan

func Scan(fsys fs.ReadOnly, dir path.Relative, opts ...ScanOption) (template []byte, err error)

scans a directory recursively and creates a template based on the structure of the files and directories

func SplitTemplate

func SplitTemplate(template string) (head, body string)

SplitTemplate splits the given template on the first empty line. It returns the head and body of the template. Templates must be UTF8 without byte order marker and have \n (linefeed) as line terminator.

func User

func User() string

Types

type ScanOption

type ScanOption func(*scannerNew)

func SkipDirs

func SkipDirs(regexstring string) ScanOption

Jump to

Keyboard shortcuts

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