Skip to content

Commit 97303ff

Browse files
authored
Apply automatic YAML formatting only to manifests (#1506)
1 parent 1198d91 commit 97303ff

File tree

5 files changed

+52
-22
lines changed

5 files changed

+52
-22
lines changed

internal/builder/dynamic_mappings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func formatResult(result interface{}) ([]byte, error) {
240240
if err != nil {
241241
return nil, errors.New("failed to encode")
242242
}
243-
yamlFormatter := &formatter.YAMLFormatter{}
243+
yamlFormatter := formatter.NewYAMLFormatter(formatter.KeysWithDotActionNone)
244244
d, _, err = yamlFormatter.Format(d)
245245
if err != nil {
246246
return nil, errors.New("failed to format")

internal/formatter/formatter.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1730
type 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

internal/formatter/yaml_formatter.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ import (
99
"fmt"
1010
"strings"
1111

12-
"github.com/Masterminds/semver/v3"
1312
"gopkg.in/yaml.v3"
1413
)
1514

1615
// YAMLFormatter is responsible for formatting the given YAML input.
1716
type YAMLFormatter struct {
18-
specVersion semver.Version
17+
keysWithDotsAction int
1918
}
2019

21-
func NewYAMLFormatter(specVersion semver.Version) *YAMLFormatter {
20+
func NewYAMLFormatter(keysWithDotsAction int) *YAMLFormatter {
2221
return &YAMLFormatter{
23-
specVersion: specVersion,
22+
keysWithDotsAction: keysWithDotsAction,
2423
}
2524
}
2625

@@ -33,9 +32,7 @@ func (f *YAMLFormatter) Format(content []byte) ([]byte, bool, error) {
3332
return nil, false, fmt.Errorf("unmarshalling YAML file failed: %w", err)
3433
}
3534

36-
if !f.specVersion.LessThan(semver.MustParse("3.0.0")) {
37-
extendNestedObjects(&node)
38-
}
35+
applyActionOnKeysWithDots(&node, f.keysWithDotsAction)
3936

4037
var b bytes.Buffer
4138
encoder := yaml.NewEncoder(&b)
@@ -55,6 +52,15 @@ func (f *YAMLFormatter) Format(content []byte) ([]byte, bool, error) {
5552
return formatted, string(content) == string(formatted), nil
5653
}
5754

55+
func applyActionOnKeysWithDots(node *yaml.Node, action int) {
56+
switch action {
57+
case KeysWithDotActionNested:
58+
extendNestedObjects(node)
59+
case KeysWithDotActionNone:
60+
// Nothing to do.
61+
}
62+
}
63+
5864
func extendNestedObjects(node *yaml.Node) {
5965
if node.Kind == yaml.MappingNode {
6066
extendMapNode(node)

internal/formatter/yaml_formatter_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package formatter
77
import (
88
"testing"
99

10-
"github.com/Masterminds/semver/v3"
1110
"github.com/stretchr/testify/assert"
1211
"github.com/stretchr/testify/require"
1312
)
@@ -104,9 +103,7 @@ es.other.level: 13`,
104103
},
105104
}
106105

107-
sv := semver.MustParse("3.0.0")
108-
formatter := NewYAMLFormatter(*sv).Format
109-
106+
formatter := NewYAMLFormatter(KeysWithDotActionNested).Format
110107
for _, c := range cases {
111108
t.Run(c.title, func(t *testing.T) {
112109
result, _, err := formatter([]byte(c.doc))

internal/packages/changelog/yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func formatResult(result interface{}) ([]byte, error) {
125125
if err != nil {
126126
return nil, errors.New("failed to encode")
127127
}
128-
yamlFormatter := &formatter.YAMLFormatter{}
128+
yamlFormatter := formatter.NewYAMLFormatter(formatter.KeysWithDotActionNone)
129129
d, _, err = yamlFormatter.Format(d)
130130
if err != nil {
131131
return nil, errors.New("failed to format")

0 commit comments

Comments
 (0)