From bc772e20519fcd7145b48a94697bc286ba0728c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20N=C3=A4gele?= Date: Mon, 22 Apr 2019 18:02:20 +0200 Subject: [PATCH] Trim leading hyphens from names (#143) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Reinhard Nägele --- pkg/chart/chart.go | 4 ++-- pkg/util/util.go | 12 ++++++++---- pkg/util/util_test.go | 15 ++++++++------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index 24eca74..5c24cda 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -198,8 +198,8 @@ func (c *Chart) CreateInstallParams(buildID string) (release string, namespace s namespace = fmt.Sprintf("%s-%s", namespace, buildID) } randomSuffix := util.RandomString(10) - release = util.TruncateLeft(fmt.Sprintf("%s-%s", release, randomSuffix), maxNameLength) - namespace = util.TruncateLeft(fmt.Sprintf("%s-%s", namespace, randomSuffix), maxNameLength) + release = util.SanitizeName(fmt.Sprintf("%s-%s", release, randomSuffix), maxNameLength) + namespace = util.SanitizeName(fmt.Sprintf("%s-%s", namespace, randomSuffix), maxNameLength) return } diff --git a/pkg/util/util.go b/pkg/util/util.go index 4d88a3c..10beffd 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -22,6 +22,7 @@ import ( "os" "path" "path/filepath" + "regexp" "strings" "time" @@ -216,12 +217,15 @@ func PrintDelimiterLine(delimiterChar string) { fmt.Println(strings.Join(delim, "")) } -func TruncateLeft(s string, maxLength int) string { - excess := len(s) - maxLength +func SanitizeName(s string, maxLength int) string { + reg := regexp.MustCompile("^[^a-zA-Z0-9]+") + processed := reg.ReplaceAllString(s, "") + + excess := len(processed) - maxLength if excess > 0 { - return s[excess:] + return processed[excess:] } - return s + return processed } func GetRandomPort() (int, error) { diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 8547449..11bd3ff 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -68,22 +68,23 @@ func TestCompareVersions(t *testing.T) { } } -func TestTruncateLeft(t *testing.T) { +func TestSanitizeName(t *testing.T) { var testDataSlice = []struct { input string maxLength int expected string }{ - {"way_shorter_than_max_length", 63, "way_shorter_than_max_length"}, - {"max_length", len("max_length"), "max_length"}, - {"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"}, - {"_one_longer_than_max_length", len("_one_longer_than_max_length") - 1, "one_longer_than_max_length"}, + {"way-shorter-than-max-length", 63, "way-shorter-than-max-length"}, + {"max-length", len("max-length"), "max-length"}, + {"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"}, } for index, testData := range testDataSlice { t.Run(string(index), func(t *testing.T) { - actual := TruncateLeft(testData.input, testData.maxLength) + actual := SanitizeName(testData.input, testData.maxLength) fmt.Printf("actual: %s,%d, input: %s,%d\n", actual, len(actual), testData.input, testData.maxLength) assert.Equal(t, testData.expected, actual) })