Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function for trimming whitespace on yaml files #44

Merged
merged 3 commits into from
May 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion yq/yq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -31,7 +32,7 @@ func (y *Yq) VersionCheck(ctx context.Context) error {
return fmt.Errorf("unable to check version: %w", err)
}
version := strings.TrimSpace(out.String())
rgx := `mikefarah.* version 4\..`
rgx := `.*mikefarah.* version v?4\..*`
cmp := regexp.MustCompile(rgx)
if !cmp.MatchString(version) {
return fmt.Errorf("must match version 4 (%s), saw -- %s", rgx, version)
Expand All @@ -56,6 +57,46 @@ func (y *Yq) ReformatYAMLDir(ctx context.Context, root string) error {
return nil
}

func (y *Yq) TrimTrailingWhitespace(ctx context.Context, path string) error {
input, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("unable to read file %s: %w", path, err)
}

lines := strings.Split(string(input), "\n")
var newLines []string
for _, line := range lines {
newLines = append(newLines, strings.TrimRight(line, " "))
}

output := []byte(strings.Join(newLines, "\n"))

if !bytes.Equal(input, output) {
err = os.WriteFile(path, output, 0644)
if err != nil {
return fmt.Errorf("unable to write file %s: %w", path, err)
}
}

return nil
}

func (y *Yq) TrimTrailingWhitespaceForYAMLDir(ctx context.Context, root string) error {
yamlFiles, err := files.AllWithExtensionInDir(root, ".yaml")
if err != nil {
return fmt.Errorf("unable to read directory %s: %w", root, err)
}

for _, file := range yamlFiles {
err := y.TrimTrailingWhitespace(ctx, filepath.Join(root, file))
if err != nil {
return fmt.Errorf("unable to trim trailing whitespace %s: %w", file, err)
}
}

return nil
}

// Reformat all YAML files in PATH with YQ
func Reformat(ctx context.Context, path string) error {
return Instance.Reformat(ctx, path)
Expand All @@ -70,3 +111,12 @@ func VersionCheck(ctx context.Context) error {
func ReformatYAMLDir(ctx context.Context, root string) error {
return Instance.ReformatYAMLDir(ctx, root)
}

func TrimTrailingWhitespace(ctx context.Context, path string) error {
return Instance.TrimTrailingWhitespace(ctx, path)
}

// TrimTrailingWhitespaceForYAMLDir trims trailing whitespace for all YAML files in PATH
func TrimTrailingWhitespaceForYAMLDir(ctx context.Context, root string) error {
return Instance.TrimTrailingWhitespaceForYAMLDir(ctx, root)
}