Skip to content

Commit

Permalink
e2e: read image replacements from file
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen authored and burgerdev committed Apr 16, 2024
1 parent 5139943 commit 297278c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions e2e/internal/kuberesource/lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kuberesource

import (
"bufio"
"fmt"
"io"
"regexp"
)

var replacementRE = regexp.MustCompile(`(?P<image>[^\s=]+)\s*=\s*(?P<replacement>\S+)`)

func ImageReplacementsFromFile(file io.ReadCloser) (map[string]string, error) {
m := make(map[string]string)

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 || line[0] == '#' {
continue
}

matches := replacementRE.FindStringSubmatch(line)
if matches == nil {
return nil, fmt.Errorf("invalid image line: %s", line)
}

if replacementRE.SubexpIndex("image") == -1 {
return nil, fmt.Errorf("image not found for image line: %s", line)
}
image := matches[replacementRE.SubexpIndex("image")]

if replacementRE.SubexpIndex("replacement") == -1 {
return nil, fmt.Errorf("replacement not found for image line: %s", line)
}
replacement := matches[replacementRE.SubexpIndex("replacement")]

m[image] = replacement
}

return m, nil
}

0 comments on commit 297278c

Please sign in to comment.