Skip to content

Commit

Permalink
feat(format): continue formatting files on error
Browse files Browse the repository at this point in the history
if an error is returned all failed files will be specified in the message
  • Loading branch information
ahaasler committed Jul 20, 2021
1 parent 8c5aa09 commit 8d75baf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
22 changes: 18 additions & 4 deletions pkg/format/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ SOFTWARE.
package format

import (
"fmt"
"strings"

"github.com/amplia-iiot/yutil/internal/io"
)

Expand Down Expand Up @@ -57,25 +60,36 @@ func FormatFileInPlaceB(file, backupSuffix string) error {
}

// FormatFilesInPlace formats a list of yaml files, modifying the original
// files.
// files. An attempt to format each file will be made regardless of previous
// errors. The final error message will specify all errored files.
func FormatFilesInPlace(files []string) error {
var errors []string
for _, file := range files {
err := FormatFileInPlace(file)
if err != nil {
return err
errors = append(errors, fmt.Sprintf("%s - %s", file, err.Error()))
}
}
if len(errors) > 0 {
return fmt.Errorf(strings.Join(errors, "\n"))
}
return nil
}

// FormatFilesInPlaceB formats a list of yaml files, creating a backup for each
// file with a suffix before modifying each file.
// file with a suffix before modifying each file. An attempt to format each file
// will be made regardless of previous errors. The final error message will
// specify all errored files.
func FormatFilesInPlaceB(files []string, backupSuffix string) error {
var errors []string
for _, file := range files {
err := FormatFileInPlaceB(file, backupSuffix)
if err != nil {
return err
errors = append(errors, fmt.Sprintf("%s - %s", file, err.Error()))
}
}
if len(errors) > 0 {
return fmt.Errorf(strings.Join(errors, "\n"))
}
return nil
}
23 changes: 13 additions & 10 deletions pkg/format/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ func TestFormatFilesInPlaceInvalid(t *testing.T) {
// Not exists
{
// Do not existing file, use one directly in tmp (that doesn't exists)
tmpPath: "tmp/not-exists",
invalid: true,
tmpPath: "tmp/not-exists",
invalid: true,
expected: "no such file or directory",
},
// Will fail because there is a previous error
// OK
{
file: "prod",
invalid: true,
file: "prod",
},
}
// Prepare tmp folder
Expand All @@ -312,6 +312,7 @@ func TestFormatFilesInPlaceInvalid(t *testing.T) {
for _, d := range data {
if d.invalid {
itesting.AssertError(t, d.expected, err)
itesting.AssertError(t, d.tmpPath, err)
}
if d.copiedToTmp {
if d.invalid {
Expand Down Expand Up @@ -390,13 +391,14 @@ func TestFormatFilesInPlaceBInvalid(t *testing.T) {
// Not exists
{
// Do not existing file, use one directly in tmp (that doesn't exists)
tmpPath: "tmp/not-exists",
invalid: true,
tmpPath: "tmp/not-exists",
expected: "no such file or directory",
invalid: true,
},
// Will fail because there is a previous error
// OK
{
file: "prod",
invalid: true,
file: "prod",
shouldBackupExist: true,
},
}
// Prepare tmp folder
Expand All @@ -418,6 +420,7 @@ func TestFormatFilesInPlaceBInvalid(t *testing.T) {
for _, d := range data {
if d.invalid {
itesting.AssertError(t, d.expected, err)
itesting.AssertError(t, d.tmpPath, err)
}
if d.copiedToTmp {
if d.invalid {
Expand Down

0 comments on commit 8d75baf

Please sign in to comment.