format

package
v0.0.1-dev.12 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Reset      = "\033[0m"
	Bold       = "\033[1m"
	Red        = "\033[31m"
	Green      = "\033[32m"
	Yellow     = "\033[33m"
	Blue       = "\033[34m"
	DarkBlue   = "\033[34;1m"
	Magenta    = "\033[35m"
	Cyan       = "\033[36m"
	White      = "\033[37m"
	BoldRed    = "\033[1;31m"
	BoldGreen  = "\033[1;32m"
	BoldYellow = "\033[1;33m"
	BoldBlue   = "\033[1;34m"
	BoldCyan   = "\033[1;36m"
)

Color codes

View Source
const (
	ErrorType   = "error"
	WarningType = "warning"
	InfoType    = "info"
)

Error types

Variables

View Source
var (
	ErrorColor     = color.New(color.FgRed, color.Bold)
	WarningColor   = color.New(color.FgYellow, color.Bold)
	SuccessColor   = color.New(color.FgGreen, color.Bold)
	FileColor      = color.New(color.FgCyan)
	LineColor      = color.New(color.FgHiGreen)
	CodeColor      = color.New(color.FgWhite)
	ContextColor   = color.New(color.FgHiBlack)
	HintColor      = color.New(color.FgYellow, color.Italic)
	HeadingColor   = color.New(color.FgHiWhite, color.Bold)
	HighlightColor = color.New(color.FgHiRed)
)

Error colors

View Source
var ErrorCategoryMapping = map[string]string{
	"invalid YAML":               "YAML_SYNTAX",
	"field validation failed":    "FIELD_VALIDATION",
	"missing required field":     "MISSING_FIELD",
	"invalid value":              "INVALID_VALUE",
	"unknown field":              "UNKNOWN_FIELD",
	"duplicate field":            "DUPLICATE_FIELD",
	"unexpected format":          "FORMAT_ERROR",
	"check indentation":          "INDENTATION_ERROR",
	"reference error":            "REFERENCE_ERROR",
	"unrecognized resource type": "UNKNOWN_RESOURCE",
	"must be between":            "RANGE_ERROR",
	"cannot be negative":         "NEGATIVE_VALUE",
	"is required":                "MISSING_REQUIRED",
	"must specify":               "MISSING_SPECIFICATION",
	"must have":                  "MISSING_REQUIREMENT",
}

ErrorCategoryMapping maps error patterns to categories

Functions

func Colorize

func Colorize(color, text string) string

Colorize adds color to a string if colors are enabled

func Dim

func Dim(format string, a ...interface{}) string

Dim formats a message as dimmed (white)

func EnableColor

func EnableColor(enable bool)

EnableColor enables or disables colored output globally

func Error

func Error(format string, a ...interface{}) string

Error formats a message as an error (red)

func FormatTable

func FormatTable(table [][]string, headerRow bool) [][]string

FormatTable colorizes a table of strings based on a header row

func Header(format string, a ...interface{}) string

Header formats a message as a header (bold blue)

func Highlight

func Highlight(format string, a ...interface{}) string

Highlight formats a message as highlighted (bold cyan)

func Info

func Info(format string, a ...interface{}) string

Info formats a message as info (cyan)

func IsColorEnabled

func IsColorEnabled() bool

IsColorEnabled returns whether colored output is enabled

func Label

func Label(key, value string) string

Label formats a key and value with a label style

func PTermStatusLabel

func PTermStatusLabel(status string) string

PTermStatusLabel applies consistent coloring to status strings based on their value

func PrintLintSummary

func PrintLintSummary(fileCount, errorCount, fixCount int, duration time.Duration)

PrintLintSummary prints a summary of the linting process

func PrintSuccess

func PrintSuccess(message string)

PrintSuccess prints a success message

func StatusLabel

func StatusLabel(status string) string

StatusLabel formats a status label based on the status value

func StatusSymbol

func StatusSymbol(success bool) string

StatusSymbol returns a colorized status symbol

func Success

func Success(format string, a ...interface{}) string

Success formats a message as a success (green)

func Warning

func Warning(format string, a ...interface{}) string

Warning formats a message as a warning (yellow)

Types

type ErrorFormatter

type ErrorFormatter struct {
	FileName      string
	FileData      []byte
	ContextLines  int
	CanAutoFix    bool
	OutputFormat  string
	Errors        []ValidationError
	StartTime     time.Time
	TerminalWidth int
	ErrorCount    int
	WarningCount  int
	FixCount      int
}

ErrorFormatter is used to format and print error messages

func NewErrorFormatter

func NewErrorFormatter(filename string, data []byte) *ErrorFormatter

NewErrorFormatter creates a new error formatter

func (*ErrorFormatter) AddError

func (f *ErrorFormatter) AddError(err ValidationError)

AddError adds a new error to the formatter

func (*ErrorFormatter) ExtractFieldName

func (f *ErrorFormatter) ExtractFieldName(errStr string) string

ExtractFieldName tries to extract a field name from an error message

func (*ErrorFormatter) ExtractLineNumber

func (f *ErrorFormatter) ExtractLineNumber(errStr string) int

ExtractLineNumber tries to extract a line number from an error message

func (*ErrorFormatter) FormatAsJSON

func (f *ErrorFormatter) FormatAsJSON() string

FormatAsJSON formats all errors as a JSON string

func (*ErrorFormatter) GenerateHint

func (f *ErrorFormatter) GenerateHint(errStr, errType string) string

GenerateHint creates a hint based on the error message

func (*ErrorFormatter) PrintError

func (f *ErrorFormatter) PrintError(errStr string, lineNum int)

PrintError prints an error with context

func (*ErrorFormatter) PrintErrorHeader

func (f *ErrorFormatter) PrintErrorHeader()

PrintErrorHeader prints a header for errors

func (*ErrorFormatter) PrintErrorSummary

func (f *ErrorFormatter) PrintErrorSummary()

PrintErrorSummary prints a summary of all errors categorized by type

func (*ErrorFormatter) PrintServiceError

func (f *ErrorFormatter) PrintServiceError(serviceName, errStr string, lineNum int)

PrintServiceError prints an error specific to a service

func (*ErrorFormatter) TryAutoFix

func (f *ErrorFormatter) TryAutoFix(errStr string, lineNum int) (bool, []byte)

TryAutoFix attempts to fix common errors

type ValidationError

type ValidationError struct {
	FileName     string `json:"file_name"`
	LineNumber   int    `json:"line_number"`
	Message      string `json:"message"`
	ErrorType    string `json:"error_type"`
	Hint         string `json:"hint,omitempty"`
	AutoFixable  bool   `json:"auto_fixable"`
	FixApplied   bool   `json:"fix_applied"`
	ResourceName string `json:"resource_name,omitempty"`
	ResourceType string `json:"resource_type,omitempty"`
	Context      string `json:"context,omitempty"`
}

ValidationError represents a structured validation error

Jump to

Keyboard shortcuts

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