Skip to content

Commit

Permalink
Apply automatic YAML formatting only to manifests (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano authored Oct 17, 2023
1 parent 1198d91 commit 97303ff
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/builder/dynamic_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func formatResult(result interface{}) ([]byte, error) {
if err != nil {
return nil, errors.New("failed to encode")
}
yamlFormatter := &formatter.YAMLFormatter{}
yamlFormatter := formatter.NewYAMLFormatter(formatter.KeysWithDotActionNone)
d, _, err = yamlFormatter.Format(d)
if err != nil {
return nil, errors.New("failed to format")
Expand Down
45 changes: 36 additions & 9 deletions internal/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,27 @@ import (
"github.com/elastic/elastic-package/internal/packages"
)

const (
KeysWithDotActionNone int = iota
KeysWithDotActionNested
)

type formatterOptions struct {
extension string
specVersion semver.Version
preferedKeysWithDotAction int

failFast bool
}

type formatter func(content []byte) ([]byte, bool, error)

func newFormatter(specVersion semver.Version, ext string) formatter {
switch ext {
func newFormatter(options formatterOptions) formatter {
switch options.extension {
case ".json":
return JSONFormatterBuilder(specVersion).Format
return JSONFormatterBuilder(options.specVersion).Format
case ".yaml", ".yml":
return NewYAMLFormatter(specVersion).Format
return NewYAMLFormatter(options.preferedKeysWithDotAction).Format
default:
return nil
}
Expand All @@ -37,18 +50,33 @@ func Format(packageRoot string, failFast bool) error {
if err != nil {
return fmt.Errorf("failed to parse package format version %q: %w", manifest.SpecVersion, err)
}

err = filepath.Walk(packageRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

options := formatterOptions{
specVersion: *specVersion,
extension: filepath.Ext(info.Name()),
failFast: failFast,
}

if info.IsDir() && info.Name() == "ingest_pipeline" {
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
err = formatFile(path, failFast, *specVersion)

// Configure handling of keys with dots.
if !specVersion.LessThan(semver.MustParse("3.0.0")) {
if info.Name() == "manifest.yml" {
options.preferedKeysWithDotAction = KeysWithDotActionNested
}
}

err = formatFile(path, options)
if err != nil {
return fmt.Errorf("formatting file failed (path: %s): %w", path, err)
}
Expand All @@ -61,14 +89,13 @@ func Format(packageRoot string, failFast bool) error {
return nil
}

func formatFile(path string, failFast bool, specVersion semver.Version) error {
func formatFile(path string, options formatterOptions) error {
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("reading file content failed: %w", err)
}

ext := filepath.Ext(filepath.Base(path))
format := newFormatter(specVersion, ext)
format := newFormatter(options)
if format == nil {
return nil // no errors returned as we have few files that will be never formatted (png, svg, log, etc.)
}
Expand All @@ -82,7 +109,7 @@ func formatFile(path string, failFast bool, specVersion semver.Version) error {
return nil
}

if failFast {
if options.failFast {
return fmt.Errorf("file is not formatted (path: %s)", path)
}

Expand Down
20 changes: 13 additions & 7 deletions internal/formatter/yaml_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import (
"fmt"
"strings"

"github.com/Masterminds/semver/v3"
"gopkg.in/yaml.v3"
)

// YAMLFormatter is responsible for formatting the given YAML input.
type YAMLFormatter struct {
specVersion semver.Version
keysWithDotsAction int
}

func NewYAMLFormatter(specVersion semver.Version) *YAMLFormatter {
func NewYAMLFormatter(keysWithDotsAction int) *YAMLFormatter {
return &YAMLFormatter{
specVersion: specVersion,
keysWithDotsAction: keysWithDotsAction,
}
}

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

if !f.specVersion.LessThan(semver.MustParse("3.0.0")) {
extendNestedObjects(&node)
}
applyActionOnKeysWithDots(&node, f.keysWithDotsAction)

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

func applyActionOnKeysWithDots(node *yaml.Node, action int) {
switch action {
case KeysWithDotActionNested:
extendNestedObjects(node)
case KeysWithDotActionNone:
// Nothing to do.
}
}

func extendNestedObjects(node *yaml.Node) {
if node.Kind == yaml.MappingNode {
extendMapNode(node)
Expand Down
5 changes: 1 addition & 4 deletions internal/formatter/yaml_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package formatter
import (
"testing"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -104,9 +103,7 @@ es.other.level: 13`,
},
}

sv := semver.MustParse("3.0.0")
formatter := NewYAMLFormatter(*sv).Format

formatter := NewYAMLFormatter(KeysWithDotActionNested).Format
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
result, _, err := formatter([]byte(c.doc))
Expand Down
2 changes: 1 addition & 1 deletion internal/packages/changelog/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func formatResult(result interface{}) ([]byte, error) {
if err != nil {
return nil, errors.New("failed to encode")
}
yamlFormatter := &formatter.YAMLFormatter{}
yamlFormatter := formatter.NewYAMLFormatter(formatter.KeysWithDotActionNone)
d, _, err = yamlFormatter.Format(d)
if err != nil {
return nil, errors.New("failed to format")
Expand Down

0 comments on commit 97303ff

Please sign in to comment.