khandra

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidActionType = errors.New("not a valid ActionType")

Functions

func BubbleAsk

func BubbleAsk(inputs []*Input) (map[string]string, error)

func Copy

func Copy(srcPath, dstPath string, src, dst afero.Fs) error

Copy copies a file from the source path to the destination path without applying any transformations

func Create added in v0.4.2

func Create(appFs afero.Fs, sourceDir, targetDir string, dryrun bool, input map[string]string, askFunc AskFunc) error

func Execute

func Execute(source *Source, target *Target, userData map[string]string) error

func PromptUser

func PromptUser(inputs []*Input, current map[string]string, ask AskFunc) (map[string]string, error)

func Template

func Template(srcPath, dstPath string, src, dst afero.Fs, data any) error

Template creates the destination file by evaluating the source file as template and writing the result to the destination file

Types

type ActionType

type ActionType int

ENUM(COPY, TEMPLATE, IGNORE)

const (
	// COPY is a ActionType of type COPY.
	COPY ActionType = iota
	// TEMPLATE is a ActionType of type TEMPLATE.
	TEMPLATE
	// IGNORE is a ActionType of type IGNORE.
	IGNORE
)

func ParseActionType

func ParseActionType(name string) (ActionType, error)

ParseActionType attempts to convert a string to a ActionType.

func (ActionType) MarshalText

func (x ActionType) MarshalText() ([]byte, error)

MarshalText implements the text marshaller method.

func (ActionType) String

func (x ActionType) String() string

String implements the Stringer interface.

func (*ActionType) UnmarshalText

func (x *ActionType) UnmarshalText(text []byte) error

UnmarshalText implements the text unmarshaller method.

type AskFunc

type AskFunc = func(input []*Input) (map[string]string, error)

type Bones

type Bones struct {
	// AllowExisting allows the template to be applied to a directory which already exists
	AllowExisting bool     `yaml:"allow_existing" json:"allow_existing,omitempty" jsonschema:"default=false"`
	Rules         []Rule   `yaml:"rules" json:"rules"`
	Inputs        []*Input `yaml:"inputs" json:"inputs,omitempty"`
}

Bones describes the template behavior and is read from the bones file in the template directory

func DefaultBones

func DefaultBones() *Bones

func LoadBones

func LoadBones(fs afero.Fs, root string) (*Bones, error)

LoadBones loads the bones file from the given root directory

type Context

type Context struct {
	SrcFs, DstFs afero.Fs
	Data         *Data
}

A Context is the data required for a templated file to be executed and written to disk. In particular, SrcFS is a filesystem rooted at the folder containing all input files (typically the template subdirectory of the Khandra repository), and DstFS is a filesystem rooted at the folder where the template should be initialized.

func NewContext

func NewContext(src *Source, dst *Target, userData map[string]string) (*Context, error)

type Data

type Data struct {
	SourceData
	TargetData
	FileData
	U map[string]string // UserData
}

func MergeData

func MergeData(userData map[string]string, sourceData SourceData, targetData TargetData) *Data

type FileData

type FileData struct {
	File string
}

type Input

type Input struct {
	Name     string `yaml:"name" json:"name"`
	Default  string `yaml:"default,omitempty" json:"default,omitempty"`
	Question string `yaml:"question,omitempty" json:"question,omitempty"`
}

func (*Input) Validate

func (i *Input) Validate() error

type Rule

type Rule struct {
	Pattern  string     `yaml:"pattern" json:"pattern"`                                    // rule is a candidate for application if the path matches this pattern
	Action   ActionType `yaml:"action" json:"action" jsonschema:"type=string"`             // COPY, TEMPLATE, IGNORE
	Priority int        `yaml:"priority" json:"priority,omitempty" jsonschema:"default=0"` // higher priority rules win over lower priority rules
	Rename   string     `yaml:"rename,omitempty" json:"rename,omitempty"`                  // a template to rename the file
}

A Rule describes how to translate a source file into a destination file

func (*Rule) Apply

func (r *Rule) Apply(file string, context *Context) error

Apply a rule to the specified file

At this point we know that the rule matches the file

func (*Rule) Match

func (r *Rule) Match(file string) bool

Match determines if a rule matches a file

type Source

type Source struct {
	Root  string   // Root is the directory the template is located in
	Fs    afero.Fs // Fs is the filesystem the source was read from
	Bones *Bones   // The Bones files which specifies the template
}

A Source represents a Khandra template on disk. Generally anything that can be known only from the source template should be bundled into Source

func NewSource

func NewSource(fs afero.Fs, root string) (*Source, error)

type SourceData

type SourceData struct {
}

type Target

type Target struct {
	Root string   // Root is the directory the template will be initialized in
	Fs   afero.Fs // Fs is the filesystem the target will be written to
}

A Target represents a call to initialize a template Generally anything that depends on the initialization location or the user's input should be bundled into Target

func NewTarget

func NewTarget(root string, fs afero.Fs, allowExisting bool) (*Target, error)

func (*Target) Data

func (t *Target) Data() TargetData

type TargetData

type TargetData struct {
	TargetName string
}

Jump to

Keyboard shortcuts

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