Skip to content

Commit

Permalink
fix GetCanonicalServiceName
Browse files Browse the repository at this point in the history
  • Loading branch information
sebv committed Oct 24, 2023
1 parent d6ed199 commit 2eabe81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/imagerunner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
)

func GetCanonicalServiceName(serviceName string) string {
serviceName = strings.TrimSpace(serviceName)
if serviceName == "" {
return ""
}
// make sure the service name has only lowercase letters, numbers, and underscores
canonicalName := strings.ToLower(regexp.MustCompile("[^a-z0-9-]").ReplaceAllString(serviceName, "-"))
canonicalName := strings.ToLower(regexp.MustCompile("[^a-zA-Z0-9-]").ReplaceAllString(serviceName, "-"))
// remove successives dashes
canonicalName = regexp.MustCompile("-+").ReplaceAllString(canonicalName, "-")
// avoids container names starting with dash
canonicalName = regexp.MustCompile("^-").ReplaceAllString(canonicalName, "s-")
return canonicalName
}
16 changes: 16 additions & 0 deletions internal/imagerunner/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package imagerunner

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestUtils_GetCanonicalServiceName(t *testing.T) {
assert.Equal(t, "", GetCanonicalServiceName(""))
assert.Equal(t, "foo", GetCanonicalServiceName("foo"))
assert.Equal(t, "fo123o", GetCanonicalServiceName("FO123o"))
assert.Equal(t, "fo1-23o", GetCanonicalServiceName("FO1_23o"))
assert.Equal(t, "fo1-23o", GetCanonicalServiceName(" FO1 23o "))
assert.Equal(t, "s-23ao", GetCanonicalServiceName("_23Ao"))
}

0 comments on commit 2eabe81

Please sign in to comment.