-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
e2e: read image replacements from file
Signed-off-by: Paul Meyer <[email protected]>
- Loading branch information
1 parent
e4c62ca
commit 15df7a2
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package kuberesource | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"regexp" | ||
) | ||
|
||
var replacementRE = regexp.MustCompile(`(?P<image>[^\s=]+)\s*=\s*(?P<replacement>\S+)`) | ||
|
||
// ImageReplacementsFromFile parses the containerlookup file into a map. | ||
// | ||
// The file is expected to contain newline-separated pairs of images and their intended | ||
// replacement, separated by an = sign. Empty lines and lines starting with the pound character | ||
// are ignored. This file is populated by container image build rules in the justfile. | ||
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 | ||
} |