Skip to content

Commit 916a01f

Browse files
authored
Merge pull request #2331 from alexandear/refactor/move-out-regexp
pkg: move regexp compilation out of loops
2 parents 4084b37 + bfb90fa commit 916a01f

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

pkg/store/instance_windows.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ func GetWslStatus(instName string) (string, error) {
9292
}
9393

9494
var instState string
95+
wslListColsRegex := regexp.MustCompile(`\s+`)
9596
// wsl --list --verbose may have different headers depending on localization, just split by line
9697
for _, rows := range strings.Split(strings.ReplaceAll(string(out), "\r\n", "\n"), "\n") {
97-
cols := regexp.MustCompile(`\s+`).Split(strings.TrimSpace(rows), -1)
98+
cols := wslListColsRegex.Split(strings.TrimSpace(rows), -1)
9899
nameIdx := 0
99100
// '*' indicates default instance
100101
if cols[0] == "*" {

pkg/wsl2/wsl_driver_windows.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,13 @@ func (l *LimaWslDriver) Validate() error {
6969
}
7070
}
7171

72-
re, err := regexp.Compile(`.*tar\.*`)
73-
if err != nil {
74-
return fmt.Errorf("failed to compile file check regex: %w", err)
75-
}
72+
// TODO: real filetype checks
73+
tarFileRegex := regexp.MustCompile(`.*tar\.*`)
7674
for i, image := range l.Yaml.Images {
7775
if unknown := reflectutil.UnknownNonEmptyFields(image, "File"); len(unknown) > 0 {
7876
logrus.Warnf("Ignoring: vmType %s: images[%d]: %+v", *l.Yaml.VMType, i, unknown)
7977
}
80-
// TODO: real filetype checks
81-
match := re.MatchString(image.Location)
78+
match := tarFileRegex.MatchString(image.Location)
8279
if image.Arch == *l.Yaml.Arch && !match {
8380
return fmt.Errorf("unsupported image type for vmType: %s, tarball root file system required: %q", *l.Yaml.VMType, image.Location)
8481
}

0 commit comments

Comments
 (0)