Skip to content

Commit 37b74b6

Browse files
authored
Merge pull request #2184 from alexandear/pkg-windows-unreachable-code
pkg/windows: remove unreachable code; compile regexp once
2 parents 253a435 + a137031 commit 37b74b6

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

pkg/windows/wsl_util_windows.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"strings"
77
)
88

9+
// vmIDRegex is a regular expression to extract the VM ID from the command line of wslhost.exe.
10+
var vmIDRegex = regexp.MustCompile(`--vm-id\s\{(?P<vmID>.{36})\}`)
11+
912
// GetInstanceVMID returns the VM ID of a running WSL instance.
1013
func GetInstanceVMID(instanceName string) (string, error) {
1114
distroID, err := GetDistroID(instanceName)
@@ -18,16 +21,11 @@ func GetInstanceVMID(instanceName string) (string, error) {
1821
return "", err
1922
}
2023

21-
re := regexp.MustCompile(`--vm-id\s\{(?P<vmID>.{36})\}`)
22-
if err != nil {
23-
return "", err
24-
}
25-
2624
vmID := ""
2725
for _, cmdLine := range cmdLines {
2826
if strings.Contains(cmdLine, distroID) {
29-
if matches := re.FindStringSubmatch(cmdLine); matches != nil {
30-
vmID = matches[re.SubexpIndex("vmID")]
27+
if matches := vmIDRegex.FindStringSubmatch(cmdLine); matches != nil {
28+
vmID = matches[vmIDRegex.SubexpIndex("vmID")]
3129
break
3230
}
3331
}

0 commit comments

Comments
 (0)