templit

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 11 Imported by: 1

README

templit

Go Report Card GoDoc GitHub go.mod Go version Build Status GitHub

import "github.com/euforic/templit"

Index

Variables

DefaultFuncMap is the default function map for templates.

var DefaultFuncMap = 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,
}

func ToCamelCase

func ToCamelCase(s string) string

ToCamelCase converts a string to CamelCase.

func ToKebabCase

func ToKebabCase(s string) string

ToKebabCase converts a string to kebab-case.

func ToPascalCase

func ToPascalCase(s string) string

ToPascalCase converts a string to PascalCase.

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase converts a string to snake_case.

type DefaultGitClient

DefaultGitClient provides a default implementation for the GitClient interface.

type DefaultGitClient struct {
    Token   string
    BaseURL string
}

func NewDefaultGitClient
func NewDefaultGitClient(defaultBranch, token string) *DefaultGitClient

NewDefaultGitClient creates a new DefaultGitClient with the optional defaultBranch and token.

func (*DefaultGitClient) Checkout
func (d *DefaultGitClient) Checkout(path, branch string) error

Checkout checks out a branch in a Git repository.

func (*DefaultGitClient) Clone
func (d *DefaultGitClient) Clone(host, owner, repo, dest string) error

Clone clones a Git repository to the given destination.

type DepInfo

DepInfo contains information about an embed URL.

type DepInfo struct {
    Host  string
    Owner string
    Repo  string
    Path  string
    Block string
    Tag   string
}

func ParseDepURL
func ParseDepURL(rawURL string) (*DepInfo, error)

ParseDepURL is a parsed embed URL.

type Executor

Executor is a wrapper around the template.Template type

type Executor struct {
    *template.Template
}

func NewExecutor
func NewExecutor(client GitClient) *Executor

New returns a new Executor

func (*Executor) EmbedFunc
func (e *Executor) EmbedFunc() func(remotePath string, data interface{}) (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:

  1. Add the function to the FuncMap.
  2. 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 (*Executor) ImportFunc
func (e *Executor) ImportFunc() func(repoAndTag, destPath string, data interface{}) (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:

  1. Add the function to the FuncMap.
  2. 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 (*Executor) ParsePath
func (e *Executor) ParsePath(inputPath string) error

ParsePath parses the given path

func (Executor) Render
func (e Executor) Render(name string, data interface{}) (string, error)

Render executes the template with the given data

func (Executor) StringRender
func (e Executor) StringRender(templateString string, data interface{}) (string, error)

StringRender renders the given template string with the given data

func (*Executor) WalkAndProcessDir
func (e *Executor) WalkAndProcessDir(inputDir, outputDir string, data interface{}) error

WalkAndProcessDir 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.

type GitClient

GitClient is an interface that abstracts Git operations.

type GitClient interface {
    Clone(host, owner, repo, dest string) error
    Checkout(path, branch string) error
    DefaultBranch() string
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFuncMap = 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,
}

DefaultFuncMap is the default function map for templates.

Functions

func ToCamelCase

func ToCamelCase(s string) string

ToCamelCase converts a string to CamelCase.

func ToKebabCase

func ToKebabCase(s string) string

ToKebabCase converts a string to kebab-case.

func ToPascalCase

func ToPascalCase(s string) string

ToPascalCase converts a string to PascalCase.

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase converts a string to snake_case.

Types

type DefaultGitClient

type DefaultGitClient struct {
	Token string
	// contains filtered or unexported fields
}

DefaultGitClient provides a default implementation for the GitClient interface.

func NewDefaultGitClient

func NewDefaultGitClient(defaultBranch string, token string) *DefaultGitClient

NewDefaultGitClient creates a new DefaultGitClient with the given token.

func (*DefaultGitClient) Checkout

func (d *DefaultGitClient) Checkout(path, ref string) error

Checkout checks out a branch, tag or commit hash in a Git repository.

func (*DefaultGitClient) Clone

func (d *DefaultGitClient) Clone(host, owner, repo, dest string) error

Clone clones a Git repository to the given destination.

func (*DefaultGitClient) DefaultBranch

func (d *DefaultGitClient) DefaultBranch() string

DefaultBranch returns the default branch name.

type DepInfo

type DepInfo struct {
	Host  string
	Owner string
	Repo  string
	Path  string
	Block string
	Tag   string
}

DepInfo contains information about an embed URL.

func ParseDepURL

func ParseDepURL(rawURL string) (*DepInfo, error)

ParseDepURL is a parsed embed URL.

func (DepInfo) String

func (d DepInfo) String() string

String returns the string representation of a DepInfo.

type Executor

type Executor struct {
	*template.Template
	// contains filtered or unexported fields
}

Executor is a wrapper around the template.Template type

func NewExecutor

func NewExecutor(gitClient GitClient) *Executor

New returns a new Executor

func (*Executor) EmbedFunc

func (e *Executor) EmbedFunc(remotePath string, data interface{}) (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:

  1. Add the function to the FuncMap.
  2. 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 (*Executor) ImportFunc

func (e *Executor) ImportFunc(outputDir string) func(repoAndTag, destPath string, data interface{}) (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:

  1. Add the function to the FuncMap.
  2. 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>#<block>@<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 (*Executor) ParsePath

func (e *Executor) ParsePath(inputPath string) error

ParsePath parses the given path

func (Executor) Render

func (e Executor) Render(name string, data interface{}) (string, error)

Render executes the template with the given data

func (Executor) StringRender

func (e Executor) StringRender(templateString string, data interface{}) (string, error)

StringRender renders the given template string with the given data

func (*Executor) WalkAndProcessDir

func (e *Executor) WalkAndProcessDir(inputDir, outputDir string, data interface{}) error

WalkAndProcessDir 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.

type GitClient

type GitClient interface {
	Clone(host, owner, repo, dest string) error
	Checkout(path, branch string) error
	DefaultBranch() string
}

GitClient is an interface that abstracts Git operations.

Directories

Path Synopsis
cmd
docs module

Jump to

Keyboard shortcuts

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