yamlfmt

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: Apache-2.0 Imports: 1 Imported by: 7

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

Installation

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

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

Usage

By default, the tool will recursively find all files that match the glob path **/*.{yaml,yml} extension and attempt to format them with the basic formatter. To run the tool with all default settings, simply run the command with no arguments:

yamlfmt

You can also run the command with paths to each individual file, or with glob paths:

yamlfmt x.yaml y.yaml config/**/*.yaml

(NOTE: Glob paths are implemented using the doublestar package, which is far more flexible than Go's glob implementation. See the doublestar docs for more details.)

Configuration

The tool can be configured with a .yamlfmt configuration file in the working directory. The configuration is specified in yaml.

Include/Exclude

If you would like to have a consistent configuration for include and exclude paths, you can also use a configuration file. The tool will attempt to read a configuration file named .yamlfmt in the directory the tool is run on. In it, you can configure paths to include and exclude, for example:

include:
  - config/**/*.{yaml,yml}
exclude:
  - excluded/**/*.yaml
Formatter

In your .yamlfmt file you can also specify configuration for the formatter if that formatter supports it. To change the indentation level of the basic formatter for example:

formatter:
  type: basic
  indentation: 4

If the type is not specified, the default formatter will be used. In the tool included in this repo, the default is the basic formatter.

Flags

The CLI supports 3 operation modes:

  • Format (default, no flags)
    • Format and write the matched files
  • Dry run (-dry flag)
    • Format the matched files and output the diff to stdout
  • Lint (-lint flag)
    • Format the matched files and output the diff to stdout, exits with status 1 if there are any differences

(NOTE: If providing paths as command line arguments, the flags must be specified before any paths)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

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

type Formatter

type Formatter interface {
	Type() string
	Format(yamlContent []byte) ([]byte, 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)

Directories

Path Synopsis
cmd
yamlfmt command
formatters

Jump to

Keyboard shortcuts

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