From 728537ceacf38953894eadb35fcf0ecff6357667 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Mon, 24 Jul 2023 15:28:27 +0800 Subject: [PATCH] chore: refactor unsafe regex for easier reuse --- cmd/helpers.go | 8 +++----- cmd/helpers_test.go | 5 +++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/helpers.go b/cmd/helpers.go index 7176566a..5243af93 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -10,13 +10,11 @@ import ( "github.com/spf13/pflag" ) +var unsafeRegex = regexp.MustCompile(`[^0-9a-z-]`) + // makeSafe ensures that any string is dns safe func makeSafe(in string) string { - out := regexp.MustCompile(`[^0-9a-z-]`).ReplaceAllString( - strings.ToLower(in), - "$1-$2", - ) - return out + return unsafeRegex.ReplaceAllString(strings.ToLower(in), "$1-$2") } // shortenEnvironment shortens the environment name down the same way that Lagoon does diff --git a/cmd/helpers_test.go b/cmd/helpers_test.go index 59524a25..4efaf97f 100644 --- a/cmd/helpers_test.go +++ b/cmd/helpers_test.go @@ -24,6 +24,11 @@ func Test_makeSafe(t *testing.T) { in: "Feature-Branch", want: "feature-branch", }, + { + name: "space in name", + in: "My Project", + want: "my-project", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {