Debian Codegraph @

pkg/os/windows/network/vswitch_windows.go

raw
1 package network
2
3 import (
4 "fmt"
5 "strings"
6
7 "github.com/crc-org/crc/v2/pkg/os/windows/powershell"
8 )
9
10 const hypervDefaultVirtualSwitchID = "c08cb7b8-9b3c-408e-8e30-5e16a3aeb444"
11
12 func SelectSwitchByNameOrDefault(name string) (bool, string) {
13 // if named exists
14 if ExistsSwitchByName(name) {
15 return true, name
16 }
17
18 // else use Default
19 return GetDefaultSwitchName()
20 }
21
22 func ExistsSwitchByName(name string) bool {
23 getSwitchByNameCmd := fmt.Sprintf("Get-VMSwitch %s | ForEach-Object { $_.Name }", name)
24 stdOut, stdErr, _ := powershell.Execute(getSwitchByNameCmd)
25
26 // If stdErr contains the command then execution failed
27 if strings.Contains(stdErr, "Get-VMSwitch") {
28 return false
29 }
30
31 if strings.Contains(stdOut, name) {
32 return true
33 }
34 return false
35 }
36
37 func GetDefaultSwitchName() (bool, string) {
38 getDefaultSwitchNameCmd := fmt.Sprintf("[Console]::OutputEncoding = [Text.Encoding]::UTF8; Get-VMSwitch -Id %s | ForEach-Object { $_.Name }", hypervDefaultVirtualSwitchID)
39 stdOut, stdErr, _ := powershell.Execute(getDefaultSwitchNameCmd)
40
41 // If stdErr contains the command then execution failed
42 if strings.Contains(stdErr, "Get-VMSwitch") {
43 return false, ""
44 }
45
46 return true, strings.TrimSpace(stdOut)
47 }