app

package
v0.0.0-...-b2003df Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package app implements the core templr CLI commands and application logic.

Index

Constants

View Source
const (
	ExitOK            = 0
	ExitGeneral       = 1
	ExitTemplateError = 2
	ExitDataError     = 3
	ExitStrictError   = 4
	ExitGuardSkipped  = 5
	ExitLintWarn      = 6 // lint found warnings (with --fail-on-warn)
	ExitLintError     = 7 // lint found errors
	ExitSchemaError   = 8 // schema validation failed
)

Exit codes for CI-friendly behavior.

View Source
const (
	// DefaultSchemaFile is the default schema filename to look for
	DefaultSchemaFile = ".templr.schema.yml"
)

Variables

View Source
var Version string

Version is set at build time via -ldflags

Functions

func ApplyConfigToLintOptions

func ApplyConfigToLintOptions(opts *LintOptions, config *Config)

ApplyConfigToLintOptions applies config values to LintOptions

func ApplyConfigToSharedOptions

func ApplyConfigToSharedOptions(opts *SharedOptions, config *Config)

ApplyConfigToSharedOptions applies config values to SharedOptions CLI flags take precedence over config file values

func Contains

func Contains(s, substr string) bool

Contains checks if a string contains a substring

func FindSchemaFile

func FindSchemaFile(configSchemaPath string) string

FindSchemaFile looks for schema file in order of precedence

func FormatSchemaErrors

func FormatSchemaErrors(result *SchemaValidationResult, mode string) string

FormatSchemaErrors formats schema errors for display

func GenerateSchema

func GenerateSchema(data map[string]interface{}, config SchemaGenerateConfig) (map[string]interface{}, error)

GenerateSchema generates a JSON Schema from data

func GetVersion

func GetVersion() string

GetVersion returns a human-friendly version string.

func RunDirMode

func RunDirMode(opts DirOptions) error

RunDirMode executes directory mode: parse all templates in dir, execute one entry

func RunLegacyMode

func RunLegacyMode()

RunLegacyMode implements backward compatibility for the old flag-based CLI. This function parses flags in the old style and routes to the appropriate command.

func RunLintMode

func RunLintMode(opts LintOptions) error

RunLintMode executes lint mode

func RunRenderMode

func RunRenderMode(opts RenderOptions) error

RunRenderMode executes single-file render mode

func RunSchemaGenerate

func RunSchemaGenerate(opts SchemaOptions, config *Config) error

RunSchemaGenerate generates a schema from data

func RunSchemaValidate

func RunSchemaValidate(opts SchemaOptions, config *Config) error

RunSchemaValidate validates data against a schema

func RunWalkMode

func RunWalkMode(opts WalkOptions) error

RunWalkMode executes walk mode: recursively render all templates in src to dst

Types

type Config

type Config struct {
	Files    FilesConfig    `yaml:"files"`
	Template TemplateConfig `yaml:"template"`
	Schema   SchemaConfig   `yaml:"schema"`
	Lint     LintConfig     `yaml:"lint"`
	Render   RenderConfig   `yaml:"render"`
	Output   OutputConfig   `yaml:"output"`
}

Config represents the complete configuration structure

func LoadConfig

func LoadConfig(configPath string) (*Config, error)

LoadConfig loads configuration from files with the following precedence: 1. Specified config file (--config flag) 2. .templr.yaml in current directory 3. ~/.config/templr/config.yaml (user config) 4. Built-in defaults

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig returns a Config with default values

type DirOptions

type DirOptions struct {
	Shared SharedOptions
	Dir    string
	In     string
	Out    string
}

DirOptions contains options specific to directory mode

type FileInfo

type FileInfo struct {
	Name    string
	Size    int64
	Mode    string
	ModTime string
	IsDir   bool
}

FileInfo contains metadata about a file.

type FilesAPI

type FilesAPI struct {
	Root string
}

FilesAPI provides a Helm-like .Files facade anchored at a directory.

func (FilesAPI) AsBase64

func (f FilesAPI) AsBase64(path string) (string, error)

AsBase64 reads a file and returns its contents as a base64-encoded string.

func (FilesAPI) AsDataURL

func (f FilesAPI) AsDataURL(path, mimeType string) (string, error)

AsDataURL reads a file and returns it as a data URL (for embedding in HTML/CSS). If mimeType is empty, it will be auto-detected from the file extension.

func (FilesAPI) AsHex

func (f FilesAPI) AsHex(path string) (string, error)

AsHex reads a file and returns its contents as a hexadecimal string.

func (FilesAPI) AsJSON

func (f FilesAPI) AsJSON(path string) (map[string]any, error)

AsJSON reads a JSON file and returns it as a map.

func (FilesAPI) AsLines

func (f FilesAPI) AsLines(path string) ([]string, error)

AsLines reads a file and returns its lines as a slice (alias for Lines).

func (FilesAPI) AsYAML

func (f FilesAPI) AsYAML(path string) (map[string]any, error)

AsYAML reads a YAML file and returns it as a map.

func (FilesAPI) Exists

func (f FilesAPI) Exists(path string) bool

Exists checks if a file or directory exists at the given path.

func (FilesAPI) Get

func (f FilesAPI) Get(path string) (string, error)

Get reads a file and returns its contents as a string.

func (FilesAPI) GetBytes

func (f FilesAPI) GetBytes(path string) ([]byte, error)

GetBytes reads a file and returns its contents as a byte slice.

func (FilesAPI) Glob

func (f FilesAPI) Glob(pat string) ([]string, error)

Glob returns files matching the given glob pattern relative to the root directory.

func (FilesAPI) GlobDetails

