Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions cmd/kube-score/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ Use "-" as filename to read from STDIN.`, execName(binName))
return fmt.Errorf("failed to initializer parser: %w", err)
}

parsedFiles, err := p.ParseFiles(allFilePointers)
if err != nil {
parsedFiles, parseErr := p.ParseFiles(allFilePointers)
if parseErr != nil && parsedFiles == nil {
return fmt.Errorf("failed to parse files: %w", err)
}

Expand All @@ -231,7 +231,11 @@ Use "-" as filename to read from STDIN.`, execName(binName))
case *exitOneOnWarning && scoreCard.AnyBelowOrEqualToGrade(scorecard.GradeWarning):
exitCode = 1
default:
exitCode = 0
if parseErr != nil {
exitCode = 1
} else {
exitCode = 0
}
}

var r io.Reader
Expand Down
8 changes: 6 additions & 2 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func Empty() ks.AllTypes {
func (p *Parser) ParseFiles(files []ks.NamedReader) (ks.AllTypes, error) {
s := &parsedObjects{}

errorOccured := false
for _, namedReader := range files {
fullFile, err := io.ReadAll(namedReader)
if err != nil {
Expand All @@ -179,14 +180,17 @@ func (p *Parser) ParseFiles(files []ks.NamedReader) (ks.AllTypes, error) {

if len(bytes.TrimSpace(fileContents)) > 0 {
if err := p.detectAndDecode(s, namedReader.Name(), offset, fileContents); err != nil {
return nil, err
errorOccured = true
log.Printf("Failed to parse '%s': %v", namedReader.Name(), err)
}
}

offset += 2 + bytes.Count(fileContents, []byte("\n"))
}
}

if errorOccured {
return s, fmt.Errorf("one or more files failed to parse")
}
return s, nil
}

Expand Down
2 changes: 1 addition & 1 deletion parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestParse(t *testing.T) {
}{
{
"testdata/invalid-yaml.yaml",
fmt.Errorf("Failed to parse /v1, Kind=Service: err=json: cannot unmarshal string into Go struct field ServicePort.spec.ports.nodePort of type int32"),
fmt.Errorf("one or more files failed to parse"),
}, {
"testdata/valid-yaml.yaml",
nil,
Expand Down