fix(dockerCompose): add port.ExistProtocol

This commit is contained in:
Markus Pesch 2025-02-20 12:33:57 +01:00
parent 54af4469a4
commit 0a68b2d8e7
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 71 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package dockerCompose
import (
"fmt"
"regexp"
"strings"
)
@ -1973,3 +1974,23 @@ func splitStringInVolume(s string) (string, string, string) {
}
return src, dest, ""
}
var protocolRegExp = regexp.MustCompile(`/(?<protocol>[a-z]*)$`)
type port string
func (p port) existsProtocol() bool {
return protocolRegExp.MatchString(string(p))
}
func (p port) getProtocol() string {
result := make(map[string]string, 0)
matches := protocolRegExp.FindStringSubmatch(string(p))
for i, name := range protocolRegExp.SubexpNames() {
if i != 0 && len(name) > 0 {
result[name] = matches[i]
}
}
return result["protocol"]
}

View File

@ -0,0 +1,50 @@
package dockerCompose
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestPort_ExistsProtocol(t *testing.T) {
require := require.New(t)
testCases := []struct {
s string
expectedResult bool
}{
{
s: "0",
expectedResult: false,
},
{
s: "53",
expectedResult: false,
},
{
s: "53/tcp",
expectedResult: true,
},
{
s: "53/udp",
expectedResult: true,
},
{
s: "53:53",
expectedResult: false,
},
{
s: "53:53/udp",
expectedResult: true,
},
{
s: "53:53/tcp",
expectedResult: true,
},
}
for _, testCase := range testCases {
p := port(testCase.s)
require.Equal(testCase.expectedResult, p.existsProtocol())
}
}