yamlfmt

package module
v0.0.0-...-ec8e592 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

yamlfmt

yamlfmt is an extensible command line tool or library to format yaml files.

Goals

  • Create a command line yaml formatting tool that is easy to distribute (single binary)
  • Make it simple to extend with new custom formatters
  • Enable alternative use as a library, providing a foundation for users to create a tool that meets specific needs

Maintainers

This tool is not yet officially supported by Google. It is currently maintained solely by @braydonk, and unless something changes primarily in spare time.

Blog

I'm going to use these links to GitHub Discussions as a blog of sorts, until I can set up something more proper:

  • yamlfmt's recent slow development #149
  • Issues related to the yaml.v3 library #148

Installation

To download the yamlfmt command, you can download the desired binary from releases or install the module directly:

go install github.com/fancom/yamlfmt/cmd/yamlfmt@latest

This currently requires Go version 1.18 or greater.

NOTE: Recommended setup if this is your first time installing Go would be in this DigitalOcean blog post.

You can also download the binary you want from releases. The binary is self-sufficient with no dependencies, and can simply be put somewhere on your PATH and run with the command yamlfmt.

You can also install the command as a pre-commit hook. See the pre-commit hook docs for instructions.

Basic Usage

See Command Usage for in-depth information and available flags.

To run the tool with all default settings, run the command with a path argument:

yamlfmt x.yaml y.yaml <...>

You can specify as many paths as you want. You can also specify a directory which will be searched recursively for any files with the extension .yaml or .yml.

yamlfmt .

You can also use an alternate mode that will search paths with doublestar globs by supplying the -dstar flag.

