Skip to content

Commit

Permalink
chore: refactor unsafe regex for easier reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
smlx authored and shreddedbacon committed Feb 22, 2024
1 parent af8dcdf commit b97db9c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions cmd/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b97db9c

Please sign in to comment.