Skip to content

Commit

Permalink
Trim leading hyphens from names (#143)
Browse files Browse the repository at this point in the history
Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>
  • Loading branch information
unguiculus authored Apr 22, 2019
1 parent 08d0724 commit bc772e2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit bc772e2

Please sign in to comment.