From f1712a6b224c9e494b536215e10dbbb7bf29a2df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20N=C3=A4gele?= Date: Wed, 24 Apr 2019 22:25:54 +0200 Subject: [PATCH] Trim leading invalid chars from names (#151) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Reinhard Nägele --- pkg/util/util.go | 8 ++++---- pkg/util/util_test.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/util/util.go b/pkg/util/util.go index 10beffd8..45d56631 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -219,13 +219,13 @@ func PrintDelimiterLine(delimiterChar string) { func SanitizeName(s string, maxLength int) string { reg := regexp.MustCompile("^[^a-zA-Z0-9]+") - processed := reg.ReplaceAllString(s, "") - excess := len(processed) - maxLength + excess := len(s) - maxLength + result := s if excess > 0 { - return processed[excess:] + result = s[excess:] } - return processed + return reg.ReplaceAllString(result, "") } func GetRandomPort() (int, error) { diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 11bd3ff3..9de19a38 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -79,7 +79,7 @@ func TestSanitizeName(t *testing.T) { {"way-longer-than-max-length", 10, "max-length"}, {"one-shorter-than-max-length", len("one-shorter-than-max-length") + 1, "one-shorter-than-max-length"}, {"oone-longer-than-max-length", len("oone-longer-than-max-length") - 1, "one-longer-than-max-length"}, - {"-starts-with-invalid-char", 63, "starts-with-invalid-char"}, + {"foo-would-start-with-hyphen-after-trimming", len("foo-would-start-with-hyphen-after-trimming") - 3, "would-start-with-hyphen-after-trimming"}, } for index, testData := range testDataSlice {