Skip to content

Commit

Permalink
Extensions (#222)
Browse files Browse the repository at this point in the history
* test(path_collector): Add (failing) test for multi-part extensions.

* feat(path_collector): Allow `extensions` to contain dots.

I want to format templated YAML files, which have file names like
`config.yaml.gotmpl`. We also have other files, such as `README.md.gotmpl`
which we do *not* want to format with yamlfmt.

Previously, yamlfmt used `strings.Split()` to extract the extension from a
file. This doesn't work when the extension itself contains a dot.

With this change, yamlfmt uses `strings.HasSuffix()` to match files instead,
allowing to match multi-part extensions.

* docs: Document differences in include/exclude behavior.

* docs(paths): Fix typo.

Co-authored-by: Braydon Kains <[email protected]>

---------

Co-authored-by: Braydon Kains <[email protected]>
  • Loading branch information
octo and braydonk authored Nov 25, 2024
1 parent fab8f29 commit d16a06c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
28 changes: 27 additions & 1 deletion docs/paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,36 @@

In standard path mode, you can specify a file or directory path directly. If specifying a file, it will simply include the file. If specifying a directory, it will include every file with the correct extension (as specified in `extensions`, default is `yml` and `yaml`).

This mode does *not* support wildcards, aka. globbing. That means with `*.yaml` yamlfmt will look for a file named asterisk dot yaml. If you require globbing, use the [Doublestar mode](#doublestar) instead.

## Doublestar

In Doublestar mode, paths are specified using the format explained in the [doublestar](https://github.com/bmatcuk/doublestar) package. It is almost identical to bash and git's style of glob pattern specification.
In Doublestar mode, paths are specified using wildcard patterns explained in the [doublestar](https://github.com/bmatcuk/doublestar) package. It is almost identical to bash and git's style of glob pattern specification.

To enable the doublestar mode, set `doublestar: true` in the config file or use the `-dstar` command line flag.

## Include and Exclude

In both modes, `yamlfmt` will allow you to configure include and exclude paths. These can be paths to files in Standard or Doublestar modes, paths to directories in Standard mode, and valid doublestar patterns in Doublestar mode. These paths should be specified **relative to the working directory of `yamlfmt`**. They will work as absolute paths if both the includes and excludes are specified as absolute paths or if both are relative paths, however it will not work as expected if they are mixed together. It usually easier to reason about includes and excludes when always specifying both as relative paths from the directory `yamlfmt` is going to be run in.

Exclude paths can be specified on the command line using the `-exclude` flag.
Paths excluded from the command line are **added* to excluded paths from the config file.

Include paths can be specified on the command line via the positional arguments, i.e. there is no flag for it.
Paths from the command line take precedence over and **replace** any paths configured in the config file.

yamlfmt will build a list of all files to format using the include list, then discard any files matching the exclude list.

## Extensions

*Only in standard mode*

By default, yamlfmt formats all files ending in `.yaml` and `.yml`.
You can modify this behavior using the config file and command line flags.

The config file **sets** the list of extensions.
For example, with `extensions: ["foo"]`, yamlfmt will only match files ending in `.foo` and will *not* match files ending in `.yaml` or `.yml`.
An empty list triggers the default behavior.

The `-extensions` command line flag **adds** to the list of extensions from the config file.
For example, `-extensions yaml.gotmpl` will match files ending in `.yaml.gotmpl` *in addition to* files ending in `.yaml` and `.yml`.
23 changes: 16 additions & 7 deletions path_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,13 @@ func (c *FilepathCollector) CollectPaths() ([]string, error) {
}

func (c *FilepathCollector) walkDirectoryForYaml(dir string) ([]string, error) {
paths := []string{}
var paths []string
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}

extension := ""
if strings.Contains(info.Name(), ".") {
nameParts := strings.Split(info.Name(), ".")
extension = nameParts[len(nameParts)-1]
}
if collections.SliceContains(c.Extensions, extension) {
if c.extensionMatches(info.Name()) {
paths = append(paths, path)
}

Expand All @@ -115,6 +110,20 @@ func (c *FilepathCollector) walkDirectoryForYaml(dir string) ([]string, error) {
return paths, err
}

func (c *FilepathCollector) extensionMatches(name string) bool {
for _, ext := range c.Extensions {
// Users may specify "yaml", but we only want to match ".yaml", not "buyaml".
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}

if strings.HasSuffix(name, ext) {
return true
}
}
return false
}

type DoublestarCollector struct {
Include []string
Exclude []string
Expand Down
20 changes: 20 additions & 0 deletions path_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,26 @@ func TestFilepathCollector(t *testing.T) {
"y.yaml": {},
},
},
{
name: "multi-part extension",
files: []tempfile.Path{
{FileName: "x.yaml"},
{FileName: "y.yaml.gotmpl"},
{FileName: "z.json"},
},
includePatterns: testPatterns{
{pattern: ""}, // with the test this functionally means the whole temp dir
},
extensions: []string{
"yaml",
"yml",
"yaml.gotmpl",
},
expectedFiles: collections.Set[string]{
"x.yaml": {},
"y.yaml.gotmpl": {},
},
},
}.runAll(t, useFilepathCollector)
}

Expand Down

0 comments on commit d16a06c

Please sign in to comment.