import"github.com/euforic/templit"DefaultFuncMap is the default function map for templates.
varDefaultFuncMap= template.FuncMap{
"lower": strings.ToLower,
"upper": strings.ToUpper,
"trim": strings.TrimSpace,
"split": strings.Split,
"join": strings.Join,
"replace": strings.ReplaceAll,
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"trimPrefix": strings.TrimPrefix,
"trimSuffix": strings.TrimSuffix,
"trimSpace": strings.TrimSpace,
"trimLeft": strings.TrimLeft,
"trimRight": strings.TrimRight,
"count": strings.Count,
"repeat": strings.Repeat,
"equalFold": strings.EqualFold,
"splitN": strings.SplitN,
"splitAfter": strings.SplitAfter,
"splitAfterN": strings.SplitAfterN,
"fields": strings.Fields,
"toTitle": strings.ToTitle,
"toSnakeCase": ToSnakeCase,
"toCamelCase": ToCamelCase,
"toKebabCase": ToKebabCase,
"toPascalCase": ToPascalCase,
"default": defaultVal,
}funcToCamelCase(sstring) stringToCamelCase converts a string to CamelCase.
funcToKebabCase(sstring) stringToKebabCase converts a string to kebab-case.
funcToPascalCase(sstring) stringToPascalCase converts a string to PascalCase.
funcToSnakeCase(sstring) stringToSnakeCase converts a string to snake_case.
DefaultGitClient provides a default implementation for the GitClient interface.
typeDefaultGitClientstruct {
TokenstringBaseURLstring
}funcNewDefaultGitClient(defaultBranch, tokenstring) *DefaultGitClientNewDefaultGitClient creates a new DefaultGitClient with the optional defaultBranch and token.
func (d*DefaultGitClient) Checkout(path, branchstring) errorCheckout checks out a branch in a Git repository.
func (d*DefaultGitClient) Clone(host, owner, repo, deststring) errorClone clones a Git repository to the given destination.
DepInfo contains information about an embed URL.
typeDepInfostruct {
HoststringOwnerstringRepostringPathstringBlockstringTagstring
}funcParseDepURL(rawURLstring) (*DepInfo, error)ParseDepURL is a parsed embed URL.
Executor is a wrapper around the template.Template type
typeExecutorstruct {
*template.TemplategitGitClient
}funcNewExecutor(clientGitClient) *ExecutorNew returns a new Executor
func (e*Executor) EmbedFunc() func(remotePathstring, datainterface{}) (string, error)EmbedFunc returns a template function that can be used to process and embed a template from a remote git repository. EmbedFunc allows embedding content from a remote repository directly into a Go template.
Steps to use:
- Add the function to the FuncMap.
- Use the following syntax within your template:
{{ embed "<host>/<owner>/<repo>/<path>@<tag_or_hash_or_branch>" . }} {{ embed "<host>/<owner>/<repo>#<block>@<tag_or_hash_or_branch>" . }}
Placeholders:
<host>: Repository hosting service (e.g., "github.com").<owner>: Repository owner or organization.<repo>: Repository name.<path>: Path to the desired file or directory within the repository.<block>: Specific template block name.<tag_or_hash_or_branch>: Specific Git reference (tag, commit hash, or branch name).
func (e*Executor) ImportFunc(destPathstring) func(repoAndTag, pathstring, datainterface{}) (string, error)ImportFunc returns a function that can be used as a template function to import and process a template from a remote git repository. ImportFunc allows embedding content from a remote repository into a Go template.
Steps to use:
- Add the function to the FuncMap.
- Use the following syntax within your template:
`{{ import "<host>/<owner>/<repo>/<path>@<tag_or_hash_or_branch>" "<path_to_genrate_files>" . }} `{{ import "<host>/<owner>/<repo>/<path>@<tag_or_hash_or_branch>" "<path_to_genrate_files>" . }}
Placeholders:
<host>: Repository hosting service (e.g., "github.com").<owner>: Repository owner or organization.<repo>: Repository name.<path>: Path to the desired file or directory within the repository.<tag_or_hash_or_branch>: Specific Git reference (tag, commit hash, or branch name).
func (e*Executor) ParsePath(inputPathstring) errorParsePath parses the given path
func (eExecutor) Render(namestring, datainterface{}) (string, error)Render executes the template with the given data
func (eExecutor) StringRender(templateStringstring, datainterface{}) (string, error)StringRender renders the given template string with the given data
func (e*Executor) WalkAndProcessDir(inputDir, outputDirstring, datainterface{}) errorWalkAndProcessDir processes all files in a directory with the given data. If walkFunc is provided, it's called for each file and directory without writing the file to disk.
GitClient is an interface that abstracts Git operations.
typeGitClientinterface {
Clone(host, owner, repo, deststring) errorCheckout(path, branchstring) errorDefaultBranch() string
}