template

package
v0.1.5-rc Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Skip   = "skip"
	Cover  = "cover"
	Append = "append"
)

Variables

View Source
var (
	MiddlewareTplName       = "middleware.go"
	MiddlewareSingleTplName = "middleware_single.go"
	ModelTplName            = "model.go"
	HttpClientTplName       = "httpclient.go" // underlying client for client command
	ErrorTplName            = "errors.go"
	IdlClientTplName        = "idl_client.go" // client of service for quick call
	IdlGroupClientTplName   = "idl_group_client.go"
)
View Source
var DefaultDelimiters = [2]string{"{{", "}}"}
View Source
var DefaultLayoutConfig = Config{
	Layouts: []Template{
		{
			Path: defaultModelDir + sp,
		},
		{
			Path:   "go.mod",
			Delims: [2]string{"{{", "}}"},
			Body:   `module {{.GoModule}}`,
		},
		{
			Path: ".gitignore",
			Body: `*.o
*.a
*.so
_obj
_test
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.exe~
*.test
*.prof
*.rar
*.zip
*.gz
*.psd
*.bmd
*.cfg
*.pptx
*.log
*nohup.out
*settings.pyc
*.sublime-project
*.sublime-workspace
!.gitkeep
.DS_Store
/.idea
/.vscode
/output
*.local.yml
dumped_crafter_remote_config.json
service
		  `,
		},
		{
			Path: "build.sh",
			Body: `#!/bin/bash
RUN_NAME={{.ServiceName}}
mkdir -p output/bin
cp script/* output 2>/dev/null
chmod +x output/bootstrap.sh
go build -o output/bin/${RUN_NAME}`,
		},
		{
			Path: defaultScriptDir + sp + "bootstrap.sh",
			Body: `#!/bin/bash
CURDIR=$(cd $(dirname $0); pwd)
BinaryName={{.ServiceName}}
echo "$CURDIR/bin/${BinaryName}"
exec $CURDIR/bin/${BinaryName}`,
		},
		{
			Path: defaultClientDir + sp + ".keep",
			Body: ``,
		},
		{
			Path:   "README.md",
			Delims: [2]string{"{{", "}}"},
			Body:   `##Example`,
		},
	},
}
View Source
var DefaultPkgConfig = Config{
	Layouts: []Template{

		{
			Path: defaultModelDir + sp + ModelTplName,
			Body: ``,
		},

		{
			Path:   defaultClientDir + sp + HttpClientTplName,
			Delims: [2]string{"{{", "}}"},
			Body:   httpClientTpl,
		},
		{
			Path:   defaultClientDir + sp + IdlGroupClientTplName,
			Delims: [2]string{"{{", "}}"},
			Body:   idlGroupClientTpl,
		},
		{
			Path:   defaultClientDir + sp + IdlClientTplName,
			Delims: [2]string{"{{", "}}"},
			Body:   idlClientTpl,
		},
	},
}
View Source
var FuncMap = func() template.FuncMap {
	m := template.FuncMap{
		"ToLowerCamelCase": util.ToLowerCamelCase,
		"TrimSuffix":       util.TrimSuffix,
		"ToSnakeCase":      util.ToSnakeCase,
		"Split":            strings.Split,
		"Trim":             strings.Trim,
		"EqualFold":        strings.EqualFold,
		"ToHttpMethod":     util.ToHttpMethod,
	}
	for key, f := range sprig.TxtFuncMap() {
		m[key] = f
	}
	return m
}()

Functions

func IsDefaultPackageTpl

func IsDefaultPackageTpl(name string) bool

Types

type Config

type Config struct {
	Layouts []Template `yaml:"layouts"`
}

type Template

type Template struct {
	Default        bool           // Is it the default template
	Path           string         `yaml:"path"`            // The generated path and its filename.
	Delims         [2]string      `yaml:"delims"`          // Template Action Instruction Identifier, default: "{{}}"
	Body           string         `yaml:"body"`            // Render template, currently only supports go template syntax
	Disable        bool           `yaml:"disable"`         // Disable generating file, used to disable default package template
	LoopMethod     bool           `yaml:"loop_method"`     // Loop generate files based on "method"
	LoopService    bool           `yaml:"loop_service"`    // Loop generate files based on "service"
	UpdateBehavior UpdateBehavior `yaml:"update_behavior"` // Update command behavior; 0:unchanged, 1:regenerate, 2:append
}

type TemplateGenerator

type TemplateGenerator struct {
	OutputDir    string
	Config       *Config
	IsPackageTpl bool
	Excludes     []string
	Templates    map[string]*TemplateInfo
	Dirs         map[string]bool
	// contains filtered or unexported fields
}

TemplateGenerator contains information about the output template

func (*TemplateGenerator) Degenerate

func (tg *TemplateGenerator) Degenerate() error

func (*TemplateGenerator) Files

func (tg *TemplateGenerator) Files() []util.File

func (*TemplateGenerator) Generate

func (tg *TemplateGenerator) Generate(input interface{}, tplName, filepath string, noRepeat bool) error

func (*TemplateGenerator) GetFormatAndExcludedFiles

func (tg *TemplateGenerator) GetFormatAndExcludedFiles() ([]util.File, error)

func (*TemplateGenerator) Init

func (tg *TemplateGenerator) Init() error

func (*TemplateGenerator) LoadLayout

func (tg *TemplateGenerator) LoadLayout(layout Template, tplName string, isDefaultTpl bool) error

func (*TemplateGenerator) Persist

func (tg *TemplateGenerator) Persist() error

func (*TemplateGenerator) SetFiles

func (tg *TemplateGenerator) SetFiles(files []util.File)

type TemplateInfo

type TemplateInfo struct {
	Template *template.Template
	Info     Template
}

type UpdateBehavior

type UpdateBehavior struct {
	Type string `yaml:"type"` // Update behavior type: skip/cover/append
	// the following variables are used for append update
	AppendKey      string   `yaml:"append_key"`         // Append content based in key; for example: 'method'/'service'
	InsertKey      string   `yaml:"insert_key"`         // Insert content by "insert_key"
	AppendTpl      string   `yaml:"append_content_tpl"` // Append content if UpdateBehavior is "append"
	ImportTpl      []string `yaml:"import_tpl"`         // Import insert template
	AppendLocation string   `yaml:"append_location"`    // AppendLocation specifies the location of append,  the default is the end of the file
}

Jump to

Keyboard shortcuts

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