func (f FilesAPI) GlobDetails(pat string) ([]FileInfo, error)

GlobDetails returns file metadata for all files matching the given pattern.

func (FilesAPI) Lines

func (f FilesAPI) Lines(path string) ([]string, error)

Lines reads a file and returns its lines as a slice of strings.

func (FilesAPI) ReadDir

func (f FilesAPI) ReadDir(path string) ([]string, error)

ReadDir returns a list of file and directory names in the given directory.

func (FilesAPI) Stat

func (f FilesAPI) Stat(path string) (FileInfo, error)

Stat returns metadata about a file.

type FilesConfig

type FilesConfig struct {
	Extensions          []string `yaml:"extensions"`
	DefaultTemplatesDir string   `yaml:"default_templates_dir"`
	DefaultOutputDir    string   `yaml:"default_output_dir"`
	DefaultValuesFile   string   `yaml:"default_values_file"`
	Helpers             []string `yaml:"helpers"`
}

FilesConfig contains file-related configuration

type LintConfig

type LintConfig struct {
	FailOnWarn        bool     `yaml:"fail_on_warn"`
	FailOnUndefined   bool     `yaml:"fail_on_undefined"`
	StrictMode        bool     `yaml:"strict_mode"`
	OutputFormat      string   `yaml:"output_format"`
	Exclude           []string `yaml:"exclude"`
	DisallowFunctions []string `yaml:"disallow_functions"`
	RequiredVars      []string `yaml:"required_vars"`
	NoUndefCheck      bool     `yaml:"no_undefined_check"`
}

LintConfig contains linting configuration

type LintIssue

type LintIssue struct {
	Severity string // "error", "warn"
	Category string // "parse", "undefined", "function", "guard"
	File     string // file path
	Line     int    // line number (0 if unknown)
	Column   int    // column number (0 if unknown)
	Message  string // human-readable message
}

LintIssue represents a single linting issue

type LintOptions

type LintOptions struct {
	Shared       SharedOptions
	In           string  // single file to lint
	Dir          string  // directory to lint
	Src          string  // source tree to walk and lint
	FailOnWarn   bool    // exit with error on warnings
	Format       string  // output format: text, json, github-actions
	NoUndefCheck bool    // skip undefined variable checking
	Config       *Config // configuration from file
}

LintOptions contains all configuration for lint mode

type LintResult

type LintResult struct {
	Issues []LintIssue
	Errors int
	Warns  int
}

LintResult contains the results of a lint operation

type OutputConfig

type OutputConfig struct {
	Color   string `yaml:"color"` // auto, always, never
	Verbose bool   `yaml:"verbose"`
	Quiet   bool   `yaml:"quiet"`
}

OutputConfig contains output formatting configuration

type RenderConfig

type RenderConfig struct {
	DryRun         bool   `yaml:"dry_run"`
	InjectGuard    bool   `yaml:"inject_guard"`
	GuardString    string `yaml:"guard_string"`
	PruneEmptyDirs bool   `yaml:"prune_empty_dirs"`
}

RenderConfig contains rendering defaults

type RenderOptions

type RenderOptions struct {
	Shared  SharedOptions
	In      string
	Out     string
	Helpers string
}

RenderOptions contains options specific to single-file render mode

type SchemaConfig

type SchemaConfig struct {
	Path     string               `yaml:"path"`     // Path to schema file (default: .templr.schema.yml)
	Mode     string               `yaml:"mode"`     // error|warn|strict (default: warn)
	Generate SchemaGenerateConfig `yaml:"generate"` // Schema generation settings
}

SchemaConfig contains schema validation configuration

type SchemaError

type SchemaError struct {
	Path       string // JSON path (e.g., ".service.replicas")
	Message    string // Error message
	Value      string // The actual value that failed
	Suggestion string // Helpful suggestion for fixing
}

SchemaError represents a schema validation error

type SchemaGenerateConfig

type SchemaGenerateConfig struct {
	Required        string `yaml:"required"`         // all|none|auto (default: auto)
	AdditionalProps bool   `yaml:"additional_props"` // Allow additionalProperties (default: true)
	InferTypes      bool   `yaml:"infer_types"`      // Infer types from values (default: true)
}

SchemaGenerateConfig contains schema generation settings

type SchemaOptions

type SchemaOptions struct {
	Shared          SharedOptions
	SchemaPath      string
	Mode            string
	Output          string
	Required        string
	AdditionalProps bool
	Format          string
}

SchemaOptions contains options for schema commands

type SchemaValidationResult

type SchemaValidationResult struct {
	Errors   []SchemaError
	Warnings []SchemaError
	Passed   bool
}

SchemaValidationResult contains validation results

func ValidateWithSchema

func ValidateWithSchema(data map[string]interface{}, schemaPath, mode string) (*SchemaValidationResult, error)

ValidateWithSchema validates data against a YAML schema file

type SharedOptions

type SharedOptions struct {
	Data           string
	Files          []string
	Sets           []string
	Strict         bool
	DryRun         bool
	Guard          string
	InjectGuard    bool
	DefaultMissing string
	NoColor        bool
	Debug          bool
	Ldelim         string
	Rdelim         string
	ExtraExts      []string
}

SharedOptions contains flags common to all commands

type TemplateConfig

type TemplateConfig struct {
	LeftDelimiter  string `yaml:"left_delimiter"`
	RightDelimiter string `yaml:"right_delimiter"`
	DefaultMissing string `yaml:"default_missing"`
}

TemplateConfig contains template engine configuration

type WalkOptions

type WalkOptions struct {
	Shared SharedOptions
	Src    string
	Dst    string
}

WalkOptions contains options specific to walk mode

Jump to

Keyboard shortcuts

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