Skip to content

Commit

Permalink
Move TrimTrailingEOL helper to pkg/strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
praveenkumar committed Aug 24, 2023
1 parent 03449df commit c21cdc3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
3 changes: 2 additions & 1 deletion pkg/crc/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/crc-org/crc/pkg/crc/ssh"
crctls "github.com/crc-org/crc/pkg/crc/tls"
"github.com/crc-org/crc/pkg/crc/validation"
crcstrings "github.com/crc-org/crc/pkg/strings"
"github.com/pborman/uuid"
)

Expand Down Expand Up @@ -134,7 +135,7 @@ func EnsureSSHKeyPresentInTheCluster(ctx context.Context, ocConfig oc.Config, ss
if err != nil {
return err
}
sshPublicKey := strings.TrimRight(string(sshPublicKeyByte), "\r\n")
sshPublicKey := crcstrings.TrimTrailingEOL(string(sshPublicKeyByte))
if err := WaitForOpenshiftResource(ctx, ocConfig, "machineconfigs"); err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/crc/network/httpproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/crc-org/crc/pkg/crc/logging"

"github.com/asaskevich/govalidator"
crcstrings "github.com/crc-org/crc/pkg/strings"
"github.com/pkg/errors"
"golang.org/x/net/http/httpproxy"
)
Expand Down Expand Up @@ -47,11 +48,7 @@ func readProxyCAData(proxyCAFile string) (string, error) {
if err != nil {
return "", err
}
return trimTrailingEOL(string(proxyCACert)), nil
}

func trimTrailingEOL(s string) string {
return strings.TrimRight(s, "\r\n")
return crcstrings.TrimTrailingEOL(string(proxyCACert)), nil
}

func NewProxyDefaults(httpProxy, httpsProxy, noProxy, proxyCAFile string) (*ProxyConfig, error) {
Expand Down
12 changes: 0 additions & 12 deletions pkg/crc/network/httpproxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,3 @@ func TestValidateProxyURL(t *testing.T) {
assert.EqualError(t, ValidateProxyURL("company.com:8080", true), "HTTPS proxy URL 'company.com:8080' is not valid: url should start with http:// or https://")
assert.EqualError(t, ValidateProxyURL("https://company.com", false), "HTTP proxy URL 'https://company.com' is not valid: url should start with http://")
}
func TestTrimTrailingEOL(t *testing.T) {
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n"))
assert.Equal(t, "foo", trimTrailingEOL("foo\n"))
assert.Equal(t, "foo", trimTrailingEOL("foo\r\n"))
assert.Equal(t, "foo\r\nbar", trimTrailingEOL("foo\r\nbar\r\n"))
assert.Equal(t, "foo\r\nbar", trimTrailingEOL("foo\r\nbar\r\n\r\n"))
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n\n"))
assert.Equal(t, "foo\nbar", trimTrailingEOL("foo\nbar\n\n\n"))
assert.Equal(t, "", trimTrailingEOL("\r\n"))
assert.Equal(t, "", trimTrailingEOL("\n"))
assert.Equal(t, "", trimTrailingEOL(""))
}
4 changes: 4 additions & 0 deletions pkg/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ func FirstLine(input string) string {

return lines[0]
}

func TrimTrailingEOL(s string) string {
return strings.TrimRight(s, "\r\n")
}
13 changes: 13 additions & 0 deletions pkg/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,16 @@ func TestFirstLine(t *testing.T) {
}
})
}

func TestTrimTrailingEOL(t *testing.T) {
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n"))
assert.Equal(t, "foo", TrimTrailingEOL("foo\n"))
assert.Equal(t, "foo", TrimTrailingEOL("foo\r\n"))
assert.Equal(t, "foo\r\nbar", TrimTrailingEOL("foo\r\nbar\r\n"))
assert.Equal(t, "foo\r\nbar", TrimTrailingEOL("foo\r\nbar\r\n\r\n"))
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n\n"))
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n\n\n"))
assert.Equal(t, "", TrimTrailingEOL("\r\n"))
assert.Equal(t, "", TrimTrailingEOL("\n"))
assert.Equal(t, "", TrimTrailingEOL(""))
}
3 changes: 2 additions & 1 deletion test/extended/util/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"
"strings"

crcstrings "github.com/crc-org/crc/pkg/strings"
yaml "gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -91,7 +92,7 @@ func CompareExpectedWithActualNotMatchesRegex(notexpected string, actual string)
}

func CheckFormat(format string, actual string) error {
actual = strings.TrimRight(actual, "\n")
actual = crcstrings.TrimTrailingEOL(actual)
var err error
switch format {
case "URL":
Expand Down

0 comments on commit c21cdc3

Please sign in to comment.