Skip to content

Commit c21cdc3

Browse files
committed
Move TrimTrailingEOL helper to pkg/strings
Looks like we are using `strings.TrimRight(<string>, "\n\r")` at multiple locations so better to move this helper to pkg/strings for better maintainance.
1 parent 03449df commit c21cdc3

6 files changed

Lines changed: 23 additions & 19 deletions

File tree

pkg/crc/cluster/cluster.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/crc-org/crc/pkg/crc/ssh"
2222
crctls "github.com/crc-org/crc/pkg/crc/tls"
2323
"github.com/crc-org/crc/pkg/crc/validation"
24+
crcstrings "github.com/crc-org/crc/pkg/strings"
2425
"github.com/pborman/uuid"
2526
)
2627

@@ -134,7 +135,7 @@ func EnsureSSHKeyPresentInTheCluster(ctx context.Context, ocConfig oc.Config, ss
134135
if err != nil {
135136
return err
136137
}
137-
sshPublicKey := strings.TrimRight(string(sshPublicKeyByte), "\r\n")
138+
sshPublicKey := crcstrings.TrimTrailingEOL(string(sshPublicKeyByte))
138139
if err := WaitForOpenshiftResource(ctx, ocConfig, "machineconfigs"); err != nil {
139140
return err
140141
}

pkg/crc/network/httpproxy/proxy.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/crc-org/crc/pkg/crc/logging"
1313

1414
"github.com/asaskevich/govalidator"
15+
crcstrings "github.com/crc-org/crc/pkg/strings"
1516
"github.com/pkg/errors"
1617
"golang.org/x/net/http/httpproxy"
1718
)
@@ -47,11 +48,7 @@ func readProxyCAData(proxyCAFile string) (string, error) {
4748
if err != nil {
4849
return "", err
4950
}
50-
return trimTrailingEOL(string(proxyCACert)), nil
51-
}
52-
53-
func trimTrailingEOL(s string) string {
54-
return strings.TrimRight(s, "\r\n")
51+
return crcstrings.TrimTrailingEOL(string(proxyCACert)), nil
5552
}
5653

5754
func NewProxyDefaults(httpProxy, httpsProxy, noProxy, proxyCAFile string) (*ProxyConfig, error) {

pkg/crc/network/httpproxy/proxy_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,3 @@ func TestValidateProxyURL(t *testing.T) {
1515
assert.EqualError(t, ValidateProxyURL("company.com:8080", true), "HTTPS proxy URL 'company.com:8080' is not valid: url should start with http:// or https://")
1616
assert.EqualError(t, ValidateProxyURL("https://company.com", false), "HTTP proxy URL 'https://company.com' is not valid: url should start with http://")
1717
}
18-
func TestTrimTrailingEOL(t *testing.T) {
19-
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n"))
20-
assert.Equal(t, "foo", trimTrailingEOL("foo\n"))
21-
assert.Equal(t, "foo", trimTrailingEOL("foo\r\n"))
22-
assert.Equal(t, "foo\r\nbar", trimTrailingEOL("foo\r\nbar\r\n"))
23-
assert.Equal(t, "foo\r\nbar", trimTrailingEOL("foo\r\nbar\r\n\r\n"))
24-
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n\n"))
25-
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n\n\n"))
26-
assert.Equal(t, "", trimTrailingEOL("\r\n"))
27-
assert.Equal(t, "", trimTrailingEOL("\n"))
28-
assert.Equal(t, "", trimTrailingEOL(""))
29-
}

pkg/strings/strings.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ func FirstLine(input string) string {
3434

3535
return lines[0]
3636
}
37+
38+
func TrimTrailingEOL(s string) string {
39+
return strings.TrimRight(s, "\r\n")
40+
}

pkg/strings/strings_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,16 @@ func TestFirstLine(t *testing.T) {
136136
}
137137
})
138138
}
139+
140+
func TestTrimTrailingEOL(t *testing.T) {
141+
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n"))
142+
assert.Equal(t, "foo", TrimTrailingEOL("foo\n"))
143+
assert.Equal(t, "foo", TrimTrailingEOL("foo\r\n"))
144+
assert.Equal(t, "foo\r\nbar", TrimTrailingEOL("foo\r\nbar\r\n"))
145+
assert.Equal(t, "foo\r\nbar", TrimTrailingEOL("foo\r\nbar\r\n\r\n"))
146+
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n\n"))
147+
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n\n\n"))
148+
assert.Equal(t, "", TrimTrailingEOL("\r\n"))
149+
assert.Equal(t, "", TrimTrailingEOL("\n"))
150+
assert.Equal(t, "", TrimTrailingEOL(""))
151+
}

test/extended/util/checks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strconv"
2525
"strings"
2626

27+
crcstrings "github.com/crc-org/crc/pkg/strings"
2728
yaml "gopkg.in/yaml.v3"
2829
)
2930

@@ -91,7 +92,7 @@ func CompareExpectedWithActualNotMatchesRegex(notexpected string, actual string)
9192
}
9293

9394
func CheckFormat(format string, actual string) error {
94-
actual = strings.TrimRight(actual, "\n")
95+
actual = crcstrings.TrimTrailingEOL(actual)
9596
var err error
9697
switch format {
9798
case "URL":

0 commit comments

Comments
 (0)