Skip to content

Commit

Permalink
Add usage documentation to README, fixed bugs found while writing
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonk committed Aug 17, 2022
1 parent 7b4f2d5 commit b15b9d8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
# yamlfmt

A command line tool to format yaml files in your project.
`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](formatters/basic). 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](https://github.com/bmatcuk/doublestar) package, which is far more flexible than Go's glob implementation. See the doublestar docs for more details.)

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
```

The CLI supports 3 operation modes:

* Format (default)
- 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)
7 changes: 6 additions & 1 deletion cmd/yamlfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"errors"
"flag"
"log"
"os"
Expand Down Expand Up @@ -55,7 +56,11 @@ func run() error {

configData, err := readDefaultConfigFile()
if err != nil {
return err
if errors.Is(err, os.ErrNotExist) {
configData = map[string]interface{}{}
} else {
return err
}
}

if len(flag.Args()) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func RunCommand(
return err
}
if len(config.Include) == 0 {
config.Include = []string{"**/*.yaml", "**/*.yml"}
config.Include = []string{"**/*.{yaml,yml}"}
}

var formatter yamlfmt.Formatter
Expand Down

0 comments on commit b15b9d8

Please sign in to comment.