-
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
5139943
commit 297278c
Showing
1 changed file
with
41 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,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 | ||
} |