yamlfmt -dstar **/*.{yaml,yml}

See the doublestar package for more information on this format.

Configuration File

The yamlfmt command can be configured through a yaml file called .yamlfmt. This file can live in your working directory, a path specified through a CLI flag, or in the standard global config path on your system (see docs for specifics). For in-depth configuration documentation see Config.

Documentation

Index

Constants

View Source
const MetadataIdentifier = "!yamlfmt!"

Variables

View Source
var (
	ErrMalformedMetadata    = errors.New("metadata: malformed string")
	ErrUnrecognizedMetadata = errors.New("metadata: unrecognized type")
)

Functions

func ExcludeWithGitignore

func ExcludeWithGitignore(gitignorePath string, paths []string) ([]string, error)

func IsMetadataType

func IsMetadataType(mdValueStr string) bool

func ReadMetadata

func ReadMetadata(content []byte, path string) (collections.Set[Metadata], collections.Errors)

Types

type BasicContentAnalyzer

type BasicContentAnalyzer struct {
	RegexPatterns []*regexp.Regexp
}

func NewBasicContentAnalyzer

func NewBasicContentAnalyzer(patterns []string) (BasicContentAnalyzer, error)

func (BasicContentAnalyzer) ExcludePathsByContent

func (a BasicContentAnalyzer) ExcludePathsByContent(paths []string) ([]string, []string, error)

type ContentAnalyzer

type ContentAnalyzer interface {
	ExcludePathsByContent(paths []string) ([]string, []string, error)
}

type DoublestarCollector

type DoublestarCollector struct {
	Include []string
	Exclude []string
}

func (*DoublestarCollector) CollectPaths

func (c *DoublestarCollector) CollectPaths() ([]string, error)

type Engine

type Engine interface {
	FormatContent(content []byte) ([]byte, error)
	Format(paths []string) (fmt.Stringer, error)
	Lint(paths []string) (fmt.Stringer, error)
	DryRun(paths []string) (fmt.Stringer, error)
}

type Factory

type Factory interface {
	Type() string
	NewFormatter(config map[string]interface{}) (Formatter, error)
}

type Feature

type Feature struct {
	Name         string
	BeforeAction FeatureFunc
	AfterAction  FeatureFunc
}

type FeatureApplyError

type FeatureApplyError struct {
	// contains filtered or unexported fields
}

func (*FeatureApplyError) Error

func (e *FeatureApplyError) Error() string

func (*FeatureApplyError) Unwrap

func (e *FeatureApplyError) Unwrap() error

type FeatureApplyMode

type FeatureApplyMode string
var (
	FeatureApplyBefore FeatureApplyMode = "Before"
	FeatureApplyAfter  FeatureApplyMode = "After"
)

type FeatureFunc

type FeatureFunc func([]byte) ([]byte, error)

type FeatureList

type FeatureList []Feature

func (FeatureList) ApplyFeatures

func (fl FeatureList) ApplyFeatures(input []byte, mode FeatureApplyMode) ([]byte, error)

type FileDiff

type FileDiff struct {
	Path string
	Diff *FormatDiff
}

func (*FileDiff) Apply

func (fd *FileDiff) Apply() error

func (*FileDiff) StrOutput

func (fd *FileDiff) StrOutput() string

func (*FileDiff) StrOutputQuiet

func (fd *FileDiff) StrOutputQuiet() string

type FileDiffs

type FileDiffs map[string]*FileDiff

func (FileDiffs) Add

func (fds FileDiffs) Add(diff *FileDiff) error

func (FileDiffs) ApplyAll

func (fds FileDiffs) ApplyAll() error

func (FileDiffs) ChangedCount

func (fds FileDiffs) ChangedCount() int

func (FileDiffs) StrOutput

func (fds FileDiffs) StrOutput() string

func (FileDiffs) StrOutputQuiet

func (fds FileDiffs) StrOutputQuiet() string

type FilepathCollector

type FilepathCollector struct {
	Include    []string
	Exclude    []string
	Extensions []string
}

func (*FilepathCollector) CollectPaths

func (c *FilepathCollector) CollectPaths() ([]string, error)

type FormatDiff

type FormatDiff struct {
	Original  string
	Formatted string
	LineSep   string
}

func (*FormatDiff) Changed

func (d *FormatDiff) Changed() bool

func (*FormatDiff) MultilineDiff

func (d *FormatDiff) MultilineDiff() (string, int)

type Formatter

type Formatter interface {
	Type() string
	Format(yamlContent []byte) ([]byte, error)
	ConfigMap() (map[string]any, error)
}

type LineBreakStyle

type LineBreakStyle string
const (
	LineBreakStyleLF   LineBreakStyle = "lf"
	LineBreakStyleCRLF LineBreakStyle = "crlf"
)

func (LineBreakStyle) Separator

func (s LineBreakStyle) Separator() (string, error)

type Metadata

type Metadata struct {
	Type    MetadataType
	LineNum int
}

type MetadataError

type MetadataError struct {
	// contains filtered or unexported fields
}

func (*MetadataError) Error

func (e *MetadataError) Error() string

func (*MetadataError) Unwrap

func (e *MetadataError) Unwrap() error

type MetadataType

type MetadataType string
const (
	MetadataIgnore MetadataType = "ignore"
)

type Operation

type Operation int
const (
	OperationFormat Operation = iota
	OperationLint
	OperationDry
	OperationStdin
	OperationPrintConfig
)

type PathCollector

type PathCollector interface {
	CollectPaths() ([]string, error)
}

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

func NewFormatterRegistry

func NewFormatterRegistry(defaultFactory Factory) *Registry

func (*Registry) Add

func (r *Registry) Add(f Factory)

func (*Registry) GetDefaultFactory

func (r *Registry) GetDefaultFactory() (Factory, error)

func (*Registry) GetFactory

func (r *Registry) GetFactory(fType string) (Factory, error)

type UnsupportedLineBreakError

type UnsupportedLineBreakError struct {
	// contains filtered or unexported fields
}

func (UnsupportedLineBreakError) Error

Directories

Path Synopsis
cmd
yamlfmt command
formatters
internal

Jump to

Keyboard shortcuts

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