Skip to content

Commit 2fa56f0

Browse files
cfergeauanjannath
authored andcommitted
Add SplitLines helper to pkg/strings
We have 2 duplicate implementations of this code. It's renamed to 'Split' instead of 'Parse' to be more consistent with other go APIs.
1 parent bca2d4b commit 2fa56f0

5 files changed

Lines changed: 69 additions & 33 deletions

File tree

pkg/crc/services/dns/dns_windows.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package dns
22

33
import (
4-
"bufio"
54
"fmt"
6-
"strings"
75
"time"
86

97
"github.com/crc-org/crc/pkg/crc/network"
@@ -41,7 +39,7 @@ func getInterfaceNameserverValues(iface string) []string {
4139
getDNSServerCommand := fmt.Sprintf(`(Get-DnsClientServerAddress "%s")[0].ServerAddresses`, iface)
4240
stdOut, _, _ := powershell.Execute(getDNSServerCommand)
4341

44-
return parseLines(stdOut)
42+
return crcstrings.SplitLines(stdOut)
4543
}
4644

4745
func setInterfaceNameserverValue(iface string, address string) {
@@ -51,14 +49,3 @@ func setInterfaceNameserverValue(iface string, address string) {
5149
// ignore the error as this is useless (prefer not to use nolint here)
5250
_ = win32.ShellExecuteAsAdmin(fmt.Sprintf("add dns server address to interface %s", iface), win32.HwndDesktop, exe, args, "", 0)
5351
}
54-
55-
func parseLines(input string) []string {
56-
output := []string{}
57-
58-
s := bufio.NewScanner(strings.NewReader(input))
59-
for s.Scan() {
60-
output = append(output, s.Text())
61-
}
62-
63-
return output
64-
}

pkg/drivers/hyperv/hyperv_windows.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
log "github.com/crc-org/crc/pkg/crc/logging"
1313
crcos "github.com/crc-org/crc/pkg/os"
1414
"github.com/crc-org/crc/pkg/os/windows/powershell"
15+
crcstrings "github.com/crc-org/crc/pkg/strings"
1516
"github.com/crc-org/machine/libmachine/drivers"
1617
"github.com/crc-org/machine/libmachine/state"
1718
)
@@ -93,7 +94,7 @@ func (d *Driver) GetState() (state.State, error) {
9394
return state.Error, fmt.Errorf("Failed to find the VM status: %v - %s", err, stderr)
9495
}
9596

96-
resp := parseLines(stdout)
97+
resp := crcstrings.SplitLines(stdout)
9798
if len(resp) < 1 {
9899
return state.Error, fmt.Errorf("unexpected Hyper-V state %s", stdout)
99100
}
@@ -256,7 +257,7 @@ func (d *Driver) chooseVirtualSwitch() (string, error) {
256257
return "", err
257258
}
258259

259-
switches := parseLines(stdout)
260+
switches := crcstrings.SplitLines(stdout)
260261

261262
found := false
262263
for _, name := range switches {
@@ -397,7 +398,7 @@ func (d *Driver) GetIP() (string, error) {
397398
return "", err
398399
}
399400

400-
resp := parseLines(stdout)
401+
resp := crcstrings.SplitLines(stdout)
401402
if len(resp) < 1 {
402403
return "", fmt.Errorf("IP not found")
403404
}

pkg/drivers/hyperv/powershell_windows.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package hyperv
22

33
import (
4-
"bufio"
54
"errors"
65
"fmt"
7-
"strings"
86

97
log "github.com/crc-org/crc/pkg/crc/logging"
108
"github.com/crc-org/crc/pkg/os/windows/powershell"
9+
crcstrings "github.com/crc-org/crc/pkg/strings"
1110
)
1211

1312
var (
@@ -26,24 +25,13 @@ func cmd(args ...string) error {
2625
return err
2726
}
2827

29-
func parseLines(stdout string) []string {
30-
resp := []string{}
31-
32-
s := bufio.NewScanner(strings.NewReader(stdout))
33-
for s.Scan() {
34-
resp = append(resp, s.Text())
35-
}
36-
37-
return resp
38-
}
39-
4028
func hypervAvailable() error {
4129
stdout, err := cmdOut("@(Get-Module -ListAvailable hyper-v).Name | Get-Unique")
4230
if err != nil {
4331
return err
4432
}
4533

46-
resp := parseLines(stdout)
34+
resp := crcstrings.SplitLines(stdout)
4735
if resp[0] != "Hyper-V" {
4836
return ErrNotInstalled
4937
}
@@ -74,7 +62,7 @@ func isHypervAdministrator() bool {
7462
return false
7563
}
7664

77-
resp := parseLines(stdout)
65+
resp := crcstrings.SplitLines(stdout)
7866
return resp[0] == "True"
7967
}
8068

@@ -84,7 +72,7 @@ func isWindowsAdministrator() (bool, error) {
8472
return false, err
8573
}
8674

87-
resp := parseLines(stdout)
75+
resp := crcstrings.SplitLines(stdout)
8876
return resp[0] == "True", nil
8977
}
9078

pkg/strings/strings.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package strings
22

3+
import (
4+
"bufio"
5+
"strings"
6+
)
7+
38
func Contains(input []string, match string) bool {
49
for _, v := range input {
510
if v == match {
@@ -8,3 +13,15 @@ func Contains(input []string, match string) bool {
813
}
914
return false
1015
}
16+
17+
// Split a multi line string in an array of string, one for each line
18+
func SplitLines(input string) []string {
19+
output := []string{}
20+
21+
s := bufio.NewScanner(strings.NewReader(input))
22+
for s.Scan() {
23+
output = append(output, s.Text())
24+
}
25+
26+
return output
27+
}

pkg/strings/strings_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,46 @@ func TestContains(t *testing.T) {
5050
}
5151
})
5252
}
53+
54+
type splitLinesTest struct {
55+
input string
56+
splitOutput []string
57+
}
58+
59+
var splitLinesTests = map[string]splitLinesTest{
60+
"ThreeLines": {
61+
input: "line1\nline2\nline3\n",
62+
splitOutput: []string{"line1", "line2", "line3"},
63+
},
64+
"ThreeLinesNoFinalEOL": {
65+
input: "line1\nline2\nline3",
66+
splitOutput: []string{"line1", "line2", "line3"},
67+
},
68+
"WindowsEOL": {
69+
input: "line1\r\nline2\r\nline3\r\n",
70+
splitOutput: []string{"line1", "line2", "line3"},
71+
},
72+
"EmptyString": {
73+
input: "",
74+
splitOutput: []string{},
75+
},
76+
"EOLOnly": {
77+
input: "\n",
78+
splitOutput: []string{""},
79+
},
80+
"NoEOL": {
81+
input: "line1",
82+
splitOutput: []string{"line1"},
83+
},
84+
}
85+
86+
func TestSplitLines(t *testing.T) {
87+
t.Run("SplitLines", func(t *testing.T) {
88+
for name, test := range splitLinesTests {
89+
t.Run(name, func(t *testing.T) {
90+
output := SplitLines(test.input)
91+
assert.Equal(t, test.splitOutput, output)
92+
})
93+
}
94+
})
95+
}

0 commit comments

Comments
 (0)