Skip to content

Commit

Permalink
pkg: refactor to compile regexp only once
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Redko <oleksandr.red+github@gmail.com>
  • Loading branch information
alexandear committed May 9, 2024
1 parent 13f1a58 commit e558941
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
5 changes: 3 additions & 2 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -1048,10 +1048,11 @@ func Accel(arch limayaml.Arch) string {
return "tcg"
}

var qemuVersionRegex = regexp.MustCompile(`^QEMU emulator version (\d+\.\d+\.\d+)`)

func parseQemuVersion(output string) (*semver.Version, error) {
lines := strings.Split(output, "\n")
regex := regexp.MustCompile(`^QEMU emulator version (\d+\.\d+\.\d+)`)
matches := regex.FindStringSubmatch(lines[0])
matches := qemuVersionRegex.FindStringSubmatch(lines[0])
if len(matches) == 2 {
return semver.New(matches[1]), nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,10 @@ func SSHArgsFromOpts(opts []string) []string {
return args
}

var openSSHVersionRegex = regexp.MustCompile(`^OpenSSH_(\d+\.\d+)(?:p(\d+))?\b`)

func ParseOpenSSHVersion(version []byte) *semver.Version {
regex := regexp.MustCompile(`^OpenSSH_(\d+\.\d+)(?:p(\d+))?\b`)
matches := regex.FindSubmatch(version)
matches := openSSHVersionRegex.FindSubmatch(version)
if len(matches) == 3 {
if len(matches[2]) == 0 {
matches[2] = []byte("0")
Expand Down
3 changes: 2 additions & 1 deletion pkg/store/instance_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ func GetWslStatus(instName string) (string, error) {
}

var instState string
wslListColsRegex := regexp.MustCompile(`\s+`)
// wsl --list --verbose may have different headers depending on localization, just split by line
for _, rows := range strings.Split(strings.ReplaceAll(string(out), "\r\n", "\n"), "\n") {
cols := regexp.MustCompile(`\s+`).Split(strings.TrimSpace(rows), -1)
cols := wslListColsRegex.Split(strings.TrimSpace(rows), -1)
nameIdx := 0
// '*' indicates default instance
if cols[0] == "*" {
Expand Down
7 changes: 2 additions & 5 deletions pkg/wsl2/wsl_driver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,13 @@ func (l *LimaWslDriver) Validate() error {
}
}

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

0 comments on commit e558941

Please sign in to comment.