@@ -14,14 +14,27 @@ import (
1414 "github.com/elastic/elastic-package/internal/packages"
1515)
1616
17+ const (
18+ KeysWithDotActionNone int = iota
19+ KeysWithDotActionNested
20+ )
21+
22+ type formatterOptions struct {
23+ extension string
24+ specVersion semver.Version
25+ preferedKeysWithDotAction int
26+
27+ failFast bool
28+ }
29+
1730type formatter func (content []byte ) ([]byte , bool , error )
1831
19- func newFormatter (specVersion semver. Version , ext string ) formatter {
20- switch ext {
32+ func newFormatter (options formatterOptions ) formatter {
33+ switch options . extension {
2134 case ".json" :
22- return JSONFormatterBuilder (specVersion ).Format
35+ return JSONFormatterBuilder (options . specVersion ).Format
2336 case ".yaml" , ".yml" :
24- return NewYAMLFormatter (specVersion ).Format
37+ return NewYAMLFormatter (options . preferedKeysWithDotAction ).Format
2538 default :
2639 return nil
2740 }
@@ -37,18 +50,33 @@ func Format(packageRoot string, failFast bool) error {
3750 if err != nil {
3851 return fmt .Errorf ("failed to parse package format version %q: %w" , manifest .SpecVersion , err )
3952 }
53+
4054 err = filepath .Walk (packageRoot , func (path string , info os.FileInfo , err error ) error {
4155 if err != nil {
4256 return err
4357 }
4458
59+ options := formatterOptions {
60+ specVersion : * specVersion ,
61+ extension : filepath .Ext (info .Name ()),
62+ failFast : failFast ,
63+ }
64+
4565 if info .IsDir () && info .Name () == "ingest_pipeline" {
4666 return filepath .SkipDir
4767 }
4868 if info .IsDir () {
4969 return nil
5070 }
51- err = formatFile (path , failFast , * specVersion )
71+
72+ // Configure handling of keys with dots.
73+ if ! specVersion .LessThan (semver .MustParse ("3.0.0" )) {
74+ if info .Name () == "manifest.yml" {
75+ options .preferedKeysWithDotAction = KeysWithDotActionNested
76+ }
77+ }
78+
79+ err = formatFile (path , options )
5280 if err != nil {
5381 return fmt .Errorf ("formatting file failed (path: %s): %w" , path , err )
5482 }
@@ -61,14 +89,13 @@ func Format(packageRoot string, failFast bool) error {
6189 return nil
6290}
6391
64- func formatFile (path string , failFast bool , specVersion semver. Version ) error {
92+ func formatFile (path string , options formatterOptions ) error {
6593 content , err := os .ReadFile (path )
6694 if err != nil {
6795 return fmt .Errorf ("reading file content failed: %w" , err )
6896 }
6997
70- ext := filepath .Ext (filepath .Base (path ))
71- format := newFormatter (specVersion , ext )
98+ format := newFormatter (options )
7299 if format == nil {
73100 return nil // no errors returned as we have few files that will be never formatted (png, svg, log, etc.)
74101 }
@@ -82,7 +109,7 @@ func formatFile(path string, failFast bool, specVersion semver.Version) error {
82109 return nil
83110 }
84111
85- if failFast {
112+ if options . failFast {
86113 return fmt .Errorf ("file is not formatted (path: %s)" , path )
87114 }
88115
0 commit comments