Skip to content

Commit

Permalink
basic: add include_document_start (#20)
Browse files Browse the repository at this point in the history
The yaml.v3 library Encoder does not include the --- at the start of
yaml files, however that is a pretty common pattern. Provide users the
option to include it if they would like.
  • Loading branch information
braydonk authored Aug 23, 2022
1 parent 79d0e08 commit 80328a4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
12 changes: 6 additions & 6 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func (e *Engine) FormatFile(path string) error {
return err
}

func (f *Engine) LintAllFiles() error {
paths, err := CollectPathsToFormat(f.Include, f.Exclude)
func (e *Engine) LintAllFiles() error {
paths, err := CollectPathsToFormat(e.Include, e.Exclude)
if err != nil {
return err
}

lintErrors := NewLintFileErrors()
for _, path := range paths {
err := f.LintFile(path)
err := e.LintFile(path)
if err != nil {
lintErrors.Add(path, err)
}
Expand Down Expand Up @@ -97,16 +97,16 @@ func (e *Engine) LintFile(path string) error {
return nil
}

func (f *Engine) DryRunAllFiles() (string, error) {
paths, err := CollectPathsToFormat(f.Include, f.Exclude)
func (e *Engine) DryRunAllFiles() (string, error) {
paths, err := CollectPathsToFormat(e.Include, e.Exclude)
if err != nil {
return "", err
}

formatErrors := NewFormatFileErrors()
dryRunDiffs := NewDryRunDiffs()
for _, path := range paths {
diff, err := f.DryRunFile(path)
diff, err := e.DryRunFile(path)
if err != nil {
formatErrors.Add(path, err)
} else if diff != "" {
Expand Down
7 changes: 4 additions & 3 deletions formatters/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The basic formatter is a barebones formatter that simply takes the data provided

## Configuration

| Key | Default | Description |
|:--------------|:--------|:------------|
| `indentation` | 2 | The indentation level in spaces to use for the formatted yaml|
| Key | Default | Description |
|:-------------------------|:--------|:------------|
| `indentation` | 2 | The indentation level in spaces to use for the formatted yaml|
| `include_document_start` | false | Include `---` at document start |
3 changes: 2 additions & 1 deletion formatters/basic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
package basic

type Config struct {
Indent int `mapstructure:"indent"`
Indent int `mapstructure:"indent"`
IncludeDocumentStart bool `mapstructure:"include_document_start"`
}

func DefaultConfig() *Config {
Expand Down
8 changes: 8 additions & 0 deletions formatters/basic/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,13 @@ func (f *BasicFormatter) Format(yamlContent []byte) ([]byte, error) {
}
}

if f.Config.IncludeDocumentStart {
return withDocumentStart(b.Bytes()), nil
}
return b.Bytes(), nil
}

func withDocumentStart(document []byte) []byte {
documentStart := "---\n"
return append([]byte(documentStart), document...)
}
14 changes: 14 additions & 0 deletions formatters/basic/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,17 @@ a:
t.Fatalf("expected yaml not to change, result: %s", string(s))
}
}

func TestWithDocumentStart(t *testing.T) {
f := &basic.BasicFormatter{Config: basic.DefaultConfig()}
f.Config.IncludeDocumentStart = true

yaml := "a:"
s, err := f.Format([]byte(yaml))
if err != nil {
t.Fatalf("expected formatting to pass, returned error: %v", err)
}
if strings.Index(string(s), "---\n") != 0 {
t.Fatalf("expected document start to be included, result was: %s", string(s))
}
}

0 comments on commit 80328a4

Please sign in to comment.