Skip to content

Commit

Permalink
handle error from regexp.Compile
Browse files Browse the repository at this point in the history
Signed-off-by: Edmund Ochieng <[email protected]>
  • Loading branch information
OchiengEd committed Jul 31, 2023
1 parent 248b703 commit b1be887
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 7 additions & 4 deletions internal/chartverifier/checks/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,16 @@ func getImageReferences(chartURI string, vals map[string]interface{}, kubeVersio
return nil, err
}

return getImagesFromContent(txt), nil
return getImagesFromContent(txt)
}

// getImagesFromContent evaluates generated templates from
// helm and extracts images which are returned in a slice
func getImagesFromContent(content string) []string {
re := regexp.MustCompile(`\s+image\:\s+(?P<image>.*)\n`)
func getImagesFromContent(content string) ([]string, error) {
re, err := regexp.Compile(`\s+image\:\s+(?P<image>.*)\n`)
if err != nil {
return nil, fmt.Errorf("error getting images; %v", err)
}
matches := re.FindAllStringSubmatch(content, -1)
imageMap := make(map[string]struct{})
for _, match := range matches {
Expand All @@ -239,7 +242,7 @@ func getImagesFromContent(content string) []string {
images = append(images, k)
}

return images
return images, nil
}

func getCacheDir(opts *CheckOptions) string {
Expand Down
5 changes: 3 additions & 2 deletions internal/chartverifier/checks/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestLongLineTemplate(t *testing.T) {
content, err := os.ReadFile("templates/test-template.yaml")
require.NoError(t, err)

images, err := getImagesFromContent(string(content)), nil
images, err := getImagesFromContent(string(content))
require.NoError(t, err)

require.Equal(t, len(images), 2)
Expand Down Expand Up @@ -212,7 +212,8 @@ func TestGetImagesFromContent(t *testing.T) {
}

t.Run(test.name, func(t *testing.T) {
got := getImagesFromContent(test.content)
got, err := getImagesFromContent(test.content)
require.Nil(t, err)
if testing.Verbose() {
t.Logf("got %d images", len(got))
}
Expand Down

0 comments on commit b1be887

Please sign in to comment.